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

网站开发设计总结上海开发小程序

网站开发设计总结,上海开发小程序,慈溪建设企业网站,现在网站给源码目录 基本的生命周期后处理器总结 基本的生命周期 为了演示生命周期的过程#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
http://www.hkea.cn/news/14564158/

相关文章:

  • 最大的开源网站天津seo建站
  • 网站建设属于现代服务吗三合一网站一般多少钱
  • 福清网站建设专家xp做网站
  • 宁夏高端网站建设创建自己网站的步骤
  • 校考前做试题的网站培训网站源码wordpress
  • 哪个网站做数学题赚钱宁波建设检测
  • 公司网站SEO优化哪个做得好做外贸学习网站
  • 做婚纱的网站网站程序设置主页面
  • 东莞市建设网站首页设计学分类
  • owasp 网站开发wordpress播放
  • 专业网站开发服务如何宣传推广自己的产品
  • 企业如何应用网站的怎么做平台网站
  • 什么都能买到的网站wordpress option
  • 吴川网站建设公司深圳小程序推广
  • 怎么自己免费创建网站广州网站建设骏域
  • 深圳开发公司网站建设wordpress调用指定分类的文章列表
  • 花网站开发背景建设网站需要的人才
  • 建设一个商业网站费用做采购 通常在什么网站看
  • 医药招商网站建设网站设置子目录
  • 网站开发邮件服务器网站服务器数据迁移
  • 如何自己做网站优化wordpress后台添加侧边栏菜单
  • 网站推广类型微商系统软件开发
  • project 网站开发计划wordpress 种子搜索引擎
  • 电商网站建设工具互联网站建设 天津
  • 网站单页面怎么做的长沙网站优化对策
  • 个人网站涉及企业内容dede网站源码
  • 广州网站建设广州网络推广公司排名网站域名被黑
  • 2023企业所得税最新政策seo关键词优化软件排名
  • 青岛网站seo服务唐山网站建设找煌途
  • 如何优化自己的网站网页布局有哪几种