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 ...
随机推荐
- 宏offsetof分析
1.前言 在C语言的结构体中,由于字节对齐的问题,所以成员的地址并不能直接根据数据类型的大小进行计算,使用宏offsetof可以获得结构体成员相对于结构体首地址的字节偏移量. 2.offsetof宏实 ...
- Lodash之throttle(节流)与debounce(防抖)总结
全手打原创,转载请标明出处:https://www.cnblogs.com/dreamsqin/p/11305028.html 先重点说一下可能遇到的坑:主要在原本默认参数的设置以及两个方法的选择上, ...
- 【题解】Sonya and Matrix Beauty [Codeforces1080E]
[题解]Sonya and Matrix Beauty [Codeforces1080E] 传送门:\(Sonya\) \(and\) \(Matrix\) \(Beauty\) \([CF1080E ...
- Java学习:线程实现方式
线程实现方式 并发与并行 并发:指两或多个事件在同一个时间段内发生 并行:指两或多个事件在同一个时刻发生(同时发生) 进程的概念 内存:所有的应用程序都需要进入到内存中执行 临时存储RAM 硬盘:永久 ...
- Replication:distribution 中一直在运行 waitfor delay @strdelaytime 语句
Replication 自动创建来一个 Job:Replication monitoring refresher for distribution,这个Agent执行一个sp: dbo.sp_repl ...
- SQL server数据库创建代码,filegroup文件组修改,
以下示例在 SQL Server 实例上创建了一个数据库.该数据库包括一个主数据文件.一个用户定义文件组和一个日志文件.主数据文件在主文件组中,而用户定义文件组包含两个次要数据文件.ALTER DAT ...
- 3DESC加密算法
3DESC 请求参数和响应参数全采用3des加密规则,由于我是用.NET对接的,而第三方是Java开发的,所以两种程序之间采用的算法有一点差异,java的3des加密采用的是"DESede/ ...
- 2019 中钢网java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.中钢网等公司offer,岗位是Java后端开发,因为发展原因最终选择去了中钢网,入职一年时间了,也成为了面试官 ...
- Spring Boot 企业级应用开发实战 刘伟东-2018年3月第一版
Spring会自动搜索某些路径下的Java类 并将这些类注册微Bean实例,这样就省去了所有Bean都配置在XML的麻烦 Spring会适当地将显示指定路径下的的类全部注册微Spring Bean . ...
- ElasticSearch 中文分词搜索环境搭建
ElasticSearch 是强大的搜索工具,并且是ELK套件的重要组成部分 好记性不如乱笔头,这次是在windows环境下搭建es中文分词搜索测试环境,步骤如下 1.安装jdk1.8,配置好环境变量 ...