在Spring中基于Java类进行配置的完整步骤
前言
JavaConfig原来是Spring的一个子项目,它通过Java类的方式提供Bean的定义信息,在Spring4的版本,JavaConfig已正式成为Spring4的核心功能。
本文将详细介绍关于Spring中基于Java类进行配置的相关内容,下面话不多说了,来一起看看详细的介绍吧
1定义Bean
普通的POJO只要标注了@Configuration注解,就可以为Spring容器提供Bean的定义信息。
@Configuration publicclassSystemConfig{ /** *定义Bean,并实例化 * *@return */ @Bean publicUserDaouserDao(){ returnnewUserDao(); } @Bean publicDeptDaodeptDao(){ returnnewDeptDao(); } /** *定义UserService,并把之前定义的UserDao与DeptDao注入进来 * *@return */ @Bean publicUserServiceuserService(){ UserServiceservice=newUserService(); service.setUserDao(userDao()); service.setDeptDao(deptDao()); returnservice; } }
这个类的方法标注了@Bean注解,即为定义Bean,Bean的类型由方法返回值的类型决定,名称默认和方法名同名,也可以通过入参显示指定Bean名称,比如@Bean(name=”xxx”)。@Bean所标注的方法体提供了实例化Bean的逻辑。
以上配置和下面的xml是等效的:
基于Java类的配置方式和基于XML或者基于注解的配置方式相比——
- Java类的配置方式通过代码编程的方式,可以更加灵活地实例化Bean和装配Bean之间的关系。
- XML或者基于注解的方式都是通过声明来定义配置的,所以灵活性上要逊一些,但在配置上更简单。
因为@Configuration注解类本身已经标注了@Component,所以这些类可以像那些普通的Bean一样被注入到其他的Bean中。
@Configuration publicclassApplicationConfig{ @Autowired privateSystemConfigsystemConfig; @Bean publicAuthorityServiceauthorityService(){ AuthorityServiceservice=newAuthorityService(); service.setUserDao(systemConfig.userDao()); service.setDeptDao(systemConfig.deptDao()); returnservice; } }
Spring会对配置类中所有标注了@Bean的方法使用AOP增强,引入Bean的生命周期管理逻辑。比如上面的systemConfig.userDao(),它返回的是对应Bean的单例。
在@Bean中,我们还可以通过标注@Scope注解来控制Bean的作用范围:
@Scope("prototype") @Bean publicDeptDaodeptDao(){ returnnewDeptDao(); }
这样每次调用deptDao()方法都会返回一个新的实例:
assertNotSame(authorityService.getDeptDao().hashCode(),authorityService .getDeptDao().hashCode());
注意:使用基于Java类进行配置,类路径下必须有SpringAOP与CGLib库。
2启动Spring容器
2.1只使用@Configuration类
可以使用AnnotationConfigApplicationContext类的构造函数传入标注了@Configuration的Java类来启动Spring容器。
ApplicationContextcontext=newAnnotationConfigApplicationContext(SystemConfig .class); UserServiceuserService=(UserService)context.getBean("userService"); assertNotNull(userService);
如果存在多个@Configuration配置类,那么可以AnnotationConfigApplicationContext中注册它们,然后再通过刷新容器应用这些配置类:
AnnotationConfigApplicationContextcontext=newAnnotationConfigApplicationContext(); //注册多个配置类 context.register(SystemConfig.class); context.register(ApplicationConfig.class); //刷新容器(应用这些配置类) context.refresh(); ApplicationConfigconfig=context.getBean(ApplicationConfig.class); assertNotNull(config);
也可以通过@Import将多个配置类组装到一个配置类中,然后仅需注册这个组装好的配置类,即可启动容器:
@Configuration @Import(SystemConfig.class) publicclassApplicationConfig2{ @Autowired privateSystemConfigsystemConfig; @Bean publicAuthorityServiceauthorityService(){ AuthorityServiceservice=newAuthorityService(); service.setUserDao(systemConfig.userDao()); service.setDeptDao(systemConfig.deptDao()); returnservice; } }
单元测试:
AnnotationConfigApplicationContextcontext=newAnnotationConfigApplicationContext(ApplicationConfig2.class); ApplicationConfig2config=context.getBean(ApplicationConfig2.class); assertNotNull(config); finalAuthorityServiceauthorityService=config.authorityService(); assertNotNull(authorityService.getDeptDao()); assertNotSame(authorityService.getDeptDao().hashCode(),authorityService .getDeptDao().hashCode());
2.2使用XML文件引用@Configuration类的配置
标注了@Configuration的配置类也是一个Bean,所以它也可以被Spring的
2.3在@Configuration类中引用XML文件的配置
在@Configuration配置类中可以直接通过@ImportResource引入XML的配置文件,这样就可以直接通过@Autowired引用xml配置文件中定义的Bean。
配置文件:
@Configuration类:
@ImportResource("classpath:beans5-11.xml") @Configuration publicclassServiceConfig{ @Bean @Autowired publicRelationServicerelationService(GroupDaogroupDao,RoleDaoroleDao){ RelationServiceservice=newRelationService(); service.setGroupDao(groupDao); service.setRoleDao(roleDao); returnservice; } }
单元测试:
AnnotationConfigApplicationContextcontext=newAnnotationConfigApplicationContext (ServiceConfig.class); ServiceConfigconfig=context.getBean(ServiceConfig.class); assertNotNull(config); RelationServiceservice=config.relationService((GroupDao)context.getBean ("groupDao"), (RoleDao)context .getBean ("roleDao")); assertNotNull(service.getRoleDao());
只要这些不同形式Bean的定义信息能够加载到Spring容器中,那么Spring就可以智能的完成Bean之间的装配。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持。