spring(二):bean的生命周期
bean的生命周期指的是bean的创建——>初始化——>销毁的过程,该过程是由spring容器进行管理的
我们可以自定义bean初始化和销毁的方法:容器在bean进行到当前生命周期时,调用自定义的初始化和销毁方法
自定义初始化和销毁方法
init-method、destroy-method
配置文件的bean标签中配置init-method、destroy-method属性
<bean id="user" class="com.enjoy.study.pojo.User" init-method="init" destroy-method="destroy"/>
bean类
public class User {
public User() {
System.out.println("user....constructor..........");
} public void init(){
System.out.println("user....init..........");
} public void destroy(){
System.out.println("user....destroy..........");
}
}
测试类
public class Cap7Test {
@Test
public void cap7Test(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:beans.xml");
System.out.println("ioc容器启动完成");
context.close();
}
}
结果
user....constructor..........
user....init.......... ioc容器启动完成
user....destroy..........
initMethod、destroyMethod
@Bean配置initMethod、destroyMethod属性
单实例情况:
@Configuration
public class Cap7MainConfig {
@Bean(initMethod = "init",destroyMethod = "destroy")
public User user(){
return new User();
}
}
测试方法
@Test
public void annoTest(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Cap7MainConfig.class);
System.out.println("ioc容器启动完成");
context.close();
}
结果
user....constructor..........
user....init.......... ioc容器启动完成
user....destroy..........
多实例情况:
@Configuration
public class Cap7MainConfig {
@Scope("prototype")
@Bean(initMethod = "init",destroyMethod = "destroy")
public User user(){
return new User();
}
}
结果
ioc容器启动完成
单实例情况下,容器启动时完成bean的实例化,容器关闭时进行bean的销毁
多实例情况下,容器启动时不进行bean的实例化,获取bean时实例化bean对象,所以启动后直接关闭容器,容器中并未进行bean的管理
InitializingBean、DisposableBean接口
bean实现InitializingBean接口,实现afterPropertiesSet()方法:beanFactory创建对象完成,并且设置完属性后,调用该方法,相当于初始化方法
bean实现DisposableBean接口,实现destroy()方法:bean对象销毁时,调用该方法
public class User implements InitializingBean,DisposableBean {
public User() {
System.out.println("user....constructor..........");
} @Override
public void afterPropertiesSet() throws Exception {
System.out.println("user....afterPropertiesSet.........");
} @Override
public void destroy() throws Exception {
System.out.println("user....destroy............");
}
}
配置类
@Configuration
public class Cap7MainConfig {
@Bean
public User user(){
return new User();
}
}
结果
user....constructor..........
user....afterPropertiesSet.........
ioc容器启动完成
user....destroy............
@PostConstruct、@PreDestroy
@Component
public class User{
public User() {
System.out.println("user....constructor..........");
} //对象创建并赋值后使用
@PostConstruct
public void init() {
System.out.println("user....init.........");
} //容器移除对象之前回调通知,销毁bean
@PreDestroy
public void destroy() {
System.out.println("user....destroy............");
}
}
配置类
@Configuration
@ComponentScan("com.enjoy.study.cap7")
public class Cap7MainConfig {
}
结果
user....constructor..........
user....init......... ioc容器启动完成
user....destroy............
BeanPostProcessor
后置处理器,负责在初始化前后做相应处理
/**
* 后置处理器
* @Component,将后置处理器加入容器中
* @author qf
* @create 2019-05-22 10:05
*/
@Component
public class MyBeanPostProcessor implements BeanPostProcessor { /**
* 在初始化之前进行相应后置处理工作。比如在init-method方法执行之前,或者initializingBean的afterPropertiesSet方法之前
* @param bean
* @param beanName
* @return
* @throws BeansException
*/
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization......"+bean+"........."+beanName);
return bean;
} /**
* 在初始化之后进行相应后置处理工作
* @param bean
* @param beanName
* @return
* @throws BeansException
*/
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization......"+bean+"........."+beanName);
return bean;
}
}
测试
@Component
public class User implements InitializingBean,DisposableBean { @Override
public void destroy() throws Exception {
System.out.println("user....destroy........");
} @Override
public void afterPropertiesSet() throws Exception {
System.out.println("user....afterPropertiesSet........");
}
}
配置类
@Configuration
@ComponentScan("com.enjoy.study.cap8")
public class Cap8MainConfig {
}
测试类
public class Cap8Test {
@Test
public void cap8Test(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Cap8MainConfig.class);
System.out.println("ioc容器创建完成");
context.close();
}
}
结果
postProcessBeforeInitialization...............org.springframework.context.event.internalEventListenerProcessor
postProcessAfterInitialization...............org.springframework.context.event.internalEventListenerProcessor
postProcessBeforeInitialization...............org.springframework.context.event.internalEventListenerFactory
postProcessAfterInitialization...............org.springframework.context.event.internalEventListenerFactory
postProcessBeforeInitialization...............cap8MainConfig
postProcessAfterInitialization...............cap8MainConfig
postProcessBeforeInitialization...............user
user....afterPropertiesSet........
postProcessAfterInitialization...............user ioc容器创建完成 user....destroy........
spring(二):bean的生命周期的更多相关文章
- 一次性讲清楚spring中bean的生命周期之二:FactoryBean的前世今生
前言 在<spring中FactoryBean是什么bean>一文中,带着小伙伴学习了spring中的FactoryBean,了解了到了FactoryBean其实是一种生产Bean的bea ...
- 深究Spring中Bean的生命周期
前言 这其实是一道面试题,是我在面试百度的时候被问到的,当时没有答出来(因为自己真的很菜),后来在网上寻找答案,看到也是一头雾水,直到看到了<Spring in action>这本书,书上 ...
- 如果你每次面试前都要去背一篇Spring中Bean的生命周期,请看完这篇文章
前言 当你准备去复习Spring中Bean的生命周期的时候,这个时候你开始上网找资料,很大概率会看到下面这张图: 先不论这张图上是否全面,但是就说这张图吧,你是不是背了又忘,忘了又背? 究其原因在于, ...
- 一次性讲清楚spring中bean的生命周期之三:bean是如何实例化的
在前面的两篇博文<一次性讲清楚spring中bean的生命周期之一:getSingleton方法>和<一次性讲清楚spring中bean的生命周期之二:FactoryBean的前世今 ...
- JAVA面试题:Spring中bean的生命周期
Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一 ...
- 深入理解Spring中bean的生命周期
[Spring中bean的生命周期] bean的生命周期 1.以ApplocationContext上下文单例模式装配bean为例,深入探讨bean的生命周期: (1).生命周期图: (2).具体事例 ...
- Spring中Bean的生命周期及其扩展点
原创作品,可以转载,但是请标注出处地址http://www.cnblogs.com/V1haoge/p/6106456.html Spring中Bean的管理是其最基本的功能,根据下面的图来了解Spr ...
- 简:Spring中Bean的生命周期及代码示例
(重要:spring bean的生命周期. spring的bean周期,装配.看过spring 源码吗?(把容器启动过程说了一遍,xml解析,bean装载,bean缓存等)) 完整的生命周期概述(牢记 ...
- 面试Spring之bean的生命周期
找工作的时候有些人会被问道Spring中Bean的生命周期,其实也就是考察一下对Spring是否熟悉,工作中很少用到其中的内容,那我们简单看一下. 在说明前可以思考一下Servlet的生命周期:实例化 ...
- 通过BeanPostProcessor理解Spring中Bean的生命周期
通过BeanPostProcessor理解Spring中Bean的生命周期及AOP原理 Spring源码解析(十一)Spring扩展接口InstantiationAwareBeanPostProces ...
随机推荐
- hdu4348 To the moon (可持久化线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4348 题目大意:给定含有n个数的序列,有以下四种操作 1.C l r d:表示对区间[l,r]中的数加 ...
- 使用GDB调试时attach ID不被允许
在进入gdb后,直接使用attach ID,出现下面的情况: Could not attach to process. If your uid matches the uid of the targ ...
- Java数组重修,猜数小游戏改进和打印正三角形
数组重修,猜数小游戏 要求:从键盘输入一个数,判断数组是否包含此数,运用随机数 我们可能会这样写 import java.util.Random; import java.util.Scanner; ...
- frugally-deep: Header-only library for using Keras models in C++
// Convenience wrapper around predict for models with // single tensor outputs of shape (1, 1, 1), / ...
- 032:DTL常用过滤器(1)
为什么需要过滤器: 在DTL中,不支持函数的调用形式‘()’,因此不能给函数传递参数,这将有很大的局限性:而过滤器其实就是一个函数,可以对需要处理的参数进行处理,并且还可以额外接受一个参数(也就是说: ...
- Test 6.29 T4 简单数据结构练习
问题描述 费了一番功夫,神犇 CJK 终于完成了前三道题目."不错,不愧是新一代神犇啊!" JesseLiu 满意地说道,"不过,你在算法方面的功底固然不错.对于数据结构 ...
- Netty模型
- 持续优化云原生体验,阿里云在Serverless容器与多云上的探索
近日,阿里云宣布推出Serverless Kubernetes服务此举意在降低容器技术的使用门槛.简化容器平台运维.并同时发布阿里云服务对Open Service Broker API标准支持,通过一 ...
- (转)pd.read_csv之OSError: Initializing from file failed的解决方案
转:https://blog.csdn.net/funnyPython/article/details/78532102 rides = pd.read_csv(data_path)1 # OSErr ...
- 2017工业软件top100