原创作品,可以转载,但是请标注出处地址: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

 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配置的更多相关文章

  1. SpringBoot基础系列-SpringBoot配置

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9990680.html SpringBoot基础系列-SpringBoot配置 概述 属性 ...

  2. Spring基础系列-AOP源码分析

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9560803.html 一.概述 Spring的两大特性:IOC和AOP. AOP是面向切 ...

  3. Spring基础系列--AOP织入逻辑跟踪

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9619910.html 其实在之前的源码解读里面,关于织入的部分并没有说清楚,那些前置.后 ...

  4. Spring基础系列-Spring事务不生效的问题与循环依赖问题

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9476550.html 一.提出问题 不知道你是否遇到过这样的情况,在ssm框架中开发we ...

  5. Spring基础系列-Web开发

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9996902.html SpringBoot基础系列-web开发 概述 web开发就是集成 ...

  6. Spring基础系列-参数校验

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9953744.html Spring中使用参数校验 概述 ​ JSR 303中提出了Bea ...

  7. Spring基础系列-容器启动流程(1)

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9870339.html 概述 ​ 我说的容器启动流程涉及两种情况,SSM开发模式和Spri ...

  8. 带你学够浪:Go语言基础系列-环境配置和 Hello world

    文章每周持续更新,原创不易,「三连」让更多人看到是对我最大的肯定.可以微信搜索公众号「 后端技术学堂 」第一时间阅读(一般比博客早更新一到两篇) 前面几周陆陆续续写了一些后端技术的文章,包括数据库.微 ...

  9. 使用IDEA的gradle整合spring+ mybatis 采用javaconfig配置

    1.新建一个工程 2.工程目录 3.添加gradle.propertes文件 activeMQVersion=5.7.0 aspectJVersion=1.7.2 commonsLangVersion ...

随机推荐

  1. Simulation of empirical Bayesian methods (using baseball statistics)

    Previously in this series: The beta distribution Empirical Bayes estimation Credible intervals The B ...

  2. 用c++实现高精度加法

    c++实习高精度加法 最近遇到一个c++实现高精度加法的问题,高精度问题往往十复杂但发现其中的规律后发现并没有那么复杂,这里我实现了一个整数的高精度加法,主要需要注意以下几点: 1:将所需输入的数据以 ...

  3. php中的数组遍历的几种方式

    [(重点)数组循环遍历的四种方式]   1.使用for循环遍历数组     conut($arr);用于统计数组元素的个数.     for循环只能用于遍历,纯索引数组!!!!     如果存在关联数 ...

  4. weex入门

    近期要做一个安卓端的原生应用程序.情况是这样的:需求方原先已经实现了网页,是一个工具类应用,大致作用是连接到他们公司生产的硬件,然后通手机与智能硬件通信来对硬件进行一系列控制.不过呢,这个网页先前是由 ...

  5. 演讲小技巧iPhone+Keynote

    原文发布在简书上:http://www.jianshu.com/p/a45538ca611f 今天在公司里分享了一个技术雷达里关于 ECMAScript 2017 的小 Session,分享加问答总共 ...

  6. echarts仪表盘如何设置图例(legend)

    echarts 图表中经常需要对不同的颜色设置图例标识不同的意义,而仪表盘的指针只存在一个值,如何表示不同颜色的意义,官网配置项并未给出该功能: 不同段的颜色是通过axisLine->lineS ...

  7. 可视化之Earth NullSchool

    上两篇我们分别介绍了<Berkeley Earth>和<AQICN>两个网站,今天来看一下Earth NullSchool. 这个网站的特色是风向图,之前有一篇可视化之风向图, ...

  8. 增强for循环用法

    1.首先增强for循环和iterator遍历的效果是一样的,也就说增强for循环的内部也就是调用iteratoer实现的, 但是增强for循环有些缺点,例如不能在增强循环里动态的删除集合内容.不能获取 ...

  9. 【知识整理】这可能是最好的RxJava 2.x 入门教程(二)

    这可能是最好的RxJava 2.x入门教程系列专栏 文章链接: 这可能是最好的RxJava 2.x 入门教程(一) GitHub 代码同步更新:https://github.com/nanchen22 ...

  10. 浅谈js中的正则表达式

    很多时候多会被正则表达式搞的晕头转向,最近抽出时间对正则表达式进行了系统的学习,整理如下: 正则表达式的创建 两种方法,一种是直接写,由包含在斜杠之间的模式组成:另一种是调用RegExp对象的构造函数 ...