spring基础系列--JavaConfig配置
原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/7171011.html
早以前,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
1 import org.springframework.stereotype.Service; 2 3 @Service 4 public class StudentService { 5 public void study(){ 6 System.out.println("学生学习Java"); 7 } 8 }
2-TeacherService
1 import org.springframework.stereotype.Service; 2 3 @Service 4 public class TeacherService { 5 6 private StudentService studentService; 7 8 public TeacherService(StudentService studentService){ 9 this.studentService=studentService; 10 } 11 12 public void teach(){ 13 studentService.study(); 14 } 15 }
3-Config:这是针对第一种注入方式而设,需要在TeacherService 中定义带参数的构造器
1 import org.springframework.context.annotation.Bean; 2 //import org.springframework.context.annotation.ComponentScan; 3 import org.springframework.context.annotation.Configuration; 4 5 @Configuration 6 //@ComponentScan 7 public class Config { 8 9 @Bean(name="student") 10 public StudentService studentService(){ 11 return new StudentService(); 12 } 13 14 @Bean(name="teacher") 15 public TeacherService teacherService(StudentService studentService){ 16 return new TeacherService(studentService); 17 } 18 19 }
针对第二种注入方式:
1-StudentService
1 public class StudentService { 2 3 public void study(){ 4 System.out.println("学生在学习Java"); 5 } 6 7 }
2-TeacherService
1 public class TeacherService { 2 3 private StudentService studentService; 4 5 public StudentService getStudentService() { 6 return studentService; 7 } 8 9 public void setStudentService(StudentService studentService) { 10 this.studentService = studentService; 11 } 12 13 public void teach(){ 14 studentService.study(); 15 } 16 17 }
3-Config:这是采用第二种注入方式:需要在TeacherService中提供set方法
1 import org.springframework.context.annotation.Bean; 2 import org.springframework.context.annotation.Configuration; 3 4 @Configuration 5 public class Config { 6 7 @Bean 8 public StudentService student(){ 9 return new StudentService(); 10 } 11 12 @Bean 13 public TeacherService teacher(){ 14 TeacherService teacherService = new TeacherService(); 15 teacherService.setStudentService(student()); 16 return teacherService; 17 } 18 19 }
4-测试类:TestMain
1 import java.util.Iterator; 2 3 import org.springframework.context.annotation.AnnotationConfigApplicationContext; 4 5 public class TestMain { 6 7 public static void main(String[] args) { 8 AnnotationConfigApplicationContext acac = new AnnotationConfigApplicationContext(Config.class); 9 TeacherService teacher = acac.getBean(TeacherService.class); 10 teacher.teach(); 11 Iterator<String> i = acac.getBeanFactory().getBeanNamesIterator(); 12 while(i.hasNext()){ 13 System.out.println(i.next()); 14 } 15 acac.close(); 16 } 17 18 }
执行结果:
七月 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)。
转载于:https://www.cnblogs.com/V1haoge/p/7171011.html
spring基础系列--JavaConfig配置相关推荐
- Spring boot 系列 入门--配置
简介 Spring Boot 并不是一个全新的框架,而是将已有的 Spring 组件整合起来.特点是去掉了繁琐的 XML 配置,改使用约定或注解.所以熟悉了 Spring Boot 之后,开发效率将会 ...
- springboot跳转html_畅游Spring Boot系列 — 自定义配置
这里要说的自定义配置主要是两类:一类是关于Spring MVC的扩展配置,一类是Spring Boot自身通过配置文件的自定义配置 首先,我们来看一下SpringBoot中关于Spring MVC的相 ...
- Spring基础知识和配置
Spring 框架两大核心机制(IoC.AOP) idea运行spring中遇到的问题参考 idea配置遇到的问题 IoC(控制反转)/ DI(依赖注入) AOP(面向切面编程) Spring 是一个 ...
- delve 调试带参数_带你学够浪:Go语言基础系列-环境配置和 Hello world
前面几周陆陆续续写了一些后端技术的文章,包括数据库.微服务.内存管理等等,我比较倾向于成体系的学习,所以数据库和微服务还有后续系列文章补充. 最近工作上比较多的 Golang 编程,现在很多互联网公司 ...
- Spring基础系列-参数校验
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9953744.html Spring中使用参数校验 概述 JSR 303中提出了Bea ...
- Spring Boot 系列:过滤器+拦截器+监听器
原 Swagger 文章合并到 Spring Boot 系列:配置 Swagger2 一.过滤器 - Filter 过滤器是处于客户端和服务器资源文件之间的一道过滤网,帮助我们过滤掉一些不符合要求的请 ...
- java小马哥百度网盘_小马哥spring boot和spring cloud系列
资源内容: 小马哥spring boot和spring cloud系列|____小马哥 Java 微服务实践 - Spring Boot 系列 |____pptx ...
- 小马哥java_小马哥 Java 微服务实践 - Spring Cloud 系列
资源内容: 小马哥 Java 微服务实践 - Spring Cloud 系列|____Java 微服务实践 - Spring Cloud系列(四)服务发现注册.wmv|____Java 微服务实践 - ...
- Spring Batch –用JavaConfig替换XML作业配置
最近,我协助一个客户启动并运行了Spring Batch实现. 该团队决定继续使用针对批处理作业的基于JavaConfig的配置,而不是传统的基于XML的配置. 随着这越来越成为配置Java应用程序的 ...
最新文章
- java重定向带参数_急 求助重新封装重定向带参数问题
- nc65语义模型设计_完整word版,NC数据加工做语义模型
- 你在发表理科学术文章过程中有哪些经验值得借鉴
- jpa jdbc jndi_没有J2EE容器的JNDI和JPA
- 利用 VBA 和 HTML自制兼容 WPS及 EXCEL(32位/64位)的颜色选择器
- 以下各节已定义,但尚未为布局页“~/Views/_LayoutHome.cshtml”呈现:“mainContent; jsSrc”。...
- pg_restore使用
- 让网站和APP更具动感的几点建议
- win10 python ffmpeg推流到b站
- 微信小程序--萌系登陆界面
- 开源边缘计算平台研究分析
- 使用mask雕刻镂空背景
- apache24+php8配置
- ThinkPad L13笔记本怎么U盘重装系统教学
- 计算机图形学--扫描线填充算法
- Options, Futures and Other Derivatives 读书笔记(三)—— CHP4
- COD测定仪小故障不及时处理,可能会演变成的大问题
- CustomerList
- 油气勘探开发从业务到IT的一体化解决方案(全文)
- 计算机网络基础之Internet(因特网)
热门文章
- Hibernate getCurrentSession()和openSession()的区别
- 在Linux 上安装WAS7.0
- java httpclient 为邮箱添加来信转发规则
- 如何利用webmin在Linux主机中添加网站
- 各种操作系统ping时的TTL值
- 【Leetcode】103. 二叉树的锯齿形层次遍历
- C#读取AD域用户信息
- Apache activemq入门示例(maven项目)
- 软件测试--测试Demo
- 出现An App ID with Identifier 'com.XXX.XXX’ is not available. Please enter a different string.