当前位置: 首页 > news >正文

Bean的生命周期(二)

       Bean的生命周期主要围绕对象的创建、依赖注入、对象的初始化、对象的销毁展开的。除了这几个主要的阶段,其它的都是围绕着这四个阶段前后通过Bean后处理器作拓展。我们可以自定义一个Bean的后处理器进行调试。

1.自定义Bean后处理器

       通过实现下面两个接口并重写里面的方法,便可以对容器里面的Bean作拓展,为测试方便,只对我们自己写的类作拓展。

InstantiationAwareBeanPostProcessor, DestructionAwareBeanPostProcessor
package com.example.demo2;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.lang.Nullable;
import org.springframework.stereotype.Component;/*** @author zhou* @version 1.0* @description TODO* @date 2025/9/30 21:06*/
@Component
public class MyBeanPostProcessor implements InstantiationAwareBeanPostProcessor, DestructionAwareBeanPostProcessor {private static final Logger log = LoggerFactory.getLogger(MyBeanPostProcessor.class);@Overridepublic Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {if(beanName.equals("lifeCycleBean")){log.info("<<<<<<<实例化之前执行,返回的对象会替换掉原本的bean");}return null;}@Overridepublic boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {if(beanName.equals("lifeCycleBean")){log.info("<<<<<<<实例化之后执行,如果返回false会跳过依赖注入阶段");}return true;}@Nullablepublic PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {if(beanName.equals("lifeCycleBean")){log.info("<<<<<<<依赖注入阶段执行,如@Autowired,@Value,@Resource");}return pvs;}@Overridepublic void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {if(beanName.equals("lifeCycleBean")){log.info("<<<<<<<销毁之前执行,效果类似@PreDestroy");}}@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {if(beanName.equals("lifeCycleBean")){log.info("<<<<<<<初始化之前执行,返回的对象会替换掉原本的bean,如@PostConstruct、@ConfigurationProperties");}return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {if(beanName.equals("lifeCycleBean")){log.info("<<<<<<<初始化后执行,返回的对象会替换掉原本的bean,如代理增强");}return bean;}
}

2.InstantiationAwareBeanPostProcessor接口

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//package org.springframework.beans.factory.config;import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.lang.Nullable;public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {@Nullabledefault Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {return null;}default boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {return true;}@Nullabledefault PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {return pvs;}
}

3.DestructionAwareBeanPostProcessor接口

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//package org.springframework.beans.factory.config;import org.springframework.beans.BeansException;public interface DestructionAwareBeanPostProcessor extends BeanPostProcessor {void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException;default boolean requiresDestruction(Object bean) {return true;}
}

4.BeanPostProcessor接口

 上面两个接口都继承自这个接口。

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//package org.springframework.beans.factory.config;import org.springframework.beans.BeansException;
import org.springframework.lang.Nullable;public interface BeanPostProcessor {@Nullabledefault Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {return bean;}@Nullabledefault Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {return bean;}
}

5.主类调用

package com.example.demo2;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplication
public class Demo2Application {public static void main(String[] args) {ConfigurableApplicationContext context = SpringApplication.run(Demo2Application.class, args);context.close();}}
package com.example.demo2;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;/*** @author zhou* @version 1.0* @description TODO* @date 2025/9/29 21:48*/
@Component
public class LifeCycleBean {private static final Logger log = LoggerFactory.getLogger(LifeCycleBean.class);public LifeCycleBean(){log.info("构造方法");}@Autowiredpublic void autowire(@Value("${JAVA_HOME}") String home){log.info("依赖注入: {}",home);}@PostConstructpublic void init(){log.info("初始化");}@PreDestroypublic void destroy(){log.info("销毁");}
}

6.结果输出

       通过结果可以看到我们重写的各个方法执行的时机,上面输出的调试顺序也代表着Bean的生命周期。

http://www.dtcms.com/a/427775.html

相关文章:

  • 智能建站系统怎么更换网站模板泰安网上房地产
  • 免费自助建站哪个平台好本机做网站服务上传到
  • 前端 开发vscode trae idea 热键
  • Perl 引用
  • Linux time
  • 深圳网站建设推广论坛网站设计费报价表
  • 北斗时空安全隔离装置——关键设施“授时安全防火墙”
  • 网站源码上传安装包互联网公司排名前十名名单
  • 新注册公司怎么做网站互联网技术的作用
  • 重庆家居网站制作公司wap购物网站源码
  • 51我们一起做网站知乎网站建设入门书
  • 广西建设厅网站绿色建筑标识网站如何做伪静态页面
  • 做相亲网站德州市住房和城乡建设局网站
  • subprocess 模块
  • HTML网页模板下载 免费html模板网站
  • 爬虫逆向--Day25Day26--京东h5st案例解析
  • 深圳拼团网站建设网页游戏网游
  • 特征值与特征向量与SVD矩阵分解
  • 建设网站站点有哪些步骤网站建设 搜狐
  • Java Deque 和 ArrayDeque(栈的推荐使用) 基本介绍以及使用
  • 网站建设提供资料没有后台的网站怎么做排名
  • 使用BatchNorm偏置填充边界:确保推理一致性与数值稳定性
  • 山东官方网站建设网站配置优化
  • 14. 初识 SPDK
  • C++ STL insert和emplace函数区别
  • 湖北省建设厅行政审批网站哈尔滨优化调整人员流动管理
  • Windows 工作组环境信息收集与系统配置
  • QML学习笔记(二十一)QML的跨组件发送信号
  • 福田网站建设费用做企业网站的意义
  • 安卓人机验证View