Spring java based configuration example

In this post , we will see how to configure spring with java based configuration. In previous post, we have already seen how to configure spring using xml configuration. Spring provides @configuaration and @Bean annotation for java based configuration.

Steps for spring java based configuration:

Project structure for below project:


1. Create a simple java maven project.

2. Maven dependency

put spring and cglib maven dependency in pom.xml.
<dependencies>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>${spring.version}</version>
  </dependency>
<!-- cglib required for @configuration annotation -->
   <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.0</version>
        </dependency>
 </dependencies>
 <properties>
  <spring.version>4.2.1.RELEASE</spring.version>
 </properties>
So your pom.xml will look like:
<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>org.arpit.java2blog</groupId>
  <artifactId>SpringHelloWorldJavaBasedConfiguration</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringHelloWorldJavaBasedConfiguration</name>
  <description>It provides hello world program for java based config
</description>

 <dependencies>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>${spring.version}</version>
  </dependency>
<!-- cglib required for @configuration annotation -->
   <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.0</version>
        </dependency>
 </dependencies>
 <properties>
  <spring.version>4.2.1.RELEASE</spring.version>
 </properties>
</project>

3. Create Bean class

Create a bean class called country.java in package org.arpit.java2blog.model
package org.arpit.java2blog.model;

public class Country {

    String countryName;
    
    public Country(String countryName) {
  this.countryName=countryName;
 }
 public String getCountryName() {
        return countryName;
    }
    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

}

4. Create application configuration class

This class will have @Configuaration and @Bean annotation .

Create class called ApplicationConfiguration.java in package org.arpit.java2blog.config
package org.arpit.java2blog.config;
import org.arpit.java2blog.model.Country;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ApplicationConfiguration {

 @Bean(name="countryObj")
 public Country getCountry()
 {
  return new Country("India");
 }
 
}

Above file is equivalent to below spring configuration xml
<?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>

5. Create main class to run the program

Create class called SpringJavaConfigMain.java
package org.arpit.java2blog.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.arpit.java2blog.config.ApplicationConfiguration;
import org.arpit.java2blog.model.Country;;

public class SpringJavaConfigMain {

 public static void main(String[] args) {
  @SuppressWarnings("resource")
  ApplicationContext appContext = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);
  Country countryObj = (Country) appContext.getBean("countryObj");
  String countryName=countryObj.getCountryName();
  
  System.out.println("Country name: "+ countryName);
 }
}

6. Run above program 

When you run above program, you will get below output.
Feb 19, 2016 4:09:08 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2096ed8b: startup date [Fri Feb 19 16:09:08 IST 2016]; root of context hierarchy
Country name: India

7. Source code 

click to begin
20KB .zip

Bingo!! we are done with spring java based configuaration.

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012