Spring之Bean的生命周期(持续更新)

Bean的生命周期

1、什么是Bean的生命周期

Bean从创建—>初始化—>销毁的过程

2、自定义初始化方法和销毁方法

我们可以自定义初始化和销毁方法;容器在Bean进行到当前周期的时候来调用我们自定义的初始化方法和销毁方法
初始化和销毁方法有四种方式,我们如何来定义呢?

  1. 来指定初始化和销毁方法—>指定init-method和destroy-method方法

我们先来写一个Car类

package com.markus.bean;
/**
 * Author:markusZhang
 * degree of proficiency:
 * Date:Create in 2020/6/1 22:55
 */
public class Car {
    public Car(){
        System.out.println("car ... construct");
    }
    public void init(){
        System.out.println("init()...");
    }
    public void destroy(){
        System.out.println("destroy()...");
    }
}

将这个Bean注册到容器当中

package com.markus.config;
import com.markus.bean.Car;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * Author:markusZhang
 * degree of proficiency:
 * Date:Create in 2020/6/1 22:43
 */
@Configuration
public class SpringConfigOfLifeCycle {
	//利用Bean的initMethod和destroyMethod来指定创建Bean后的初始化方法和销毁方法
    @Bean(initMethod = "init",destroyMethod = "destroy")
    public Car car(){
        return new Car();
    }
}

测试类:

package com.markus.test;
import com.markus.config.SpringConfigOfLifeCycle;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
 * Author:markusZhang
 * degree of proficiency:
 * Date:Create in 2020/6/1 22:58
 */
public class IOCTestBeanLife {
    @Test
    public void test(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfigOfLifeCycle.class);
        System.out.println("容器启动");
        applicationContext.close();//关闭容器,单实例Bean销毁
    }
}

测试结果:
在这里插入图片描述
初始化时机:对象创建完成,并且赋值好,调用初始化方法
销毁时机:容器关闭的时候进行销毁
但是一定要注意Bean的作用域是单实例Bean还是多实例Bean
如果在单实例Bean的情况下,初始化和销毁时机都是正常的,而当Bean是多实例Bean的时候,只有在获取该对象的时候才会调用初始化方法,并且容器关闭的话,也不会调用销毁方法
2. 通过实现InitializingBean(定义初始化逻辑)、实现DisposableBean(定义销毁逻辑)
代码实现:

package com.markus.bean;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

/**
 * Author:markusZhang
 * degree of proficiency:
 * Date:Create in 2020/6/2 22:06
 */
@Component
public class Cat implements InitializingBean, DisposableBean {
    public Cat(){
        System.out.println("cat ... constructor...");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("容器已关闭...destroy");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("对象已被创建...afterPropertiesSet...");
    }
}
  1. 第三种方式就是通过jsr250规范,有两个注解@PostConstruct和@PreDestroy,第一个注解表示:在调用完构造器之后,进行操作;第二个注解表示:在容器销毁Bean之前通知清理工作
    代码类:
package com.markus.bean;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * Author:markusZhang
 * degree of proficiency:
 * Date:Create in 2020/6/2 22:17
 */
@Component
public class Dog {
    public Dog(){
        System.out.println("dog ... construct...");
    }
    @PostConstruct
    public void postConstruct(){
        System.out.println("postConstruct...");
    }

    @PreDestroy
    public void preDestroy(){
        System.out.println("preDestroy...");
    }
}
  1. 还有一种方法就是实现spring的BeanPostProcessor的接口,重写它的两个方法,它的作用是在Bean初始化前后做些工作
    实现类
package com.markus.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
**
 * Author:markusZhang
 * degree of proficiency:
 * Date:Create in 2020/6/2 22:33
 */
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization==>"+beanName+"-->"+bean);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessAfterInitialization==>"+beanName+"-->"+bean);
        return bean;
    }
}

测试结果:
在这里插入图片描述

3、接着BeanPostProcessor,我们来梳理下它的原理

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值