Bean生命周期及增强

Bean生命周期

bean生命周期主要是4个阶段:创建、注入、初始化、销毁。

通过 BeanPostProcessor等处理器扩展功能

本例中用到的处理器有:

  1. postProcessBeforeDestruction
  2. postProcessBeforeInstantiation、postProcessAfterInstantiation
  3. postProcessProperties
package com.example.demo.springTest.a03;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("com.example.demo.springTest.a03")
@Slf4j
public class A03Application {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(A03Application.class, args);

        String marker = "mmmmmmmm";
        log.info(marker +"01 已启动");
        LifeCycleBean bean = run.getBean(LifeCycleBean.class);
        log.info(marker +"02 获取lifecycleBean "+bean);
        bean.destroy2();
        log.info(marker +"03 结束");
        run.close();

    }
}
package com.example.demo.springTest.a03;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

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

@Slf4j
@Component
public class LifeCycleBean {

    public LifeCycleBean(){
        log.info("========"+"LifeCycleBean 构造");
    }


    @Autowired
    public void autowire(@Value("${JAVA_HOME}") String home){
        log.info("========"+"LifeCycleBean 装配" +home);
    }

    @PostConstruct
    public void init2(){
        log.info("========"+"LifeCycleBean 初始化");
    }

    @PreDestroy
    public void destroy2(){
        log.info("========"+"LifeCycleBean 销毁");
    }
}

package com.example.demo.springTest.a03;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class MyBeanPostProcessor implements InstantiationAwareBeanPostProcessor, DestructionAwareBeanPostProcessor {
    @Override
    public void postProcessBeforeDestruction(Object o, String s) throws BeansException {
        log.info("<<<<<<<< "+s+" : 销毁前执行");
        if (s.equals("lifeCycleBean")){
            log.info("<<<<<<<< 2销毁前执行,如@PreDestroy");
        }
    }

    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        if (beanName.equals("lifeCycleBean")){
            log.info("22222222"+"创建_前");
        }
        return null;
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        if (beanName.equals("lifeCycleBean")){
            log.info("33333333"+"创建_后");
        }
        return true;
    }

    @Override
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
        if (beanName.equals("lifeCycleBean")){
            log.info("44444444"+"依赖注入阶段。eg:@Autowired @Value @Resource");
        }
        return null;
    }
}

输出验证

##构造前、后,有增强的处理postProcessBeforeInstantiation、postProcessAfterInstantiation
2023-06-26 23:41:51.776 [main] INFO  [com.example.demo.springTest.a03.MyBeanPostProcessor:24] - 22222222创建_前
2023-06-26 23:41:51.776 [main] INFO  [com.example.demo.springTest.a03.LifeCycleBean:17] - ========LifeCycleBean 构造
2023-06-26 23:41:51.776 [main] INFO  [com.example.demo.springTest.a03.MyBeanPostProcessor:32] - 33333333创建_后

##注入前,有增强的处理 postProcessProperties
2023-06-26 23:41:51.776 [main] INFO  [com.example.demo.springTest.a03.MyBeanPostProcessor:40] - 44444444依赖注入阶段。eg:@Autowired @Value @Resource
2023-06-26 23:41:51.776 [main] INFO  [com.example.demo.springTest.a03.LifeCycleBean:23] - ========LifeCycleBean 装配C:\Java\jdk1.8.0_341
2023-06-26 23:41:51.776 [main] INFO  [com.example.demo.springTest.a03.LifeCycleBean:28] - ========LifeCycleBean 初始化

##销毁前,有增强的处理 postProcessBeforeDestruction
2023-06-26 23:41:54.755 [main] INFO  [com.example.demo.springTest.a03.MyBeanPostProcessor:17] - <<<<<<<< 2销毁前执行,如@PreDestroy
2023-06-26 23:41:54.755 [main] INFO  [com.example.demo.springTest.a03.LifeCycleBean:33] - ========LifeCycleBean 销毁
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值