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 ...
随机推荐
- laravel如何引用外部文件
(1).首先在app\Http\routes.php中定义路由: 1 2 3 Route::get('view','ViewController@view'); Route::get('article ...
- SQL Server ----- 生成sql 脚本
通过生成sql 脚本进行数据库转移 选中需要进行转移的数据库 点击生成脚本后出现 进入的是简介界面 直接点击下一步 进入到选择对象界面. 按照步骤进行设置 选择你需要的 架构和数据 ...
- MySQL 索引小结
1.!=.not in 在primary key上使用 !=.not in,explain 的 type 是 range,非primary key是全表扫描(即非主键字段即使有索引也无法应用) 2.a ...
- [转帖]华为PC端台式机电脑来啦!自研主板及自研CPU处理器
华为PC端台式机电脑来啦!自研主板及自研CPU处理器 在性能上,4核版相当于酷睿i5 ,8核版相当于酷睿i5 8300H. https://www.bilibili.com/read/cv376376 ...
- [转帖]Redis持久化--Redis宕机或者出现意外删库导致数据丢失--解决方案
Redis持久化--Redis宕机或者出现意外删库导致数据丢失--解决方案 https://www.cnblogs.com/xlecho/p/11834011.html echo编辑整理,欢迎转载,转 ...
- 【Mac+Android+uiautomatorviewer】之报错:Unexpected error while obtaining UI hierarchy java.lang.reflect.InvocationTargetException
安卓8.0以后uiautomatorviewer会报错,解决办法如下: 参考:<android 8.0 以后 uiautomator 无法直接使用的问题> 步骤一: 先执行命令:(注意:想 ...
- Java中Integer和ThreadLocal
一. Integer 1.引子 在开始之前,我还是需要吐槽下自己,我是真的很菜! 他问:**两个Integer对象==比较是否相等? 我答:对象==比较,是引用比较,不相等! 他问:IntegerCa ...
- k8s-架构中各个组件介绍
参考链接:https://github.com/opsnull/follow-me-install-kubernetes-cluster kubernetes 概述 1.kubernetes 是什么 ...
- 数据库‘master’中拒绝CREATE DATABASE权限
今天在创建数据库的时候,遇到了没有创建数据库权限的问题,后来百度了一下解决了该问题. 1.先用windows身份验证登录,在安全性下面的找到自己创建的登录名,双击,在弹出的对话框中为它赋予权限. 2. ...
- quota磁盘配额
一.什么是磁盘配额 磁盘配额从字面意思上看就是给一个磁盘配置多少额度,而quota就是有多少限额的意思,所以总的来说就是限制用户对磁盘空间的使用量.因为Linux是多用户多任务的操作系统,许多人公用磁 ...