spring基础系列之JavaConfig配置详解
早以前,Spring推荐使用XML的方式来定义Bean及Bean之间的装配规则,但是在Spring3之后,Spring提出的强大的JavaConfig这种类型安全的Bean装配方式,它基于Java代码的灵活性,使得装配的过程也变得及其灵活。
使用JavaConfig来装配Bean拥有其自己的一套规则,我们在这里来看一看:
1、规则
规则一:@Configuration注解
我们在定义JavaConfig类时,都会在其上加注@Configuration注解,来表明这是一个配置类,@Configuration注解底层是@Component注解,而且这个注解会被AnnotationConfigApplicationContext来进行加载,AnnotationConfigApplicationContext是ApplicationContext的一个具体实现,代表依据配置注解启动应用上下文。
规则二:@ComponentScan注解
我们使用JavaConfig的目的是为了实现以前XML配置实现的功能,首先就是组件扫描功能,将我们使用特定注解标注的类统一扫描加载到Spring容器,这一功能就是依靠@ComponentScan注解来实现的,我们可以为其指定位置参数来指定要扫描的包。
规则三:@Bean注解
使用@Bean注解我们可以实现XML配置中手动配置第三方Bean的功能,这里我们使用方法来定义Bean,并在方法前面加注@Bean注解,表示要将该方法返回的对象加载到Spring容器中,这样就对我们的方法定义带来了一些限制,这些限制包括方法的大概格式:
1-方法带返回值,且返回类型为你要加载的第三方类类型
2-方法的名称为默认的Bean的name,如果要自定义Bean的name,可以使用@Bean注解的name属性。
3-要实现注入只需要将要注入的Bean的类型作为参数,调用该类的带参数的构造器构建这个Bean,或者采用第二种方式:先创建这个类的对象,然后调用该对象的set方法进行注入,以被注入的Bean的方法为参数
规则验证:
首先我们创建几个测试类
针对第一种注入方式:
1-StudentService
importorg.springframework.stereotype.Service;
@Service
publicclassStudentService{
publicvoidstudy(){
System.out.println("学生学习Java");
}
}
2-TeacherService
importorg.springframework.stereotype.Service;
@Service
publicclassTeacherService{
privateStudentServicestudentService;
publicTeacherService(StudentServicestudentService){
this.studentService=studentService;
}
publicvoidteach(){
studentService.study();
}
}
3-Config:这是针对第一种注入方式而设,需要在TeacherService中定义带参数的构造器
importorg.springframework.context.annotation.Bean;
//importorg.springframework.context.annotation.ComponentScan;
importorg.springframework.context.annotation.Configuration;
@Configuration
//@ComponentScan
publicclassConfig{
@Bean(name="student")
publicStudentServicestudentService(){
returnnewStudentService();
}
@Bean(name="teacher")
publicTeacherServiceteacherService(StudentServicestudentService){
returnnewTeacherService(studentService);
}
}
针对第二种注入方式:
1-StudentService
publicclassStudentService{
publicvoidstudy(){
System.out.println("学生在学习Java");
}
}
2-TeacherService
publicclassTeacherService{
privateStudentServicestudentService;
publicStudentServicegetStudentService(){
returnstudentService;
}
publicvoidsetStudentService(StudentServicestudentService){
this.studentService=studentService;
}
publicvoidteach(){
studentService.study();
}
}
3-Config:这是采用第二种注入方式:需要在TeacherService中提供set方法
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
@Configuration
publicclassConfig{
@Bean
publicStudentServicestudent(){
returnnewStudentService();
}
@Bean
publicTeacherServiceteacher(){
TeacherServiceteacherService=newTeacherService();
teacherService.setStudentService(student());
returnteacherService;
}
}
4-测试类:TestMain
importjava.util.Iterator;
importorg.springframework.context.annotation.AnnotationConfigApplicationContext;
publicclassTestMain{
publicstaticvoidmain(String[]args){
AnnotationConfigApplicationContextacac=newAnnotationConfigApplicationContext(Config.class);
TeacherServiceteacher=acac.getBean(TeacherService.class);
teacher.teach();
Iteratori=acac.getBeanFactory().getBeanNamesIterator();
while(i.hasNext()){
System.out.println(i.next());
}
acac.close();
}
}
执行结果:
七月14,20174:10:56下午org.springframework.context.annotation.AnnotationConfigApplicationContextprepareRefresh 信息:Refreshingorg.springframework.context.annotation.AnnotationConfigApplicationContext@7e6cbb7a:startupdate[FriJul1416:10:56CST2017];rootofcontexthierarchy 学生学习Java org.springframework.context.annotation.internalConfigurationAnnotationProcessor org.springframework.context.annotation.internalAutowiredAnnotationProcessor org.springframework.context.annotation.internalRequiredAnnotationProcessor org.springframework.context.annotation.internalCommonAnnotationProcessor org.springframework.context.event.internalEventListenerProcessor org.springframework.context.event.internalEventListenerFactory config student teacher environment systemProperties systemEnvironment org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry messageSource applicationEventMulticaster lifecycleProcessor 七月14,20174:10:59下午org.springframework.context.annotation.AnnotationConfigApplicationContextdoClose 信息:Closingorg.springframework.context.annotation.AnnotationConfigApplicationContext@7e6cbb7a:startupdate[FriJul1416:10:56CST2017];rootofcontexthierarchy
该测试结果中打印出的是Spring上下文中所有加载的Bean的名称(name)。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。