1. package com.xx;
  2. import javax.annotation.PostConstruct;
  3. import javax.annotation.Resource;
  4. import org.springframework.beans.factory.InitializingBean;
  5. import org.springframework.context.ApplicationListener;
  6. import org.springframework.context.event.ContextRefreshedEvent;
  7. import org.springframework.stereotype.Component;
  8. import com.xx.service.DemoService;
  9. @Component
  10. public class InitBeanTest implements InitializingBean,ApplicationListener<ContextRefreshedEvent> {
  11. @Resource
  12. DemoService demoService;
  13. public InitBeanTest() {
  14. System.err.println("----> InitSequenceBean: constructor: "+demoService);
  15. }
  16. @PostConstruct
  17. public void postConstruct() {
  18. System.err.println("----> InitSequenceBean: postConstruct: "+demoService);
  19. }
  20. @Override
  21. public void afterPropertiesSet() throws Exception {
  22. System.err.println("----> InitSequenceBean: afterPropertiesSet: "+demoService);
  23. }
  24. @Override
  25. public void onApplicationEvent(ContextRefreshedEvent arg0) {
  26. System.err.println("----> InitSequenceBean: onApplicationEvent");
  27. }
  28. }

执行结果:

----> InitSequenceBean: constructor: null
----> InitSequenceBean: postConstruct: com.yiniu.kdp.service.impl.DemoServiceImpl@40fe544
----> InitSequenceBean: afterPropertiesSet: com.yiniu.kdp.service.impl.DemoServiceImpl@40fe544
----> InitSequenceBean: onApplicationEvent
----> InitSequenceBean: onApplicationEvent

分析:

构造函数是每个类最先执行的,这个时候,bean属性还没有被注入

postConstruct优先于afterPropertiesSet执行,这时属性竟然也被注入了,有点意外

spring很多组建的初始化都放在afterPropertiesSet做。我们在做一些中间件想和spring一起启动,可以放在这里启动。

onApplicationEvent属于应用层的时间,最后被执行,很容易理解。注意,它出现了两次,为什么?因为bean注入了DemoService,spring容器会被刷新。

换言之onApplicationEvent会被频繁执行,需要使用它监听,需要考虑性能问题。

很显然,这是观察者模式的经典应用。

Spring启动,constructor,@PostConstruct,afterPropertiesSet,onApplicationEvent执行顺序的更多相关文章

  1. Spring MVC中Filter Servlet Interceptor 执行顺序

    <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springfr ...

  2. Spring的Bean的生命周期方法执行顺序测试

    通过一个简单的Maven工程来演示Spring的Bean生命周期函数的执行顺序. 下面是工程的目录结构: 直接贴代码: pom.xml文件内容: <?xml version="1.0& ...

  3. SpringMVC自定义多个拦截器执行顺序

    一.正常流程下的拦截器(全部放行) 1.springMVC中拦截器实现这个接口HandlerInterceptor 第一个拦截器 HandlerInterceptor1   public class ...

  4. Spring InitializingBean init-method @PostConstruct 执行顺序

    Spring 容器中的 Bean 是有生命周期的,Spring 允许在 Bean 在初始化完成后以及 Bean 销毁前执行特定的操作,常用的设定方式有以下三种:   通过实现 Initializing ...

  5. spring注解之@PostConstruct在项目启动时执行指定方法

    一.注解解释 Spring的@PostConstruct注解在方法上,表示此方法是在Spring实例化该Bean之后马上执行此方法,之后才会去实例化其他Bean,并且一个Bean中@PostConst ...

  6. spring bean中构造函数,afterPropertiesSet和init-method的执行顺序

    http://blog.csdn.net/super_ccc/article/details/50728529 1.xml文件 <bean id="aaa" class=&q ...

  7. Spring生命周期 Constructor > @PostConstruct > InitializingBean > init-method

    项目中用到了 afterPropertiesSet: 于是具体的查了一下到底afterPropertiesSet到底是什么时候执行的.为什么一定要实现 InitializingBean; **/ @C ...

  8. 在Spring Bean的生命周期中各方法的执行顺序

    Spring 容器中的 Bean 是有生命周期的,Spring 允许在 Bean 在初始化完成后以及 Bean 销毁前执行特定的操作,常用的设定方式有以下十种: 通过实现 InitializingBe ...

  9. spring中bean初始化执行顺序

    常用的javabean的初始化方法为,构造方法,@PostConstruct,以及实现InitializingBean接口的afterPropertiesSet方法. note在构造方法执行时候,sp ...

随机推荐

  1. 第九节:ASP.NET Core 中多环境的使用

    一. 环境相关配置 1. 说明 ASP.NET Core 在应用启动时读取环境变量 ASPNETCORE_ENVIRONMENT, ASPNETCORE_ENVIRONMENT 可设置为任意值,但框架 ...

  2. LPAT: Learning to Predict Adaptive Threshold for Weakly-supervised Temporal Action Localization [Paper Reading]

    Motivation: 阈值分割的阈值并没有通过模型训练学出来,而是凭借主观经验设置,本文通过与背景得分比较提取对应的proposal,不用阈值的另一篇文章是Shou Zheng的AutoLoc,通过 ...

  3. Spring Cloud OkHttp设计原理

    Spring Cloud 框架最底层核心的组件就是服务调用方式,一般Spring Cloud框架采用的是HTTP的调用框架,本文将在 Spring Cloud应用场景下,介绍组件OkHttp3的设计原 ...

  4. 2014百度之星 Party

    Party Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  5. Java学习之旅(一):探索extends

    鄙人为兴趣爱好,0基础入门学习Java,有些心得想法,记录于此,与君分享. 然毕竟新手,学识尚浅,错误之处,希望多多指正批评,也是对我最大的帮助! 前言:本篇文章,主要讨论在子类继承父类之后,一些继承 ...

  6. lucene字典实现原理(转)

    原文:https://www.cnblogs.com/LBSer/p/4119841.html 1 lucene字典 使用lucene进行查询不可避免都会使用到其提供的字典功能,即根据给定的term找 ...

  7. B树和B+树的增/删结点(转)

    add by zhj: 算法其实不复杂,尤其是增加结点的算法,逻辑很简单,但有时自己想不到. 增加结点算法:首先,对于B树,没有重复结点,所以新插入的数据一定会落在叶结点上,或者说落在叶结点的所有父结 ...

  8. 示例:WPF中自定义MessageService应用DialogHost、Snackbar、NotifyIcon显示各种场景提示消息

    原文:示例:WPF中自定义MessageService应用DialogHost.Snackbar.NotifyIcon显示各种场景提示消息 一.目的:不同交互场景需要提示不同的消息,不同的消息需要用不 ...

  9. 【已解決】谷歌浏览器如何清理缓存(cookie)

    清除缓存快捷键 Ctrl+Shift+Delete

  10. 1-python运算符和逻辑控制语句

    目录 运算符 条件语句if…else 断言assert 循环语句while 遍历for循环 1.运算符 1.1.算数运算符 加+.减-.乘*.除/.余%.次方**.向下取整除// 1.2.赋值运算符 ...