Preface
JavaConfig turned out to be a subproject of Spring. It provides Bean definition information through Java classes. In the Spring4 version, JavaConfig has officially become the core function of Spring4.
This article will introduce in detail the relevant content about configuration based on Java classes in Spring. I won’t say much below. Let’s take a look at the detailed introduction together.
1 Definition Bean
Ordinary POJO can provide Bean definition information for Spring containers as long as the @Configuration annotation is marked.
@Configurationpublic class SystemConfig { /** * Define the Bean and instantiate * * @return */ @Bean public UserDao userDao() { return new UserDao(); } @Bean public DeptDao deptDao() { return new DeptDao(); } /** * Define the UserService and inject the previously defined UserDao and DeptDao* * @return */ @Bean public UserService userService() { UserService service = new UserService(); service.setUserDao(userDao()); service.setDeptDao(deptDao()); return service; }}The method of this class is annotated by the @Bean annotation, that is, to define the bean. The type of the bean is determined by the type of the method return value. The name defaults to the method name and the method name. You can also specify the bean name by entering the parameter, such as @Bean(name=”xxx”). The method body marked by @Bean provides the logic to instantiate the Bean.
The above configuration is equivalent to the following xml:
<bean id="userDao"/><bean id="deptDao"/><bean id="userService"p:userDao-ref="userDao" p:deptDao-ref="deptDao"/>
Compared with the configuration method based on Java classes, which is based on XML or annotation-based configuration method -
Because the @Configuration annotation class itself has @Component, these classes can be injected into other beans like ordinary beans.
@Configurationpublic class ApplicationConfig { @Autowired private SystemConfig systemConfig; @Bean public AuthorityService authorityService() { AuthorityService service = new AuthorityService(); service.setUserDao(systemConfig.userDao()); service.setDeptDao(systemConfig.deptDao()); return service; }}Spring uses AOP enhancement for all methods marked @Bean in the configuration class, introducing the lifecycle management logic of the bean. For example, systemConfig.userDao() above, it returns a singleton corresponding to the bean.
In @Bean, we can also control the scope of the bean by annotating the @Scope annotation:
@Scope("prototype")@Beanpublic DeptDao deptDao() { return new DeptDao();}This way, each call to the deptDao() method will return a new instance:
assertNotSame(authorityService.getDeptDao().hashCode(),authorityService.getDeptDao().hashCode());
Note: Use Java-based classes for configuration, Spring AOP and CGLib libraries must be included in the classpath.
2 Start the Spring container
2.1 Use only the @Configuration class
You can use the constructor of the AnnotationConfigApplicationContext class to pass in the Java class marked @Configuration to start the Spring container.
ApplicationContext context=new AnnotationConfigApplicationContext(SystemConfig.class);UserService userService= (UserService) context.getBean("userService");assertNotNull(userService);If there are multiple @Configuration configuration classes, you can register them in the AnnotationConfigApplicationContext and then apply these configuration classes by refreshing the container:
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext();//Register multiple configuration classes context.register(SystemConfig.class);context.register(ApplicationConfig.class);//Refresh the container (apply these configuration classes) context.refresh();ApplicationConfig config=context.getBean(ApplicationConfig.class);assertNotNull(config);
You can also assemble multiple configuration classes into one configuration class through @Import, and then just register the assembled configuration class to start the container:
@Configuration@Import(SystemConfig.class)public class ApplicationConfig2 { @Autowired private SystemConfig systemConfig; @Bean public AuthorityService authorityService() { AuthorityService service = new AuthorityService(); service.setUserDao(systemConfig.userDao()); service.setDeptDao(systemConfig.deptDao()); return service; }}Unit Tests:
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(ApplicationConfig2.class);ApplicationConfig2 config=context.getBean(ApplicationConfig2.class);assertNotNull(config);final AuthorityService authorityService = config.authorityService();assertNotNull(authorityService.getDeptDao());assertNotSame(authorityService.getDeptDao().hashCode(),authorityService .getDeptDao().hashCode());
2.2 Using XML files to refer to the configuration of the @Configuration class
The configuration class marked @Configuration is also a bean, so it can also be scanned by Spring's <context:component-scan>. Therefore, if you want to assemble the configuration class into the XML configuration file and start Spring through the XML configuration file, you only need to scan the corresponding configuration class through <context:component-scan> in XML.
<context:component-scan base-package="net.deniro.spring4.conf" resource-pattern="ApplicationConfig2.class" />
2.3 Configuration of reference XML files in @Configuration class
In the @Configuration configuration class, you can directly introduce XML configuration files through @ImportResource, so that you can directly refer to the beans defined in the xml configuration file through @Autowired.
Configuration file:
<bean id="groupDao"/><bean id="roleDao"/>
@Configuration class:
@ImportResource("classpath:beans5-11.xml")@Configurationpublic class ServiceConfig { @Bean @Autowired public RelationService relationService(GroupDao groupDao,RoleDao roleDao){ RelationService service=new RelationService(); service.setGroupDao(groupDao); service.setRoleDao(roleDao); return service; }}Unit Tests:
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext (ServiceConfig.class);ServiceConfig config=context.getBean(ServiceConfig.class);assertNotNull(config);RelationService service=config.relationService((GroupDao) context.getBean ("groupDao"), (RoleDao) context .getBean ("roleDao"));assertNotNull(service.getRoleDao());As long as these definition information of different forms of beans can be loaded into Spring containers, Spring can intelligently complete the assembly between beans.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.