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. Baidu UEditor .net 下修改默认上传路径

    public override void Process() { byte[] uploadFileBytes = null; string uploadFileName = null; if (Up ...

  2. golang 数据导出excel (github.com/360EntSecGroup-Skylar/excelize)

    package handler import ( "fmt" "git.shannonai.com/public_info_prophet/prophet_risk_ag ...

  3. QT之类型转换

    Qt在进行数据类型转换时,容易忘记如何使用,或者是早已厌倦了百度QString转QByteArray,QByteArray转char,QString转string....... 现在分享一篇QT数据类 ...

  4. 数据库权限优化,权限设计BigInteger

    最近看到了一个项目的权限是根据bigineger来进行计算的菜单权限,觉得还是不错,存储上只需要存储在一个字段里就可以了,通过计算算出该角色的菜单权限即可,效率也非常的快,放在session中也非常的 ...

  5. XAML加载的四种方式

    XAML加载与编译可以分为四种: 仅使用代码进行WPF程序的生成 使用代码和未编译的标记 使用代码和编译过的BAML 1.只是用代码进行窗体的生成:优点是可以随意定制应用程序,缺点是没有可视化编辑窗口 ...

  6. Ubuntu 18.04 RTX2080(ti) + tensorflow-gpu + cuda9.0 + gcc5 兼容性问题解决

    0.下载display driver.cuda和cudnn RTX2080 Display Driver cuda cudnn 1. 禁止系统默认的显卡驱动 打开系统黑名单 sudo gedit /e ...

  7. tf.assign_add

    import tensorflow as tf global_step = tf.Variable(1.0, dtype=tf.float64, trainable=False, name='glob ...

  8. kvm第一章--概念

  9. Vue学习之全局和私有组件小结(七)

    一.组件: 组件的出现,就是为了拆分Vue实例的代码量的,能够让我们以不同的组件,来划分不同的功能模块,将来我们需要什么样的功能,就可以去调用相应的组件即可. 二.组件和模块: 1.模块化:是从代码逻 ...

  10. vue 实现滚动到页面底部开始加载更多

    直接上代码: <template> <div class="newsList"> <div v-for="(items, index) in ...