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
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配置的更多相关文章
- SpringBoot基础系列-SpringBoot配置
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9990680.html SpringBoot基础系列-SpringBoot配置 概述 属性 ...
- Spring基础系列-AOP源码分析
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9560803.html 一.概述 Spring的两大特性:IOC和AOP. AOP是面向切 ...
- Spring基础系列--AOP织入逻辑跟踪
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9619910.html 其实在之前的源码解读里面,关于织入的部分并没有说清楚,那些前置.后 ...
- Spring基础系列-Spring事务不生效的问题与循环依赖问题
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9476550.html 一.提出问题 不知道你是否遇到过这样的情况,在ssm框架中开发we ...
- Spring基础系列-Web开发
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9996902.html SpringBoot基础系列-web开发 概述 web开发就是集成 ...
- Spring基础系列-参数校验
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9953744.html Spring中使用参数校验 概述 JSR 303中提出了Bea ...
- Spring基础系列-容器启动流程(1)
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9870339.html 概述 我说的容器启动流程涉及两种情况,SSM开发模式和Spri ...
- 带你学够浪:Go语言基础系列-环境配置和 Hello world
文章每周持续更新,原创不易,「三连」让更多人看到是对我最大的肯定.可以微信搜索公众号「 后端技术学堂 」第一时间阅读(一般比博客早更新一到两篇) 前面几周陆陆续续写了一些后端技术的文章,包括数据库.微 ...
- 使用IDEA的gradle整合spring+ mybatis 采用javaconfig配置
1.新建一个工程 2.工程目录 3.添加gradle.propertes文件 activeMQVersion=5.7.0 aspectJVersion=1.7.2 commonsLangVersion ...
随机推荐
- js继承之原型链继承
面向对象编程都会涉及到继承这个概念,JS中实现继承的方式主要是通过原型链的方法. 一.构造函数.原型与实例之间的关系 每创建一个函数,该函数就会自动带有一个 prototype 属性.该属性是个指针, ...
- 读Zepto源码之操作DOM
这篇依然是跟 dom 相关的方法,侧重点是操作 dom 的方法. 读Zepto源码系列文章已经放到了github上,欢迎star: reading-zepto 源码版本 本文阅读的源码为 zepto1 ...
- python中xrange用法分析
本文实例讲述了python中xrange用法.分享给大家供大家参考.具体如下: 先来看如下示例: >>> x=xrange(0,8) >>> print x xra ...
- Visual Studio Code 使用心得
Visual Studio Code 使用心得 最好用的跨平台编辑器,没有之一! 修改编辑器的显示语言 起因:vsCode又升级了(1.13.0),重启之后发现熟悉的中文菜单没有了,而且设置文件 se ...
- Nginx学习笔记1-Nginx功能模块以及进程管理
1. 功能 1.1. 功能描述 使用缓存加速反向代理,简单负载均衡和容错: 使用缓存机制加速远程FastCGI服务器的访问: 模块化结构: 基本的HTTP功能: 邮 ...
- 二阶段项目所遇问题 如何实现php向js传输数据
首先当前页面做了一个双处理的界面,就是有PhP也有JS的处理界面. 上一个传值界面是一个PHP的传值,结果,在当前页面的JS中也要用到上一界面传的值,这时发现,PHP与JS就像是两个互相孤立的小岛,根 ...
- KVM之Live Migration
1.安装KVM必要的软件包 #sudo apt-get install qemu-kvm bridge-utilus 2.制作虚拟机映像ubuntu-12.04.qcow2 $qemu-img cre ...
- BinarySearchTree-二叉搜索树
一.二叉搜索树的定义及性质 二叉查找树(Binary Search Tree),也称有序二叉树(ordered binary tree),排序二叉树(sorted binary tree),是指一棵空 ...
- loadrunner11的移动端性能测试之脚本录制
以前使用LR11录制过一个app的登录操作,这里记录一下 测试准备 硬件要求 1, 负载测试机一台(内存8G以上,cpu 1.5GHZ以上,存储空间20G以上)或两台以上(控制机和多个负载机). 2 ...
- Python:Anaconda安装虚拟环境到指定路径
1 曾经的困扰 有段时间,想使用基于不同python版本的anaconda,就直接从官网下载了两个不同的anaconda版本进行安装.刚开始的时候,还觉得也没啥问题.用了一小段时间,在安装其他的第三方 ...