Spring中Bean的生命周期自定义销毁和初始化方法实现详解

生命周期

所谓的生命周期就是bean从创建到初始化到销毁的一个过程,而spring中bean的生命周期都是交给容器进行管理的。因此我们可以自动以初始化和销毁方法,容器在bean进行到当前生命周期的时候来调用我们自定义的初始化和销毁方法,下面就演示下如何自定义初始化方法和销毁方法的几种方式。

  1. 通过@Bean是定destroyMethod和initMethod,这种方式相当于在之前的配置文件中编写init-method和destroy-method的方法,具体实例如下:
public class Car {
    public Car() {
        System.out.println("car construction...");
    }
    public void destory(){
        System.out.println("car destory...");
    }
    public void init(){
        System.out.println("car init...");
    }
}
//通过配置类的形式将当前的bean注入到容器中,并且指定destroyMethod 和initMethod 。
@Configuration
public class MyConfig3 {
    @Bean(destroyMethod = "destory",initMethod = "init")
    public Car car(){
        return new Car();
    }
}

//测试方法
public class App {
    public static void main(String[] args) {
        //读取注解
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig3.class);
        System.out.println("Spring 容器加载完成....");
        ((AnnotationConfigApplicationContext) applicationContext).close();
    }
}

输出的结果为:
输出结果
从以上输出内容可以看出,初始化方法是在对象创建完成并赋值好,才会调用初始化方法。而销毁方法是在容器关闭时。但是如果定义 @Scope("prototype"),定义bean为多实例时,是在获取bean的时候才会调用初始化方法,但是不处理bean的销毁方式,输出结果如下:
多实例bean调用初始化方式的结果图
2. 通过InitializingBean定义初始化逻辑, DisposableBean定义销毁逻辑

//定义一个对象实现 InitializingBean, DisposableBean并且重新里面的destroy和afterPropertiesSet方法
@Component
public class Tree implements InitializingBean, DisposableBean {
    public Tree() {
        System.out.println("Tree construction .....");
    }
    @Override
    public void destroy() throws Exception {
        System.out.println("Tree InitializingBean destroy.....");
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Tree InitializingBean afterPropertiesSet.....");
    }
}
//扫描对象所在的包
@ComponentScan("Spring.bean")
@Configuration
public class MyConfig3 {
    @Bean(destroyMethod = "destory",initMethod = "init")
    public Car car(){
        return new Car();
    }
}
//测试方法
public class App {
    public static void main(String[] args) {
        //读取注解
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig3.class);
        System.out.println("Spring 容器加载完成....");
        ((AnnotationConfigApplicationContext) applicationContext).close();
    }
}

这里可以看到输出的结果为:
InitializingBean, DisposableBean输出结果
3. 使用JSR250规范中的@PostConstruct定义初始化方法,该注解是在bean创建完成并且属性赋值完成,来执行这个初始化方法。@PreDestroy定义销毁前的方法。

//定义一个bean对象在其中的方法是添加@PostConstruct、@PreDestroy注解
@Component
public class Cat {
    public Cat() {
        System.out.println("Cat construction...");
    }
    
    @PreDestroy
    public void destory(){
        System.out.println("Cat destory...");
    }

    @PostConstruct
    public void init(){
        System.out.println("Cat init...");
    }
}
//配置类
@ComponentScan("Spring.bean")
@Configuration
public class MyConfig3 {
    @Bean(destroyMethod = "destory",initMethod = "init")
    public Car car(){
        return new Car();
    }

}
//测试类
public class App {
    public static void main(String[] args) {
        //读取注解
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig3.class);
        System.out.println("Spring 容器加载完成....");
        ((AnnotationConfigApplicationContext) applicationContext).close();
    }
}

输出结果为:
JSR250输出结果
4. BeanPostProcessor(bean的后置处理器)
postProcessBeforeInitialization:在初始化之前
postProcessAfterInitialization:在初始化方法调用之后

//需要先定义一个后置处理器 实现postProcessAfterInitialization、postProcessBeforeInitialization方法
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("BeanPostProcessor postProcessAfterInitialization....."+bean.getClass());
        return bean;
    }
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("BeanPostProcessor postProcessBeforeInitialization....."+bean.getClass());
        return bean;
    }
}

//配置类
@ComponentScan("Spring.bean")
@Configuration
public class MyConfig3 {

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

}
//测试方法
  public static void main(String[] args) {
        //读取注解
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig3.class);
        System.out.println("Spring 容器加载完成....");
        ((AnnotationConfigApplicationContext) applicationContext).close();
    }

输出结果如下:
BeanPostProcessor运行结果图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值