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的生命周期的更多相关文章

  1. 一次性讲清楚spring中bean的生命周期之二:FactoryBean的前世今生

    前言 在<spring中FactoryBean是什么bean>一文中,带着小伙伴学习了spring中的FactoryBean,了解了到了FactoryBean其实是一种生产Bean的bea ...

  2. 深究Spring中Bean的生命周期

    前言 这其实是一道面试题,是我在面试百度的时候被问到的,当时没有答出来(因为自己真的很菜),后来在网上寻找答案,看到也是一头雾水,直到看到了<Spring in action>这本书,书上 ...

  3. 如果你每次面试前都要去背一篇Spring中Bean的生命周期,请看完这篇文章

    前言 当你准备去复习Spring中Bean的生命周期的时候,这个时候你开始上网找资料,很大概率会看到下面这张图: 先不论这张图上是否全面,但是就说这张图吧,你是不是背了又忘,忘了又背? 究其原因在于, ...

  4. 一次性讲清楚spring中bean的生命周期之三:bean是如何实例化的

    在前面的两篇博文<一次性讲清楚spring中bean的生命周期之一:getSingleton方法>和<一次性讲清楚spring中bean的生命周期之二:FactoryBean的前世今 ...

  5. JAVA面试题:Spring中bean的生命周期

    Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一 ...

  6. 深入理解Spring中bean的生命周期

    [Spring中bean的生命周期] bean的生命周期 1.以ApplocationContext上下文单例模式装配bean为例,深入探讨bean的生命周期: (1).生命周期图: (2).具体事例 ...

  7. Spring中Bean的生命周期及其扩展点

    原创作品,可以转载,但是请标注出处地址http://www.cnblogs.com/V1haoge/p/6106456.html Spring中Bean的管理是其最基本的功能,根据下面的图来了解Spr ...

  8. 简:Spring中Bean的生命周期及代码示例

    (重要:spring bean的生命周期. spring的bean周期,装配.看过spring 源码吗?(把容器启动过程说了一遍,xml解析,bean装载,bean缓存等)) 完整的生命周期概述(牢记 ...

  9. 面试Spring之bean的生命周期

    找工作的时候有些人会被问道Spring中Bean的生命周期,其实也就是考察一下对Spring是否熟悉,工作中很少用到其中的内容,那我们简单看一下. 在说明前可以思考一下Servlet的生命周期:实例化 ...

  10. 通过BeanPostProcessor理解Spring中Bean的生命周期

    通过BeanPostProcessor理解Spring中Bean的生命周期及AOP原理 Spring源码解析(十一)Spring扩展接口InstantiationAwareBeanPostProces ...

随机推荐

  1. LA 3263 That Nice Euler Circuit(欧拉定理)

    That Nice Euler Circuit Little Joey invented a scrabble machine that he called Euler, after the grea ...

  2. robot framework 自动化框架环境搭建

    win10 64位系统 1.安装python2.7.15 在官网https://www.python.org/downloads/下载对应版本 在同一台电脑上同时安装Python2和Python3参考 ...

  3. Mac xlwings aem.aemsend.EventError: Command failed: The user has declined permission. (-1743)

    aem.aemsend.EventError: Command failed: The user has declined permission. (-1743) 关于mac pycharm 使用xl ...

  4. LYXF-PE-tools

    先随便说一下这个PE-tools有什么用? 我开发这款PE-tools是为了学习而开发的,且是开源的,这里我会提供源码链接.它可以解析windows 32/64位程序中比较常用的一些属性. 里面有个稍 ...

  5. Test 6.24 T3 水题

    问题描述 秋之国首都下了一场暴雨,由于秋之国地势高低起伏,不少地方出现了积水. 秋之国的首都可以看成一个 n 行 m 列的矩阵,第 i 行第 j 列的位置高度为 ai,j,首都以外的地方的高度可以都看 ...

  6. CodeChef Max-digit Tree(动态规划)

    传送门. 题解: 最主要的问题是如何判断一个数是否合法,这就需要发现性质了. 这个状态划分还是不太容易想到, 每次加的数\(∈[0,k)\),也就是个位一直在变变变,更高的位每次都是加一,这启发我们状 ...

  7. redis学习 --Sorted Set

    一.概述: Sorted Set(有序集合)和Set类型极为相似,它们都是字符串的集合,都不允许重复的成员出现在一个Set中.它们之间的主要差别是Sorted Set中的每一个成员都会有一个分数(sc ...

  8. Python3解leetcode Count Binary Substrings

    问题描述: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...

  9. 彻底关闭Postprocess

    即使场景中没有postprocess volume,场景中也会有默认的postprocess volume效果,如果需要彻底关闭postprocess, 可以使用命令: sg.PostProcessQ ...

  10. 如何实现全屏遮罩(附Vue.extend和el-message源码学习)

    [Vue]如何实现全屏遮罩(附Vue.extend和el-message源码学习) 在做个人项目的时候需要做一个类似于电子相册浏览的控件,实现过程中首先要实现全局遮罩,结合自己的思路并阅读了(饿了么) ...