今天就跟大家聊聊有关使用spring怎么对JavaConfig进行配置,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
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
import org.springframework.stereotype.Service;
@Service
public class StudentService {
public void study(){
System.out.println("学生学习Java");
}
}
2-TeacherService
import org.springframework.stereotype.Service;
@Service
public class TeacherService {
private StudentService studentService;
public TeacherService(StudentService studentService){
this.studentService=studentService;
}
public void teach(){
studentService.study();
}
}
3-Config:这是针对第一种注入方式而设,需要在TeacherService 中定义带参数的构造器
import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
//@ComponentScan
public class Config {
@Bean(name="student")
public StudentService studentService(){
return new StudentService();
}
@Bean(name="teacher")
public TeacherService teacherService(StudentService studentService){
return new TeacherService(studentService);
}
}
针对第二种注入方式:
1-StudentService
public class StudentService {
public void study(){
System.out.println("学生在学习Java");
}
}
2-TeacherService
public class TeacherService {
private StudentService studentService;
public StudentService getStudentService() {
return studentService;
}
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
public void teach(){
studentService.study();
}
}
3-Config:这是采用第二种注入方式:需要在TeacherService中提供set方法
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config {
@Bean
public StudentService student(){
return new StudentService();
}
@Bean
public TeacherService teacher(){
TeacherService teacherService = new TeacherService();
teacherService.setStudentService(student());
return teacherService;
}
}
4-测试类:TestMain
import java.util.Iterator;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestMain {
public static void main(String[] args) {
AnnotationConfigApplicationContext acac = new AnnotationConfigApplicationContext(Config.class);
TeacherService teacher = acac.getBean(TeacherService.class);
teacher.teach();
Iterator<String> i = acac.getBeanFactory().getBeanNamesIterator();
while(i.hasNext()){
System.out.println(i.next());
}
acac.close();
}
}
执行结果:
七月 14, 2017 4:10:56 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7e6cbb7a: startup date [Fri Jul 14 16:10:56 CST 2017]; root of context hierarchy
学生学习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, 2017 4:10:59 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7e6cbb7a: startup date [Fri Jul 14 16:10:56 CST 2017]; root of context hierarchy
该测试结果中打印出的是Spring上下文中所有加载的Bean的名称(name)。
看完上述内容,你们对使用spring怎么对JavaConfig进行配置有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。