Spring Bean的生命周期

Spring Bean的生命周期主要指的是单例bean(singleton),对于多例bean,Spring在创建好后就不会管理后续的生命周期。
对于普通的Java对象,它们的生命周期一般就是:

  • 实例化
  • 不再使用时,被垃圾回收器回收

而对于Spring bean来说,大致上它的生命周期:

实例化–>属性赋值–>初始化–>销毁

1、实例化
通过 Java 的反射机制或工厂方法创建一个 Bean 的实例
2、属性赋值
构造函数或者setter方法
3、初始化和销毁方法

以下主要讲初始化和销毁的方法:

方式一: 自定义init-method和destroy-method

具体操作:

public class Car {
    public Car(){
        System.out.println("Car ... Construct.....");
    }
    public void init(){
        System.out.println("Car ... init......");
    }
    public void destroy(){
        System.out.println("Car ... destroy.......");
    }
}

配置类:

@Configuration
@ComponentScan(value = {"com.herd.beanlifecycle"})
public class MyConfig3 {
    @Bean(initMethod = "init",destroyMethod = "destroy")
    public Car car(){
        return new Car();
    }
}

测试方法:

@Test
    public void test01(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig3.class);
        System.out.println("容器创建完成....");
        context.close();
    }

在这里插入图片描述

方式二:让Bean实现InitializingBeanDisposableBean接口,并重写方法

具体操作:

public class Cat implements InitializingBean, DisposableBean {

    public Cat(){
        System.out.println("Cat ... construct......");
    }

    /**
     * 初始化方法
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Cat ... init .....");
    }
    /**
     * 销毁方法
     */
    @Override
    public void destroy() throws Exception {
        System.out.println("Cat ... destroy......");
    }
}

配置类:

@Configuration
public class MyConfig3 {
    /**
     *实现 InitializingBean, DisposableBean接
     */
    @Bean
    public Cat cat(){
        return new Cat();
    }
}

测试类:

@Test
    public void test01(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig3.class);
        System.out.println("容器创建完成....");
        context.close();
    }

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

第三种方式:使用JSR250规范的两个注解

具体操作:

public class Dog {

    public Dog(){
        System.out.println("Dog ... Construct.....");
    }

    @PostConstruct
    public void init(){
        System.out.println("Dog ... init......");
    }

    @PreDestroy
    public void destroy(){
        System.out.println("Dog ... destroy.......");
    }
}

配置类:

@Configuration
public class MyConfig3 {
    /**
     * 使用JSR250规定的方法
     */
    /**
     * @PostConstruct: 在Bean创建完成并属性赋值完成,来执行初始化方法
     * @PreDestroy: 在容器销毁之前,通知清理工作
     */
    @Bean
    public Dog dog(){
        return new Dog();
    }
}

测试类:

@Test
    public void test01(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig3.class);
        System.out.println("容器创建完成....");
        context.close();
    }

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


在初始化方法之前我们也可以做一些操作,这就是后置处理器:实现BeanPostProcessor接口,重写里面的两个方法
postProcessBeforeInitialization()后置处理器 前置操作、postProcessAfterInitialization()后置处理器 后置操作,Spring AOP的实现就是在后置处理器 后置方法 这里实现

具体实现:
编写一个类,并注入到容器中,以便观察后置处理器的执行时机

public class Car {
    public Car(){
        System.out.println("Car ... Construct.....");
    }
    public void init(){
        System.out.println("Car ... init......");
    }
    public void destroy(){
        System.out.println("Car ... destroy.......");
    }
}

编写后置处理器类,实现BeanPostProcessor接口,重写方法

@Component //交给容器管理
public class MyBeanPostProcessor implements BeanPostProcessor {

    /**
     * 后置处理器 前置操作
     * @param bean the new bean instance 新创建的实例
     * @param beanName the name of the bean 实例的名称
     * @return
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization"+bean+"===>"+beanName);
        return bean;
    }
    /**
     * 后置处理器 后置操作  AOP
     * @param bean the new bean instance
     * @param beanName the name of the bean
     * @return
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessAfterInitialization"+bean+"===>"+beanName);
        return bean;
    }
}

配置类:

@Configuration
@ComponentScan(value = {"com.herd.beanlifecycle"})
public class MyConfig3 {

    @Bean(initMethod = "init",destroyMethod = "destroy")
    public Car car(){
        return new Car();
    }

}

测试:

@Test
    public void test01(){
    	//启动容器
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig3.class);
        System.out.println("容器创建完成....");
        
        //关闭容器
        context.close();
    }

在这里插入图片描述
这里我们跟一下源码
关键位置:
在这里插入图片描述
进入前置操作方法:
里面有很多的PostProcessor
在这里插入图片描述
循环找自己写的PostProcessor
在这里插入图片描述
然后跳出前置操作,进入初始化方法,在下图我们可以看出,执行了init方法
在这里插入图片描述
后置方法不再赘述。
总之,Spring Bean的生命周期:

在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值