网站建设 物流,咋做网站代码背景图,docker实际企业解决方案,免费做企业网站的步骤目录 基本的生命周期后处理器总结 基本的生命周期
为了演示生命周期的过程#xff0c;我们直接使用 SpringApplication.run()方法#xff0c;他会直接诶返回一个容器对象。
import org.springframework.boot.SpringApplication;
import org.springframework.context.Config… 目录 基本的生命周期后处理器总结 基本的生命周期
为了演示生命周期的过程我们直接使用 SpringApplication.run()方法他会直接诶返回一个容器对象。
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;SpringBootApplication
public class BeanLifecycleTest {public static void main(String[] args) {ConfigurableApplicationContext context SpringApplication.run(BeanLifecycleTest.class, args);context.close();}
}然后定义一个Bean如下
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;Component
public class LifeCycleBean {public LifeCycleBean() {System.out.println(构造方法);}/*** 依赖注入方法* 当参数是一个对象时可以直接注入* 但是如果是String类型则需要使用Value找到环境变量JAVA_HOME** param home*/Autowiredpublic void autowired(Value(${JAVA_HOME}) String home) {System.out.println(依赖注入);}/*** PostConstruct 用来标记 bean 初始化完成后的回调方法*/PostConstructpublic void init() {System.out.println(初始化);}/*** PreDestroy 用来标记 bean 销毁前的回调方法*/PreDestroypublic void destory() {System.out.println(销毁);}}上面的LifeCycleBean我们定义了构造方法、初始化方法、依赖注入方法、销毁方法。
启动应用打印如下
构造方法
依赖注入
初始化
2024-02-29 22:53:29.169 INFO 39870 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path
2024-02-29 22:53:29.182 INFO 39870 --- [ main] c.c.demo02.chapter03.BeanLifecycleTest : Started BeanLifecycleTest in 1.724 seconds (JVM running for 7.593)
2024-02-29 22:53:29.191 INFO 39870 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
销毁上面就可以看出整个流程。
后处理器
除了基本的生命周期下面看下加上后处理器后的流程。
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;Component
public class MyBeanPostProcessor implements InstantiationAwareBeanPostProcessor, DestructionAwareBeanPostProcessor {Overridepublic void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {if (beanName.equals(lifeCycleBean)) {System.out.println( postProcessBeforeDestruction 销毁之前执行);}}Overridepublic Object postProcessBeforeInstantiation(Class? beanClass, String beanName) throws BeansException {if (beanName.equals(lifeCycleBean)) {System.out.println( postProcessBeforeInstantiation 实例化之前执行);}return null;
// return InstantiationAwareBeanPostProcessor.super.postProcessBeforeInstantiation(beanClass, beanName);}Overridepublic boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {if (beanName.equals(lifeCycleBean)) {System.out.println( postProcessBeforeInstantiation 实例化之后执行);}// 如果返回false会跳过依赖注入阶段return true;
// return InstantiationAwareBeanPostProcessor.super.postProcessAfterInstantiation(bean, beanName);}Overridepublic PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {// 依赖注入阶段执行如AutowiredValue Resourceif (beanName.equals(lifeCycleBean)) {System.out.println( postProcessProperties 依赖注入阶段执行);}return null;
// return InstantiationAwareBeanPostProcessor.super.postProcessProperties(pvs, bean, beanName);}Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {// 初始化之前执行如PostConstructConfigurationPropertiesif (beanName.equals(lifeCycleBean)) {System.out.println( postProcessBeforeInitialization 初始化之前执行);}return null;
// return InstantiationAwareBeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);}Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {// 一般这个阶段是替换掉原有的bean代替增强if (beanName.equals(lifeCycleBean)) {System.out.println( postProcessAfterInitialization 初始化之后执行);}return null;
// return InstantiationAwareBeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);}
}再次执行结果如下 postProcessBeforeInstantiation 实例化之前执行
构造方法postProcessBeforeInstantiation 实例化之后执行postProcessProperties 依赖注入阶段执行
依赖注入postProcessBeforeInitialization 初始化之前执行postProcessAfterInitialization 初始化之后执行
2024-02-29 23:47:57.110 INFO 42057 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path
2024-02-29 23:47:57.145 INFO 42057 --- [ main] c.c.demo02.chapter03.BeanLifecycleTest : Started BeanLifecycleTest in 2.242 seconds (JVM running for 8.116)
2024-02-29 23:47:57.158 INFO 42057 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]postProcessBeforeDestruction 销毁之前执行
销毁通过上面可以看出各个后置处理器分别作用在Bean生命周期的哪个阶段了。
总结
基本生命周期 #mermaid-svg-uEK4KJx0Ll1v4Yl5 {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 .error-icon{fill:#552222;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 .marker.cross{stroke:#333333;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 .cluster-label text{fill:#333;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 .cluster-label span{color:#333;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 .label text,#mermaid-svg-uEK4KJx0Ll1v4Yl5 span{fill:#333;color:#333;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 .node rect,#mermaid-svg-uEK4KJx0Ll1v4Yl5 .node circle,#mermaid-svg-uEK4KJx0Ll1v4Yl5 .node ellipse,#mermaid-svg-uEK4KJx0Ll1v4Yl5 .node polygon,#mermaid-svg-uEK4KJx0Ll1v4Yl5 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 .node .label{text-align:center;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 .node.clickable{cursor:pointer;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 .arrowheadPath{fill:#333333;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 .cluster text{fill:#333;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 .cluster span{color:#333;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-uEK4KJx0Ll1v4Yl5 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 创建 依赖注入 初始化 可用 销毁 创建前后的增强
postProcessBeforeInstantiation这里返回的对象若不为 null 会替换掉原本的 bean并且仅会走 postProcessAfterInitialization 流程postProcessAfterInstantiation这里如果返回 false 会跳过依赖注入阶段
依赖注入前的增强
postProcessProperties如 Autowired、Value、Resource
初始化前后的增强
postProcessBeforeInitialization这里返回的对象会替换掉原本的 bean如 PostConstruct、ConfigurationPropertiespostProcessAfterInitialization 这里返回的对象会替换掉原本的 bean如代理增强
销毁之前的增强
postProcessBeforeDestruction如 PreDestroy