Spring Java Based Configuration

Spring Java Based Configuration

Spring java based configuration is alternate method of providing bean definition to IoC container. Which reduces the amount of code we write to provide bean dependencies and definitions.

You can also look at the good working example on how we can use Java based configuration in our project here : CRUD Example using Spring, Hibernate and AngularJS

We have configured and managed spring bean container with the help of XML configuration metadata. Now we will lean how we can configure spring IoC container using Java Based configuration.

There are two annotations @Configuration and @Bean which plays important role in Java Based spring configuration. If we annotate our class with @Configuration annotation that indicates its purpose as bean definition.

You can easily define bean by using @Bean annotation inside @Configuration annotated class. @Bean is as same as defining bean with the help of <bean/> element in XML based configuration.

For Example
@Configuration
public class ApplicationConfig {

 @Bean
 public ServiceBean serviceBean() {
 return new ServiceBeanImpl();
 }
}
which is exactly same as below
<beans>
 <bean id="serviceBean" class="com.package.ServiceBean" />
</beans>

Now, lets understand java based configuration with a simple example.

Note*: For initial environment setup and configuration for Spring Framework 4.0, please refer this tutorial Hello World Example in Spring, we will proceed this setup.

Step 1

Please setup your environment with the above link in order to practice some spring core example. It is pretty easy to setting up all the required things.

Step 2

Create the directory structure as below
Spring Java Based Configuration
Spring Java Based Configuration

Step 3

Create a simple bean which will be configured later in this example.

TestBean.java
package com.testspring.beans;

public class TestBean {
 
 public void displayMessage() {
 System.out.println("Welcome to spring test.");
 }
}

Step 4

Create Java based configuration class with the help of @Configuration and @Bean annotations.

ApplicationConfig.java
package com.testspring.config;

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

import com.testspring.beans.TestBean;

@Configuration
public class ApplicationConfig {

 @Bean
 public TestBean testBean() {
 return new TestBean();
 }
}

Step 5

Create a Main class which will create a container and container will read the bean definition from Annotated class ApplicationConfig.java. Since we are using Annotation and Java based configuration then we will initiate our container with the help of AnnotationConfigApplicationContext.

MainClass.java
package com.testspring.main;

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

import com.testspring.beans.TestBean;
import com.testspring.config.ApplicationConfig;

public class MainClass {

 public static void main(String[] args) {
 
 ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
 
 TestBean testBean = (TestBean) context.getBean("testBean");
 
 testBean.displayMessage();

 }

}
That’s it, further you can do more with java based configuration as below.

How to programmatically build container and register configuration


You can also register configuration class with the help of register method of AnnotationConfigApplicationContext.

Consider the below example
package com.testspring.main;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.testspring.beans.TestBean;
import com.testspring.config.ApplicationConfig;

public class MainClass {

 public static void main(String[] args) {
 
 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
 context.register(ApplicationConfig.class);
 context.refresh();
 
 TestBean testBean = (TestBean) context.getBean("testBean");
 
 testBean.displayMessage();

 }

}

How to enable scanning of configuration classes

If you don’t want to specify the class name of the configuration, you can also use @ComponentScan annotation which takes package name as value of basePackages attribute.

Modify ApplicationConfig.java as below
package com.testspring.config;

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

import com.testspring.beans.TestBean;

@Configuration
@ComponentScan(basePackages = "com.testspring.config")
public class ApplicationConfig {

 @Bean
 public TestBean testBean() {
 return new TestBean();
 }
}
And MainClass.java as below
package com.testspring.main;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.testspring.beans.TestBean;

public class MainClass {

 public static void main(String[] args) {
 
 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
 context.scan("com.testspring.config");
 context.refresh();
 
 TestBean testBean = (TestBean) context.getBean("testBean");
 
 testBean.displayMessage();

 }

}

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *