sprongboot项目启动初始化加载方式

  • ApplicationRunner与CommandLineRunner接口
  • Spring Bean初始化的构造方法、PostConstruct、init-method、InitializingBean

1、ApplicationRunner与CommandLineRunner
如果需要在SpringApplication启动时执行一些特殊的代码,你可以实现ApplicationRunner或CommandLineRunner接口,这两个接口工作方式相同,都只提供单一的run方法,而且该方法仅在SpringApplication.run(…)完成之前调用,更准确的说是在构造SpringApplication实例完成之后调用run()的时候。

  • ApplicationRunner
@Component
public class ApplicationRunnerTest implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) throws Exception {System.out.println("ApplicationRunner");}
}
  • CommandLineRunner

Order注解或者使用Ordered接口来指定调用顺序,@Order()中的值越小,优先级越高

@Component
@Order(1)
public class CommandLineRunnerTest implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("CommandLineRunner...");}
}

2、Spring Bean初始化的构造方法、PostConstruct、init-method、InitializingBean

  • 构造器注入
@Component
public class LogicInConstructorExampleBean {private static final Logger LOG = Logger.getLogger(LogicInConstructorExampleBean.class);private final Environment environment;@Autowiredpublic LogicInConstructorExampleBean(Environment environment) {//environment实例已初始化this.environment = environment;LOG.info(Arrays.asList(environment.getDefaultProfiles()));}
}
  • @PostConstruct

@PostConstruct注解的方法,会在构造方法之后执行,顺序为Constructor > @Autowired > @PostConstruct > 静态方法

@Component
public class PostConstructExampleBean {private static final Logger LOG = Logger.getLogger(PostConstructExampleBean.class);@Autowiredprivate Environment environment;@PostConstructpublic void init() {//environment 已经注入LOG.info(Arrays.asList(environment.getDefaultProfiles()));}
}
  • InitializingBean

Bean在实例化的过程中执执行顺序为:Constructor > @PostConstruct > InitializingBean > init-method

InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet()方法。在spring初始化bean的时候,如果bean实现了InitializingBean接口,在对象的所有属性被初始化后之后才会调用afterPropertiesSet()方法。spring初始化bean肯定会在ApplicationRunner和CommandLineRunner接口调用之前

@Component
public class InitialingzingBeanTest implements InitializingBean {@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("InitializingBean..");}
}

尽管使用initialingBean接口可以实现初始化动作,但是官方并不建议我们使用InitializingBean接口,因为它将你的代码耦合在Spring代码中,官方的建议是在bean的配置文件指定init-method方法,或者在@Bean中设置init-method属性。

  • @Bean initMethod方法
@Bean(initMethod="init")
public InitMethodExampleBean exBean() {return new InitMethodExampleBean();
}
public class InitMethodExampleBean {private static final Logger LOG = Logger.getLogger(InitMethodExampleBean.class);@Autowiredprivate Environment environment;public void init() {LOG.info(Arrays.asList(environment.getDefaultProfiles()));}
}

初始化的顺序为总结:

  1. 构造器方法
  2. @PostConstruct 注解方法
  3. InitializingBean的afterPropertiesSet()
  4. Bean定义的initMethod属性方法

Springboot启动初始化相关推荐

  1. springboot启动时初始化sql脚本

    文章目录 1.初始化mysql数据库脚本 1.使用springboot jdbc初始化数据库 2.使用原生mybatis执行sql脚本 3.改良springboot jdbc初始化数据库 参考地址 基 ...

  2. SpringBoot启动流程分析(四):IoC容器的初始化过程

    SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...

  3. Springboot 启动时Bean初始化,启动异常-Assert.isTrue(condition,message) 报错

    Springboot 启动时Bean初始化启动异常Assert.isTrue(condition,message) 报错,如果 condition为false 则会出现 java.lang.Illeg ...

  4. springboot启动源码解析(三):初始化启动上下文、初始化监听器列表、发布开始启动事件

    此章节主要对springboot启动过程中,发生的[初始化启动上下文].[初始化监听器列表].[发布springboot开始启动事件]进行源码解析,对应的代码如图1所示: 图1: // 首先初始化一个 ...

  5. Springboot启动原理解析

    点击上方"方志朋",选择"置顶公众号" 技术文章第一时间送达! 我们开发任何一个Spring Boot项目,都会用到如下的启动类 @SpringBootAppl ...

  6. 手把手带你剖析 Springboot 启动原理!

    作者:平凡希 cnblogs.com/xiaoxi/p/7999885.html 我们开发任何一个Spring Boot项目,都会用到如下的启动类 @SpringBootApplication pub ...

  7. spring-boot启动源码学习-1

    2019独角兽企业重金招聘Python工程师标准>>> spring-boot启动源码分析-启动初始化 主要对spring-boot的启动流程中的启动初始化进行学习,学习spring ...

  8. springboot启动过程_不要搞笑哈,你用了5年的SpringBoot框架,竟然不了解它的启动过程?...

    SpringBoot的启动很简单,代码如下: @SpringBootApplicationpublic class MyApplication { public static void main(St ...

  9. SpringBoot启动过程详解

    Spring Boot通常有一个名为*Application的入口类,在入口类里有一个main方法,这个main方法其实就是一个标准的java应用的入口方法. 在main方法中使用SpringAppl ...

最新文章

  1. JavaIO操作(1)字节流和字符流-1
  2. base64是哪个jar包的_如何通过一个类名找到它属于哪个jar包?
  3. python 清空list 方法
  4. 从零开始做Vue前端架构(5)
  5. 关于 Method Swizzing方法
  6. libevent源码深度剖析九
  7. 2017年网络犯罪现状分析报告
  8. 小型电商Web架构!小而美!值得学习!
  9. css+javascript模拟OneNote2007Tab标签
  10. 应用密码学:位移密码极简(凯撒密码)
  11. 【一步步学OpenGL 20】 -《点光源》
  12. gitlab hook declined错误
  13. 怎么用微信打开链接才不会提示已停止访问网页
  14. armbian 安装python3
  15. day---06 文件的操作
  16. Java学习之while循环及案例
  17. 04 关于热血篮球的一个观点
  18. window下Java诊断工具arthas 实操心得
  19. 简要理解什么是趋肤效应
  20. 协方差矩阵的模拟及独立性、相关性的判断

热门文章

  1. uni.app小程序登录页持久化存储和退出后清除本地缓存
  2. 控制系统中的AI、AO、DI、DO是什么意思——控制系统基础知识
  3. SCP Linux远程下载命令
  4. MySQL连接远程数据库(使用Navicat工具)
  5. IAP( 应用程序內购买): 完全攻略
  6. 使用mpvue开发小程序教程(三)
  7. 网页服务器慢怎么设置兼容,网站打开网页很慢要怎么办?打网页慢怎么回事?
  8. 语音信号分析之FFT变换过程
  9. Vue使用v-for与v-if搭配满足条件进行赋值,和v-if三目表达式的使用
  10. python列表导出excel_python list格式数据excel导出方法