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

有没有学做家具的网站深圳的知名网站设计有哪些

有没有学做家具的网站,深圳的知名网站设计有哪些,高端建材门店年销售额,ftp如何备份网站1. Spring Bean 生命周期概述 Spring 框架中,Bean 的生命周期是其核心机制之一。了解 Bean 的完整生命周期有助于开发者更好地控制对象的创建、初始化、使用和销毁过程,尤其是在需要扩展、增强或定制 Bean 行为时,如资源加载、AOP 代理、日志…

1. Spring Bean 生命周期概述

Spring 框架中,Bean 的生命周期是其核心机制之一。了解 Bean 的完整生命周期有助于开发者更好地控制对象的创建、初始化、使用和销毁过程,尤其是在需要扩展、增强或定制 Bean 行为时,如资源加载、AOP 代理、日志记录等。

Spring Bean 的生命周期通常包括以下阶段(以 Singleton 作用域 为例):

阶段描述可干预方式开发中可能的操作
1. 实例化通过反射创建 Bean 实例无需干预通常由 Spring 自动完成
2. 属性注入注入依赖属性(字段、构造器、setter)无需干预通过 @Autowired@Resource 注入依赖
3. 前处理(BeforeInit):

在初始化方法之前执行;

执行方法BeanPostProcessor#postProcessBeforeInitialization

实现BeanPostProcessor 接口修改 Bean 属性、创建代理、日志记录
4. 初始化方法执行 @PostConstructInitializingBean#afterPropertiesSet()、或 init-method使用注解或配置资源加载(如数据库连接)、校验逻辑、预处理
5. 后处理(AfterInit):

在初始化方法之后执行;

执行方法BeanPostProcessor#postProcessAfterInitialization

实现BeanPostProcessor 接口AOP 代理创建、权限控制、日志记录
6. 使用阶段Bean 已准备好,可供应用使用无需干预业务调用、依赖注入
7. 销毁方法容器关闭时执行 @PreDestroyDisposableBean#destroy()、或 destroy-method使用注解或配置资源释放(如关闭连接池、停止线程)

2. Spring Bean生命周期的代码演示

2.1 代码示例

step1. 定义一个 Bean

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;@Component
public class MyBean implements InitializingBean, DisposableBean {public MyBean() {System.out.println("1. MyBean 实例化");}public void initMethod() {System.out.println("4. init-method: 初始化方法");}@PostConstructpublic void postConstruct() {System.out.println("4. @PostConstruct: 初始化方法");}@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("4. InitializingBean#afterPropertiesSet: 初始化方法");}@PreDestroypublic void preDestroy() {System.out.println("7. @PreDestroy: 销毁方法");}@Overridepublic void destroy() throws Exception {System.out.println("7. DisposableBean#destroy: 销毁方法");}
}

step2. 定义一个 BeanPostProcessor

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;@Component
public class MyBeanPostProcessor implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("3. BeanPostProcessor#postProcessBeforeInitialization: " + beanName);return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("5. BeanPostProcessor#postProcessAfterInitialization: " + beanName);return bean;}
}

step3. 配置 Bean 的 init 和 destroy 方法(可选)

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class AppConfig {@Bean(initMethod = "initMethod", destroyMethod = "destroy")public MyBean myBean() {return new MyBean();}
}

step4. 主启动类(Spring Boot 示例)

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplication
public class Application {public static void main(String[] args) {ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);MyBean myBean = context.getBean(MyBean.class);System.out.println("6. Bean 已使用");// 关闭容器以触发销毁方法context.close();}
}

启动主类后,控制台输出顺序(部分)示例

1. MyBean 实例化
3. BeanPostProcessor#postProcessBeforeInitialization: myBean
4. @PostConstruct: 初始化方法
4. InitializingBean#afterPropertiesSet: 初始化方法
4. init-method: 初始化方法
5. BeanPostProcessor#postProcessAfterInitialization: myBean
6. Bean 已使用
7. @PreDestroy: 销毁方法
7. DisposableBean#destroy: 销毁方法

2.2 注意事项

  • 多个初始化/销毁方法:Spring 支持多种方式定义初始化和销毁方法,按照以下优先级执行:
    • @PostConstruct > InitializingBean#afterPropertiesSet() > init-method
    • @PreDestroy > DisposableBean#destroy() > destroy-method
  • Bean 作用域影响
    • @Scope("prototype") 的 Bean 不会执行 destroy 方法,除非手动调用 destroy()
  • BeanPostProcessor 顺序:如果有多个 BeanPostProcessor,可通过 @Order 或实现 Ordered 接口控制执行顺序。

3. 总结

Spring Bean 的生命周期是一个清晰且可扩展的过程。通过理解每个阶段的功能和干预方式,开发者可以灵活地控制 Bean 的行为,实现诸如 AOP、日志、资源管理等高级功能。掌握生命周期不仅有助于调试,还能帮助构建更加健壮和可维护的 Spring 应用。相关应用场景举例如下:

场景使用阶段
日志记录BeanPostProcessor
资源加载初始化方法(@PostConstruct
代理对象创建postProcessAfterInitialization(AOP 实现)
数据库连接关闭销毁方法(@PreDestroy
权限控制postProcessBeforeInitialization

4.相关文档

BeanPostProcessor用法-笔记-CSDN博客

InitializingBean接口和@PostConstruct-笔记_postconstruct和initializingbean-CSDN博客

SpringBoot启动后自动执行方法的各种方式-笔记_springboot项目启动自动调用方法-CSDN博客

Spring的xxxAware接口工作原理-笔记-CSDN博客


文章转载自:

http://wv9GLmpb.znrgq.cn
http://wKuaG3vy.znrgq.cn
http://dnRRsTG7.znrgq.cn
http://TKTyucb2.znrgq.cn
http://o1B5wI7d.znrgq.cn
http://rJmZXzVB.znrgq.cn
http://ElmjAGkK.znrgq.cn
http://wNu6GaCy.znrgq.cn
http://5PHR2yal.znrgq.cn
http://Yxua35Ho.znrgq.cn
http://BWCuIsm8.znrgq.cn
http://9w9CqXfA.znrgq.cn
http://HbesVQyp.znrgq.cn
http://D4AkNRGo.znrgq.cn
http://5CgwACxs.znrgq.cn
http://CoB0anH0.znrgq.cn
http://jQye7oDA.znrgq.cn
http://i9mWgW6R.znrgq.cn
http://oNUd68rX.znrgq.cn
http://5vuohSGM.znrgq.cn
http://NyQM1bIk.znrgq.cn
http://QSOZtaQW.znrgq.cn
http://LG4L6MRe.znrgq.cn
http://P0A5oZ3w.znrgq.cn
http://FEUh4GvE.znrgq.cn
http://jpKjaOCv.znrgq.cn
http://AeMcQBkJ.znrgq.cn
http://DkJlikb0.znrgq.cn
http://g2jivXPh.znrgq.cn
http://YD8gYmVZ.znrgq.cn
http://www.dtcms.com/wzjs/650041.html

相关文章:

  • 一个网站做数据维护需要多久徐州网站外包
  • app和网站哪个难做php网站开发培训
  • 怎么查网站是否被k应用软件大全
  • 中国制造网网站特色学网站建设的工资高吗
  • 北京网站开发设计qq空间刷赞网站推广
  • 数据上传网站做网站收广告费
  • 做视频直播的网站免费智能seo收录工具
  • 个人网站备案流程重庆市网上房地产官网
  • 临海营销型网站建设中山教育平台网站建设
  • 微网站在哪个平台上搭建好 知乎wordpress图片上传压缩
  • 折再返怎么 做网站wordpress 域名米表
  • 北京网站建设公司 蓝纤科技 网络服务江苏企业seo推广
  • 网站备案准备资料系统优化最好的安卓手机
  • 一家专做有机蔬菜的网站网红营销价值
  • 南昌网站建设公司收费万网手机网站
  • 深圳公司形象墙制作百度上做优化一年多少钱
  • jquery 手机网站开发建设网站需要备案么
  • 湖南做网站的公司网站首页收录没有了
  • 怎样评价一个网站做的好与不好网站服务器错误
  • 网站文字编辑怎么做黄冈黄页
  • 泰安整站优化臭事百科wordpress
  • 郑州郑州网站建设河南做网站公司点击精灵seo
  • 做网站要学哪些代码岳阳云溪区建设局网站
  • 天河网站建设优化安徽建设厅网站施
  • 一般做网站服务器的cpu怎么重新打开wordpress
  • 网站建设市场占有率wordpress提货下载
  • 网站视频可以拔下来做的网站吗
  • 青海企业网站制作帮忙做快站旅游网站
  • 沧州市网站建设公司设计wordpress页面模板
  • 网站的数据库选择贵阳网站建设公司哪个好