3spring:生命周期,属性赋值,自动装配
有不懂的可以参考之前的文章!
https://www.cnblogs.com/Mrchengs/p/10109053.html
1.Bean的生命周期
public class Chirld {
public Chirld() {
System.out.println("创建Child实例....");
}
public void init(){
System.out.println("初始化方法.....");
}
public void destroy(){
System.out.println("销毁方法....");
}
}
@Configuration
public class TheLifeOfInitAnfDestroy {
@Bean(initMethod="init",destroyMethod="destroy")
public Chirld car(){
return new Chirld();
}
}
@Test
public void test4(){
AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(TheLifeOfInitAnfDestroy.class); System.out.println("容器创建完成");
//关闭
app.close();
}
创建Child实例....
初始化方法.....
容器创建完成
十一月 , :: 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@512ddf17: startup date [Fri Nov :: CST ]; root of context hierarchy
销毁方法....
在容器关闭的时候进行销毁
2.接口方法
public interface InitializingBean {
void afterPropertiesSet() throws Exception;
}
public interface DisposableBean {
void destroy() throws Exception;
}
@Component
public class Chirld2 implements InitializingBean,DisposableBean { public Chirld2() {
System.out.println("创建Child2实例....");
}
public void afterPropertiesSet() throws Exception {
System.out.println("init....."); }
public void destroy() throws Exception {
System.out.println("destroy....");
}
}
@Configuration
@ComponentScan("coom.MrChengs.bean")
public class TheLifeOfInitAnfDestroy { }
@Test
public void test4(){
AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(TheLifeOfInitAnfDestroy.class); System.out.println("容器创建完成");
//关闭
app.close();
}
创建Child2实例....
init.....
容器创建完成
十一月 , :: 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@512ddf17:
startup date [Fri Nov :: CST ]; root of context hierarchy
destroy....
//代码.....
4.BeanPostProcessor接口:bean的后置处理器
@Configuration
@ComponentScan("coom.MrChengs.bean")
public class TheLifeOfInitAnfDestroy {
}
@Test
public void test4(){
AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(TheLifeOfInitAnfDestroy.class);
System.out.println("容器创建完成");
//关闭
app.close();
}
post init.....
aftet init ....
post init.....
aftet init ....
post init.....
aftet init ....
创建Child2实例....
post init.....
init.....
aftet init ....
容器创建完成
十一月 , :: 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@512ddf17: startup date [Fri Nov :: CST ]; root of context hierarchy
destroy....
2.属性赋值:
person.properties
person.school=MrChengs
public class Person {
@Value("MrChengs")
private String name;
@Value("#{20-12}")
private int age;
@Value("${person.school}")
private String school;
...
}
@Configuration
//引入资源
@PropertySource(value={"classpath:/person.properties"})
public class ValueConfig {
@Bean
public Person person(){
return new Person();
}
}
@Test
public void test5(){
AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(ValueConfig.class); String [] names = app.getBeanDefinitionNames();
for(String name : names){
System.out.println(name);
}
Person person = app.getBean(Person.class);
System.out.println(person); }
valueConfig
person
Person [name=MrChengs, age=8, school=MrChengs]
public @interface PropertySource {
String name() default "";
String[] value();
boolean ignoreResourceNotFound() default false;
String encoding() default "";
Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;
}
自动装配:
@Autowired
applicationContext.getBean("customerDao")...
@Autowired
private CustomerDao customerDao;
默认按照方法名去id中查找
@Qualifier("customerDao")
@Autowired
private CustomerDao customerDao2;
@Qualifier("customerDao")
@Autowired(required=false)
private CustomerDao customerDao2;
6.@Primary:让spring进行装配的时候,默认使用首选的bean
@Autowired(required=false)
private CustomerDao customerDao; @Primary
@Bean("customerDao2")
public CustomerDao customer(){
...
}
@Repository
public class CustomerDao {
}
@Service
public class CustomerService {
@Autowired
private CustomerDao customerDao; public void print(){
System.out.println("---->"+customerDao);
}
}
@Configuration
@ComponentScan({"coom.MrChengs.config.service","coom.MrChengs.config.dao"})
public class AutowiredConfig {
@Bean("customerDao2")
public CustomerDao customer(){
return new CustomerDao("customer2");
} }
@Test
public void test6(){
AnnotationConfigApplicationContext app = new
AnnotationConfigApplicationContext(coom.MrChengs.config.AutowiredConfig.class); String [] names = app.getBeanDefinitionNames();
for(String name : names){
System.out.println(name);
}
CustomerService cus = app.getBean(CustomerService.class);
cus.print();
//System.out.println(cus);
}
autowiredConfig
customerService 此时容器中有两个相同类型的bean
customerDao
customerDao2 此时注入的类型是:
---->CustomerDao [name=null]
@Resource
private CustomerDao customerDao;
可以修改器默认装配的名称
@Resource(name="customerDao2")
private CustomerDao customerDao;
不可以支持@Primary功能和request=false的功能
<!-- https://mvnrepository.com/artifact/javax.inject/javax.inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
@Inject
private CustomerDao customerDao;
@Autowire
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD,
ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
可以标注的位置有构造器,方法,属性
方法上
@Component
public class Person {
...
}
@Component
public class Students {
...
@Autowired
public void setPerson(Person person) {
this.person = person;
}
..
}
AnnotationConfigApplicationContext app = new
AnnotationConfigApplicationContext(coom.MrChengs.config.AutowiredConfig.class); Students s = app.getBean(Students.class); System.out.println(s);
Person p = app.getBean(Person.class);
System.out.println(p);
Students [person=Person [name=MrChengs, age=, school=${person.school}], name=null]
Person [name=MrChengs, age=, school=${person.school}]
@Autowired
public Students(Person person, String name) {
super();
this.person = person;
this.name = name;
System.out.println("Students.....");
}
3spring:生命周期,属性赋值,自动装配的更多相关文章
- Spring笔记(2) - 生命周期/属性赋值/自动装配及部分源码解析
一.生命周期 @Bean自定义初始化和销毁方法 //====xml方式: init-method和destroy-method==== <bean id="person" c ...
- Spring Bean 注入 1 - 构造方法注入,属性注入,自动装配
1.代码结构图 xxx 2.bean代码 package com.xxx.bean; /** * Created with IntelliJ IDEA. * User: zhenwei.liu * D ...
- 从EFCore上下文的使用到深入剖析DI的生命周期最后实现自动属性注入
故事背景 最近在把自己的一个老项目从Framework迁移到.Net Core 3.0,数据访问这块选择的是EFCore+Mysql.使用EF的话不可避免要和DbContext打交道,在Core中的常 ...
- Spring 框架基础(02):Bean的生命周期,作用域,装配总结
本文源码:GitHub·点这里 || GitEE·点这里 一.装配方式 Bean的概念:Spring框架管理的应用程序中,由Spring容器负责创建,装配,设置属性,进而管理整个生命周期的对象,称为B ...
- Spring - bean的autowire属性(自动装配)
当我们要往一个bean的某个属性里注入另外一个bean,我们会使用<property> + <ref/>标签的形式.但是对于大型项目,假设有一个bean A被多个bean引用注 ...
- spring4笔记----spring生命周期属性
init-method : 指定bean的初始化方法-spring容器会在bean的依赖关系注入完成后调用该方法 destroy-method :指定bean销毁之前的方法-spring容器将会在销毁 ...
- 浅尝Spring注解开发_自定义注册组件、属性赋值、自动装配
Spring注解开发 浅尝Spring注解开发,基于Spring 4.3.12 包含自定义扫描组件.自定义导入组件.手动注册组件.自动注入方法和参数.使用Spring容器底层组件等 配置 @Confi ...
- bean的autowire属性及其生命周期
一:sutowire属性 1.no:默认值,禁用自动装配: 2.byName:根据属性名称自动装配: 3.byType:根据属性类型自动装配: 4.constructor:通过构造方法自动装配,不推荐 ...
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
随机推荐
- Spring - 几种RPC模型的使用与比较
Spring中,用JMS搞RPC时会用到: org.springframework.jms.remoting.JmsInvokerServiceExporter org.springframework ...
- 六 Selector
选择器是java NIO中能够检测一到多个NIO通道(Channel),并能知晓是否为诸如读写时间做好准备的组件.这样,一个单独的线程可以管理多个channel,从而管理多个网络连接 为什么用Sele ...
- Linux 更新python至2.7后ImportError: No module named _ssl
原文:http://blog.51cto.com/hunt1574/1630961 编译安装python 2.7后无法导入ssl包 解决办法: 1 下载地址:http://www.openssl.or ...
- nexus3安装
windows环境 1.下载nexus3 2.以管理员的方式打开命令行,建议打开服务管理界面,将这个服务设置为手动,步骤是:右键我的电脑-->服务-->nexus,然后点击设置手动启动 3 ...
- Redis(什么是Redis?)
Redis是一个开源的内存数据库,可以作为缓存也可以作为消息队列.它支持的数据结构有:字符串.哈希表.列表.集合.有序集合. Redis:Redis是Remote Dictionary Server( ...
- nginx关于 error_page指令详解.md
error_page指令解释 nginx指令error_page的作用是当发生错误的时候能够显示一个预定义的uri,比如: error_page 502 503 /50x.html; 这样实际上产生了 ...
- javaweb servlet jsp简单笔记
第二章: 1: web 俗称 : 万维网 www 2: web开发 的三大核心: HTML(网页) ,URL(定位),HTTP:(协议) 页面的分类: 静态页面: html+css 动态页面:jsp ...
- MDM-Object.fn 一些实践与理解
Object.assign() 用于将所有可枚举属性的值从一个或多个源对象复制到目标对象.它将返回目标对象.如果目标对象中的属性具有相同的键,则属性将被源中的属性覆盖.后来的源的属性将类似地覆盖早先的 ...
- Java jsp 自定义标签
1 自定义标签 1.1 引入 需求: 向浏览器输出当前客户的IP地址 (只能使用jsp标签) 1.2 第一个自定义标签开发步骤 1)编写一个普通的java类,继承SimpleTagSupport类,叫 ...
- 定期重启SSRS 服务
SSRS 在执行了一段时间之后会变得非常卡,遇到好几次内存暴涨,CPU100%的情况. 但是在查询了一通以后发现,这个时间没有人在运行报告,不知道是哪里有问题,没有回收... 所以决定定期在晚上没有不 ...