@configuration annotation in Spring 3
In the last section we developed the .xml file to configure the IOC (Spring Container). In this section we will learn another method for configuring the Spring 3 IOC, which is through the Java configuration annotation @configuration. The @configuration annotation is code centric and can be used in place of xml configuration file.
Two way to configure Spring IOC container
a) xml file: Developer can define the bean configuration in xml file and then instruct the Spring to use the xml file to configure the beans. The xml file configuration method is targeted to infrastructure providers.
b) Annotation: The @configuration spring annotation can also be used to configure the Spring IOC container. Here programmer add the @configuration to the Java class and this class is considered as special configuration class. The @configuration annotation method is targeted the developers. This is pure-java approach to configure the Spring IoC Container. The @Bean tag is used to define the bean, and the Spring framework executes the method and then register the object returned. By default the name of the method is used as the bean name.
Step 1:
Download the asm-3.2 from http://forge.ow2.org/project/showfiles.php?group_id=23&release_id=3334 and the add the jar file into project workspace.
Step 2:
Download cglib from http://cglib.sourceforge.net/ and then add the jar file into project workspace. Above jar files are required to work with @configuration annotation. These are runtime code generation library used by the Spring 3.0 framework.
Step 3:
Creating the configuration file: Create a new java file (Spring3HelloWorldConfig.java) in net.roseindia package. This file is used to programmatically configure the beans in the application. In our example we will configure Spring3HelloWorld bean. Here is the code of Spring3HelloWorldConfig.java:
package net.roseindia;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Spring3HelloWorldConfig {
public @Bean Spring3HelloWorld spring3HelloWorld() {
return new Spring3HelloWorld();
}
}
The @configuration spring annotation configures as special class to be used by the Spring IoC to configure beans. The @Bean annotation instruct the IoC container to execute the function spring3HelloWorld() function and register the returned class with the name spring3HelloWorld. Now we will see how to configure the IoC container and retrieve the spring3HelloWorld bean.
Step 4:
Now create a new java class (Spring3HelloWorldConfigTest.java) and add the following code:
package net.roseindia;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; 0
public class Spring3HelloWorldConfigTest {
1
public static void main(String[] args) {
//Initialize IoC Container
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( 2
Spring3HelloWorldConfig.class);
System.out.println("Calling Bean method: sayHello()"); 3
//Retrieve the bean from Container
Spring3HelloWorld myBean = (Spring3HelloWorld) context
.getBean("spring3HelloWorld"); 4
myBean.sayHello();
} 5
}
Following code initializes the Spring IoC and configures the beans defined in the Spring3HelloWorldConfig class. 6
//Initialize IoC Container
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
Spring3HelloWorldConfig.class); 7
To retrieve the bean from context we can use the following code:
Spring3HelloWorld myBean = (Spring3HelloWorld) context
.getBean("spring3HelloWorld"); 8
Step 5:
To run the example open Spring3HelloWorldConfigTest.java in Eclipse editor window, right click and then select Run As -> Java application. This will execute the code and following output will be displayed on the console.
Jan 2, 2010 2:50:22 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh 9
INFO: Refreshing
org.springframework.context.annotation.AnnotationConfigApplicationContext@c1b531:
startup date [Sat Jan 02 14:50:22 IST 2010]; root of context hierarchy
Jan 2, 2010 2:50:22 PM org.springframework.context.annotation.ConfigurationClassEnhancer enhance
INFO: Successfully enhanced
net.roseindia.Spring3HelloWorldConfig; enhanced class name is:
net.roseindia.Spring3HelloWorldConfig$$EnhancerByCGLIB$$196e06e4
0
Jan 2, 2010 2:50:22 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory@1888759:
defining beans
[org.springframework.context.annotation.internalConfigurationAnnotationProcessor,
org.springframework.context.annotation.internalAutowiredAnnotationProcessor,
org.springframework.context.annotation.internalRequiredAnnotationProcessor,
org.springframework.context.annotation.internalCommonAnnotationProcessor,
spring3HelloWorldConfig,spring3HelloWorld]; root of factory hierarchy
Calling Bean method: sayHello() 1
Hello Spring 3.0
In this section we learned about the @configuration annotation. In the next section we will learn the architecture of Spring 3 Framework. 2