Spring Annotation Based Configuration

In previous post we have seen how to configure spring using XML configuration. Now in this example , we will see spring configuration using annotations –

Tools used to create below project – 

  1. Spring 5.0.7.RELEASE
  2. Maven 3.3
  3. Java 8
  4. Eclipse Neon.2

Github Link :   Download

Step 1: Create Simple Maven Project with Spring Configuration

Step 2: Configure maven dependencies

Below pom.xml file contains the dependencies required for spring project –

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.myjavablog</groupId>
<artifactId>SpringAnnotationBasedConfiguration</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>SpringAnnotationBasedConfiguration</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>5.0.7.RELEASE</spring.version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<!-- Spring 5 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>

</dependencies>
</project>

Below project structure will be created for you by Eclipse .


 

 

 

 

 

 

 

 

 

Step 3:  Create City.java under com.myjavablog.beans package –

package com.myjavablog.beans;

/**
* @author anupb
*
*/
public class City {

String cityName;

public City(String cityName) {
this.cityName = cityName;
}

public String getcityName() {
return cityName;
}

public void setcityName(String cityName) {
this.cityName = cityName;
}

}

This is the bean file where we have getters and setters defined.

Step 4: Create AnnotationConfig .java file under com.myjavablog.config package-

AnnotationConfig.java

/**
*
*/
package com.myjavablog.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.myjavablog.beans.City;

/**
* @author anupb
*
*/
@Configuration
public class AnnotationConfig {

@Bean(name="cityObj")
public City getCountry()
{
return new City("Pune");
}
}

This is Annotation based configuration to configure City bean. Its similar to XML based configuration which we did in previous post. The same Configuration can be done in XML as below –

<div id="crayon-5b537011533c5704156605-2" class="crayon-line crayon-striped-line"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<bean id="countryObj" class="org.arpit.java2blog.Country" >
<property name="countryName" value="India"/>
</bean>
</beans></div>

Step 5: Create a SpringJavaBasedConfigMain.java under com.myjavablog package –

package com.myjavablog;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.myjavablog.beans.City;
import com.myjavablog.config.AnnotationConfig;

/**
* Hello world!
*
*/
public class SpringJavaBasedConfigMain {
public static void main(String[] args) {

@SuppressWarnings("resource")
ApplicationContext appContext = new AnnotationConfigApplicationContext(AnnotationConfig.class);
City cityObj = (City) appContext.getBean("cityObj");
String cityName = cityObj.getcityName();

System.out.println("City name: " + cityName);
}
}

We have created ApplicationContext which actually loads bean definitions from annotation configuration i.e. AnnotationConfig.java

Step 6: Clean and install project

Now run the maven clean install phase as below –

Command:  mvn clean install

OR

Right click on project -> Debug As -> Maven build . Below popup will come up –


 

 

 

 

 

 

 

 

 

Step 7: Run the spring application as Java Application

 

 

 

 

 

 

 

 

Output:

City name: Pune

2 thoughts on “Spring Annotation Based Configuration”

  1. ApplicationContext appContext = new AnnotationConfigApplicationContext(AppConfig.class);

    I think, above line you need to change to below:

    ApplicationContext appContext = new AnnotationConfigApplicationContext(AnnotationConfig.class);

    Reply
    • Yes Sanjeev , Your observation is correct!!!
      I was playing around with different configurations so that it could be the reason i might have included wrong configuration.

      Thanks for pointing it out.
      Regards
      Anup

      Reply

Leave a Comment

Bitnami