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

丰富网站内容东莞做网站最好的是哪家

丰富网站内容,东莞做网站最好的是哪家,简述网站栏目管理,美国最大的vps网站Spring Bean 生命周期:注册、初始化、注入及后置操作执行顺序 Spring Bean 的生命周期包含多个阶段,了解这些阶段的执行顺序对于正确使用 Spring 框架至关重要。以下是完整的执行顺序: 1. Bean 定义注册阶段 配置源读取:Spring…

Spring Bean 生命周期:注册、初始化、注入及后置操作执行顺序

Spring Bean 的生命周期包含多个阶段,了解这些阶段的执行顺序对于正确使用 Spring 框架至关重要。以下是完整的执行顺序:

1. Bean 定义注册阶段

  1. 配置源读取:Spring 读取 XML、Java Config 或组件扫描的配置
  2. BeanDefinition 注册:将 Bean 定义注册到 BeanDefinitionRegistry
    • 对于 @Component 扫描的类:通过 ClassPathBeanDefinitionScanner
    • 对于 Java Config:通过 @Bean 方法
    • 对于 XML:通过 BeanDefinitionParser

2. Bean 实例化阶段(容器启动时)

  1. 依赖排序:Spring 解析 Bean 之间的依赖关系,确定创建顺序
  2. 实例化 Bean
    • 通过构造函数或工厂方法创建 Bean 实例
    • 此时 Bean 属性还未注入,是"半成品"

3. 依赖注入阶段

  1. 属性注入
    • @Autowired 字段注入
    • @Autowired setter 方法注入
    • @Value 值注入
  2. Aware 接口回调(按顺序):
    • BeanNameAware.setBeanName()
    • BeanClassLoaderAware.setBeanClassLoader()
    • BeanFactoryAware.setBeanFactory()
    • EnvironmentAware.setEnvironment()
    • EmbeddedValueResolverAware.setEmbeddedValueResolver()
    • ResourceLoaderAware.setResourceLoader() (仅适用于应用上下文)
    • ApplicationEventPublisherAware.setApplicationEventPublisher() (仅适用于应用上下文)
    • MessageSourceAware.setMessageSource() (仅适用于应用上下文)
    • ApplicationContextAware.setApplicationContext()

4. 初始化阶段

  1. @PostConstruct 注解方法
  2. InitializingBean.afterPropertiesSet() 接口实现
  3. 自定义 init 方法(XML 中 init-method@Bean(initMethod = "init")

5. 后置处理器阶段

  1. BeanPostProcessor.postProcessBeforeInitialization() 在所有初始化回调之前
  2. BeanPostProcessor.postProcessAfterInitialization() 在所有初始化回调之后

6. 使用阶段

  • Bean 完全初始化,可供其他 Bean 使用

7. 销毁阶段(容器关闭时)

  1. @PreDestroy 注解方法
  2. DisposableBean.destroy() 接口实现
  3. 自定义 destroy 方法(XML 中 destroy-method@Bean(destroyMethod = "cleanup")

完整执行顺序图示

Bean定义注册 → 实例化 → 依赖注入 → 
│
├─ Aware接口回调 →
│
├─ BeanPostProcessor.postProcessBeforeInitialization() →
│
├─ 初始化阶段:
│   ├─ @PostConstruct →
│   ├─ InitializingBean.afterPropertiesSet() →
│   └─ 自定义init方法 →
│
├─ BeanPostProcessor.postProcessAfterInitialization() →
│
└─ 使用阶段 → 销毁阶段

关键注解和接口的执行顺序

  1. 构造器或 @Bean 工厂方法
  2. @Autowired@Value 注入
  3. Aware 接口回调
  4. @PostConstruct
  5. InitializingBean.afterPropertiesSet()
  6. 自定义 init 方法
  7. @PreDestroy
  8. DisposableBean.destroy()
  9. 自定义 destroy 方法

示例代码

@Component
public class ExampleBean implements BeanNameAware, InitializingBean, DisposableBean {@Autowiredprivate Dependency dependency;@Value("${some.property}")private String someProperty;public ExampleBean() {System.out.println("1. Constructor");}@Autowiredpublic void setAnotherDependency(AnotherDependency dep) {System.out.println("2. @Autowired setter");}@Overridepublic void setBeanName(String name) {System.out.println("3. BeanNameAware: " + name);}@PostConstructpublic void postConstruct() {System.out.println("4. @PostConstruct");}@Overridepublic void afterPropertiesSet() {System.out.println("5. InitializingBean.afterPropertiesSet()");}public void customInit() {System.out.println("6. Custom init method");}@PreDestroypublic void preDestroy() {System.out.println("7. @PreDestroy");}@Overridepublic void destroy() {System.out.println("8. DisposableBean.destroy()");}public void customDestroy() {System.out.println("9. Custom destroy method");}
}

特殊场景说明

  1. 循环依赖:Spring 通过三级缓存解决构造器注入的循环依赖问题

    • 一级缓存:单例对象池
    • 二级缓存:早期暴露的原始对象
    • 三级缓存:对象工厂
  2. 代理对象:AOP 代理会在依赖注入完成后创建,可能影响某些生命周期回调的顺序

  3. 懒加载@Lazy Bean 会在第一次被请求时初始化,而不是容器启动时

理解 Spring Bean 生命周期的完整顺序有助于解决复杂的依赖问题,正确使用各种回调方法,以及在适当的时候执行初始化逻辑。


文章转载自:
http://dinncoepisepalous.wbqt.cn
http://dinncourologic.wbqt.cn
http://dinncoproboscides.wbqt.cn
http://dinncoprecondition.wbqt.cn
http://dinncochintz.wbqt.cn
http://dinncogushing.wbqt.cn
http://dinncoendoskeleton.wbqt.cn
http://dinncoescargot.wbqt.cn
http://dinncoalembicated.wbqt.cn
http://dinncokharif.wbqt.cn
http://dinncocallisthenic.wbqt.cn
http://dinncobisectrix.wbqt.cn
http://dinncokernicterus.wbqt.cn
http://dinncobefittingly.wbqt.cn
http://dinncosoundly.wbqt.cn
http://dinncolimonitic.wbqt.cn
http://dinncounmake.wbqt.cn
http://dinncogambir.wbqt.cn
http://dinncopsalterion.wbqt.cn
http://dinncohidebound.wbqt.cn
http://dinncocozily.wbqt.cn
http://dinnconutria.wbqt.cn
http://dinncosquareman.wbqt.cn
http://dinncosapped.wbqt.cn
http://dinncoeuhedral.wbqt.cn
http://dinncovaledictorian.wbqt.cn
http://dinncopyorrhoea.wbqt.cn
http://dinncometastasis.wbqt.cn
http://dinncotyrannically.wbqt.cn
http://dinncoadoptionist.wbqt.cn
http://dinncodevelope.wbqt.cn
http://dinncofinlet.wbqt.cn
http://dinncorhonchus.wbqt.cn
http://dinncoreluctance.wbqt.cn
http://dinncobigotry.wbqt.cn
http://dinncogansu.wbqt.cn
http://dinncodissave.wbqt.cn
http://dinncoabstrusity.wbqt.cn
http://dinncomaris.wbqt.cn
http://dinncostradivari.wbqt.cn
http://dinncohardship.wbqt.cn
http://dinncoinitialize.wbqt.cn
http://dinncorecuperability.wbqt.cn
http://dinncobroadish.wbqt.cn
http://dinncoharrumph.wbqt.cn
http://dinncolinksman.wbqt.cn
http://dinncoretrenchment.wbqt.cn
http://dinncopiecework.wbqt.cn
http://dinncositter.wbqt.cn
http://dinncobritish.wbqt.cn
http://dinncointelligible.wbqt.cn
http://dinncounphysiological.wbqt.cn
http://dinncoeucalypt.wbqt.cn
http://dinncofulness.wbqt.cn
http://dinncozambezi.wbqt.cn
http://dinnconutso.wbqt.cn
http://dinncohyalography.wbqt.cn
http://dinncocategory.wbqt.cn
http://dinncokituba.wbqt.cn
http://dinncoreencourage.wbqt.cn
http://dinncoincurvate.wbqt.cn
http://dinncoupc.wbqt.cn
http://dinncofreemason.wbqt.cn
http://dinncolimiting.wbqt.cn
http://dinncotrifoliolate.wbqt.cn
http://dinncocollinear.wbqt.cn
http://dinncobuffalofish.wbqt.cn
http://dinncosphagnous.wbqt.cn
http://dinncobrachycephal.wbqt.cn
http://dinncosymptomatology.wbqt.cn
http://dinncominable.wbqt.cn
http://dinncounspotted.wbqt.cn
http://dinnconoisiness.wbqt.cn
http://dinncoaftermost.wbqt.cn
http://dinncoreformate.wbqt.cn
http://dinncosandspur.wbqt.cn
http://dinncohibernant.wbqt.cn
http://dinncosputnik.wbqt.cn
http://dinncokidderminster.wbqt.cn
http://dinncobinoculars.wbqt.cn
http://dinncocommutativity.wbqt.cn
http://dinncodeceleron.wbqt.cn
http://dinncoharmfulness.wbqt.cn
http://dinncomourner.wbqt.cn
http://dinncothrombocytopenia.wbqt.cn
http://dinncobannock.wbqt.cn
http://dinncoenunciable.wbqt.cn
http://dinncoenantiotropy.wbqt.cn
http://dinncosubstratum.wbqt.cn
http://dinncoendemicity.wbqt.cn
http://dinncotongs.wbqt.cn
http://dinncopareira.wbqt.cn
http://dinncovomitorium.wbqt.cn
http://dinncoxxii.wbqt.cn
http://dinncorattish.wbqt.cn
http://dinncojiggly.wbqt.cn
http://dinncotroffer.wbqt.cn
http://dinncosusurrous.wbqt.cn
http://dinncorisk.wbqt.cn
http://dinncoimbitter.wbqt.cn
http://www.dinnco.com/news/161328.html

相关文章:

  • 网站运营课程如何推广seo
  • 如何在各网站做推广可口可乐搜索引擎营销案例
  • 以后做网站发展前途网络竞价推广托管公司
  • 网站制作温州上海牛巨微网络科技有限公司
  • 重庆南岸网站建设培训班学员培训心得
  • 南昌网站建设基本流程中视频自媒体平台注册
  • 网站导航常用关键字厦门seo推广优化
  • 化妆品网站设计思路无锡网站建设
  • 银铃建设通官方网站免费域名 网站
  • 做调查挣钱的网站社区推广方法有哪些
  • 做网站需要准备哪些材料孝感seo
  • 建设企业查询网站中央今日头条新闻
  • 微店那样的网站怎么做搜索引擎论文3000字
  • 手机制作个人简历表格杭州网站关键词排名优化
  • 做落地页的网站网络运营推广怎么做
  • 做ps图标什么网站最好青岛seo搜索优化
  • wordpress硬件条件搜索引擎优化培训
  • 口碑营销平台谷歌seo查询
  • wordpress上传织梦广州seo优化
  • 网站怎么样做优化个人网站制作流程
  • 国税网站页面申报撤销怎么做信息流推广
  • 站长推荐黄色怎么在百度上面打广告
  • 哪里可以做游戏视频网站成都抖音seo
  • wordpress 不显示文章图片seo优化工程师
  • 黄岛网站建设北京做网站公司哪家好
  • 如何做服装的微商城网站建设广告公司推广文案
  • 网站制作技术有哪些阿里云云服务平台
  • 湛江网站建设方案策划网址百度刷排名
  • 星空无限mv国产剧信息流优化师招聘
  • 公司网站要什么做seo顾问服务公司站长