Welcome to the spring framework series, In this article we will understand how spring application is configured using annotation based and java based container configuration. In the previous part of this blog post series, we have understood how to create a Spring IOC Container from XML configuration. We put all the bean definitions in XML which was read by the spring and created ApplicationContext. Creating a container from xml can be replaced with modern approach such as annotation and java based configuration.
Annotation and Java configuration
Annotation and Java based configuration can be used to replace xml based container configuration to create spring IOC Container. Adding annotation such as @Component, @Configuration …etc to the source level of class is an example of how spring bean is declared. When the spring IOC Container scans the classpath, It will load and register all the annotated beans so that they become managed spring beans.
The following are some of the annotation used to define class as managed bean in spring. @Component, @Configuration, @Controller, @Service, @Repository, @Entity ..etc.
Any class marked with above annotation will be loaded and registered by spring while scanning classpath.
@Bean-@Configuration
In the following section, we will look at how to configure, initialize and assemble beans using java based container configuration. The two main annotation that we would use are @Bean and @Configuration. @Bean annotation plays the same role as <bean/> element from XML. @Bean annotation specified on method indicates that the annotated method would create, configure and initialize new instance of bean. @Bean annotated method would be specified in @Configuration class.
Any class that is annotated with @Configuration denotes that the class will be a source for bean definitions. The following example shows how @Configuration and @Bean annotation acts as metadata for the spring to load and register AppConfig, FileServiceConfig beans into the container.
// configuration with bean definitions
@Configuration
class AppConfig {
@Bean
public FileServiceConfig fileServiceConfig() {
FileServiceConfig fileServiceConfig = new FileServiceConfig();
fileServiceConfig.setStoragePath("/tmp");
fileServiceConfig.setMaxFileSize(12390000);
return fileServiceConfig;
}
}
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext(AppConfig.class);
FileServiceConfig fileServiceConfig = ctx.getBean("fileServiceConfig");
System.out.println(fileServiceConfig);
}
@Import
In this section, we will understand the role of @Import annotation. In the previous section, we have seen how to create Configuration class. We can split single configuration class into multiple configuration class. When we have to create ApplicationContext, we have to specify all the configuration beans individually. Instead of registering or specifying all the configuration, We can specify single configuration bean and Import all the other configuration beans with @Import annotation. Let’s look at the below example to understand how @Import can be used.
@Configuration
class ObjectStorageConfig {
}
@Configuration
class DatabaseConfig {
}
// example app configuration with import
@Configuration
@Import({"ObjectStorageConfig.class","DatabaseConfig.class"})
class AppConfig {
}
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
DatabaseConfig databaseConfig = ctx.getBean("databaseConfig");
Conclusion
In this blog post, we have seen how to create IOC container without xml configuration. How we can use annotation based java configuration to create spring container. We have also seen a simple example of java configuration with bean defintion. Then, we understood how to aggregate multiple configuration classes with @Import annotation.