Spring启动,constructor,@PostConstruct,afterPropertiesSet,onApplicationEvent执行顺序
- package com.xx;
- import javax.annotation.PostConstruct;
- import javax.annotation.Resource;
- import org.springframework.beans.factory.InitializingBean;
- import org.springframework.context.ApplicationListener;
- import org.springframework.context.event.ContextRefreshedEvent;
- import org.springframework.stereotype.Component;
- import com.xx.service.DemoService;
- @Component
- public class InitBeanTest implements InitializingBean,ApplicationListener<ContextRefreshedEvent> {
- @Resource
- DemoService demoService;
- public InitBeanTest() {
- System.err.println("----> InitSequenceBean: constructor: "+demoService);
- }
- @PostConstruct
- public void postConstruct() {
- System.err.println("----> InitSequenceBean: postConstruct: "+demoService);
- }
- @Override
- public void afterPropertiesSet() throws Exception {
- System.err.println("----> InitSequenceBean: afterPropertiesSet: "+demoService);
- }
- @Override
- public void onApplicationEvent(ContextRefreshedEvent arg0) {
- System.err.println("----> InitSequenceBean: onApplicationEvent");
- }
- }
执行结果:
----> 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执行顺序的更多相关文章
- Spring MVC中Filter Servlet Interceptor 执行顺序
<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springfr ...
- Spring的Bean的生命周期方法执行顺序测试
通过一个简单的Maven工程来演示Spring的Bean生命周期函数的执行顺序. 下面是工程的目录结构: 直接贴代码: pom.xml文件内容: <?xml version="1.0& ...
- SpringMVC自定义多个拦截器执行顺序
一.正常流程下的拦截器(全部放行) 1.springMVC中拦截器实现这个接口HandlerInterceptor 第一个拦截器 HandlerInterceptor1 public class ...
- Spring InitializingBean init-method @PostConstruct 执行顺序
Spring 容器中的 Bean 是有生命周期的,Spring 允许在 Bean 在初始化完成后以及 Bean 销毁前执行特定的操作,常用的设定方式有以下三种: 通过实现 Initializing ...
- spring注解之@PostConstruct在项目启动时执行指定方法
一.注解解释 Spring的@PostConstruct注解在方法上,表示此方法是在Spring实例化该Bean之后马上执行此方法,之后才会去实例化其他Bean,并且一个Bean中@PostConst ...
- spring bean中构造函数,afterPropertiesSet和init-method的执行顺序
http://blog.csdn.net/super_ccc/article/details/50728529 1.xml文件 <bean id="aaa" class=&q ...
- Spring生命周期 Constructor > @PostConstruct > InitializingBean > init-method
项目中用到了 afterPropertiesSet: 于是具体的查了一下到底afterPropertiesSet到底是什么时候执行的.为什么一定要实现 InitializingBean; **/ @C ...
- 在Spring Bean的生命周期中各方法的执行顺序
Spring 容器中的 Bean 是有生命周期的,Spring 允许在 Bean 在初始化完成后以及 Bean 销毁前执行特定的操作,常用的设定方式有以下十种: 通过实现 InitializingBe ...
- spring中bean初始化执行顺序
常用的javabean的初始化方法为,构造方法,@PostConstruct,以及实现InitializingBean接口的afterPropertiesSet方法. note在构造方法执行时候,sp ...
随机推荐
- bolt继承关系和区别
先上个图: 具体区别: IRichBolt/IBasicBolt 区别IRichBolt和IBasicBolt IRichBolt继承自IBolt和IComponent.IBasicBolt继承自I ...
- 59 网络编程(一)——端口与InetSocketAddress
端口与几个CMD命令 公认端口:0-1023 比如80端口分配给www,21端口分配给FTP等 注册端口:2014-49151 分配给用户进程或引用程序 动态/私有端口:49151-65535 需要 ...
- NodeJS 使用内容以及模拟一个接口
1.结合上一篇 安装完Nodejs之后 通过手动创建一个完整的NodeJs项目 2.https://www.jianshu.com/p/7b0a5d4491ba 创建一个完整的项目之后 3.下面是一个 ...
- Java学习:List接口
List接口 java.util.list接口 extends Collection接口 List接口的特点: 有序的集合,存储元素和取出元素的顺序是一致的(存储123 取出123) 有索引,包含了一 ...
- Mysql系列(一)—— 基于5.7.22 解压版下载、安装、配置和卸载
1.下载 从官网中直接获取自己想要的版本: MySQL Community Server 5.7.22 2.解压 将下载到的文件解压缩到自己喜欢的位置. 与mysql 5.6不同的是5.7版本中没有d ...
- 集成开发环境(IDE)
学习目标: 1.了解Java的IDE开发工具 2.会使用Eclipse.IDEA开发工具新建项目,编写代码,并运行程序. 学习过程: 使用文本开发效率无疑是很低的,每次编写完代码后,还需要手动的编译执 ...
- ffmpeg命令参数详解
ffmpeg命令参数详解 http://linux.51yip.com/search/ffmpeg ffmpeg图片加滤镜效果 参考:https://cloud.tencent.com/develop ...
- 1-python运算符和逻辑控制语句
目录 运算符 条件语句if…else 断言assert 循环语句while 遍历for循环 1.运算符 1.1.算数运算符 加+.减-.乘*.除/.余%.次方**.向下取整除// 1.2.赋值运算符 ...
- 在 Vim 中,删除 ^@ 符号的几种方法
在 Vim 中,^@ 表示 ASCII 码中的 NULL 字符,编码为 0x00,占用一个字节. 删除方法 方法1,采用 <CTRL-V><CTRL-J> 或 <CTRL ...
- jq 实现切换菜单选中状态
点击导航菜单,切换选中状态 效果: 思路:首先获取选中的URL,再通过正则判断是否相同,相同就加上相应的属性,不相同就去除相应的属性. html代码 <div class="layui ...