结论:

/**
* step1:执行构造函数
* step2:执行使用@PostConstruct注解修饰的方法【如果有多个,则执行顺序不确定】
* step3:执行@Bean注解中initMethod指定的方法
*/
示例代码:
@Slf4j
public class InitBean { public InitBean() {
log.info("Step1:begin to execute Constructor Function");
} public void initMethod() {
log.info("Step3:begin to execute initMethod");
} @PostConstruct
public void postConstructMethod1() {
log.info("Step2:begin to execute postConstructMethod1 with @PostConstruct");
} @PostConstruct
public void postConstructMethod2() {
log.info("Step2:begin to execute postConstructMethod2 with @PostConstruct");
} }

Java Config方式进行Bean的初始化:

@Configuration
public class InitBeanConfig { @Bean(initMethod = "initMethod")
public InitBean initBean() {
return new InitBean();
} }
测试用例:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = InitBeanConfig.class)
public class InitBeanConfigTest { @Test
public void givenBean_WhenInit_thenFirstConstructorFunction_SecondPostConstructAnna_ThirdInitMethod() {
/**
* step1:执行构造函数
* step2:执行使用@PostConstruct注解修饰的方法
* step3:执行@Bean注解中initMethod指定的方法
*/
} }
执行结果:
2019-01-06 22:16:03.362 INFO 21944 --- [main] c.t.learning.init.spring.InitBean : Step1:begin to execute Constructor Function
2019-01-06 22:16:03.377 INFO 21944 --- [main] c.t.learning.init.spring.InitBean : Step2:begin to execute postConstructMethod1 with @PostConstruct
2019-01-06 22:16:03.378 INFO 21944 --- [main] c.t.learning.init.spring.InitBean : Step2:begin to execute postConstructMethod2 with @PostConstruct
2019-01-06 22:16:03.378 INFO 21944 --- [main] c.t.learning.init.spring.InitBean : Step3:begin to execute initMethod
2019-01-06 22:16:03.461 INFO 21944 --- [main] c.t.l.init.spring.InitBeanConfigTest: Started InitBeanConfigTest in 3.548 seconds (JVM running for 5.781)

温馨提示: 如果存在多个使用注解@PostConstruct修饰的方法,则这些方法执行顺序不确定

代码:
https://github.com/helloworldtang/spring-boot-cookbook/blob/v1.0.2-thymeleaf/learning-demo/src/main/java/com/tangcheng/learning/init/spring/InitBean.java

哪个先执行:@PostConstruct和@Bean的initMethod?的更多相关文章

  1. 【Java】利用反射执行Spring容器Bean指定的方法,支持多种参数自动调用

    目录 使用情景 目的 实现方式 前提: 思路 核心类 测试方法 源码分享 使用情景 将定时任务录入数据库(这样做的好处是定时任务可视化,也可以动态修改各个任务的执行时间),通过反射执行对应的方法: 配 ...

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

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

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

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

  4. Spring Bean初始化之后执行指定方法

    转: Spring Bean初始化之后执行指定方法 2017年07月31日 15:59:33 vircens 阅读数:24807   Spring Bean初始化之后执行指定方法 在运用Spring进 ...

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

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

  6. spring初始化bean时执行某些方法完成特定的初始化操作

    在项目中经常会在容器启动时,完成特定的初始化操作,如资源文件的加载等. 一 实现的方式有三种: 1.使用@PostConstruct注解,该注解作用于void方法上 2.在配置文件中配置init-me ...

  7. Spring学习(十二)-----Spring Bean init-method 和 destroy-method实例

    实现 初始化方法和销毁方法3种方式: 实现标识接口 InitializingBean,DisposableBean(不推荐使用,耦合性太高) 设置bean属性 Init-method destroy- ...

  8. 003-Spring4 扩展分析-spring类初始化@PostConstruct > InitializingBean > init-method、ApplicationContext、BeanPostProcessor、BeanFactoryPostProcessor、BeanDefinitionRegistryPostProcessor

    一.spring类初始化@PostConstruct > InitializingBean > init-method InitializingBean接口为bean提供了初始化方法的方式 ...

  9. @PostConstruct - 静态方法调用IOC容器Bean对象

    需求:工具类里面引用IOC容器Bean,强迫症患者在调用工具类时喜欢用静态方法的方式而非注入的方式去调用,但是spring 不支持注解注入静态成员变量. 静态变量/类变量不是对象的属性,而是一个类的属 ...

随机推荐

  1. Mongodb 与 SQL 语句对照表

    In addition to the charts that follow, you might want to consider the Frequently Asked Questions sec ...

  2. Mina集成Spring --- 在配置文件中配置sessionconfig

    这个找了很久,一直想用这个功能,在xml里,配置如下: <?xml version="1.0" encoding="UTF-8"?> <bea ...

  3. 在Windows 7上安装Team Foundation Server(TFS)的代理服务器(Agent)

    自2009年微软发布Windows 7以来,经过8年的市场验证,Windows 7已经成为史上应用最为广泛的操作系统.但是面对技术变化的日新月异,2015年微软正式停止了对Windows 7的主流支持 ...

  4. Python 数据结构与算法——桶排序

    #简单的桶排序 def bucksort(A): bucks = dict() # 定义一个桶变量,类型为字典 for i in A: bucks.setdefault(i,[]) # 每个桶默认为空 ...

  5. xt

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. 如何利用Python绘制一个爱心

    刚学习Python几周,闲来无事,突然想尝试画一个爱心,步骤如下: 打开界面 打开Python shell界面,具体是Python语言的IDLE软件脚本. 2.建立脚本 单击左上角’File’,再单击 ...

  7. SSM集成Easyui框架及多模块开发的认识

    首先我们需要建立好一个emaven项目,并且在pom.xml中导入响应的jar包, <?xml version="1.0" encoding="UTF-8" ...

  8. list页面-按照choice筛选丶传condition过滤筛选项丶筛选与显示同步

    1.list页面:筛选功能优化,显示choices,传condition过滤筛选项 list_filter = [     # sites.FilterOption("name", ...

  9. django 视图中执行原生的 sql 查询语句

    可以使用objects的raw()方法执行原生的sql语句,进行对数据库的查询操作,raw()方法只能执行查询语句 query_set = your_model.objects.raw("s ...

  10. Reservoir Sampling-382. Linked List Random Node

    Given a singly linked list, return a random node's value from the linked list. Each node must have t ...