springboot项目启动后执行方法的三种方式

目录
  • 1 方法
    • 方法1:spring的ApplicationListener< ContextRefreshedEvent>接口
    • 方法2:springboot的ApplicationRunner接口
    • 方法3:springboot的CommandLineRunner接口
  • 2 指定执行顺序
  • 3 原理

springboot项目启动后执行方法,有三种实现方式。

1 方法

  • ApplicationListener< ContextRefreshedEvent> 不推荐
  • ApplicationListener  推荐
  • CommandLineRunner 推荐

方法1:spring的ApplicationListener< ContextRefreshedEvent>接口

实现ApplicationListener接口,并实现 onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent)方法

@Service
public class SearchReceive implements  ApplicationListener<ContextRefreshedEvent> {
   @Override
   public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
       if (contextRefreshedEvent.getApplicationContext().getParent() == null) {//保证只执行一次
           //需要执行的方法
       }
   }
}

方法2:springboot的ApplicationRunner接口

ApplicationListener和CommandLineRunner两个接口是springBoot提供用来在spring容器加载完成后执行指定方法。两个接口区别主要是入参不同。

实现ApplicationRunner接口

@Component
@Order(value = 1)
public class AfterRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("执行方法");
    }
}

方法3:springboot的CommandLineRunner接口

实现CommandLineRunner接口

@Component
@Order(value = 2)
public class CommandLineRunnerImpl implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("执行方法");
    }
}

注:如果同时implements   ApplicationListener和CommandLineRunner两个接口,ApplicationRunner接口的方法先执行,CommandLineRunner后执行;

@Slf4j
@Component
public class RunnerTest implements ApplicationRunner, CommandLineRunner {

  @Override
  public void run(ApplicationArguments args) throws Exception {
    System.out.println("服务启动RunnerTest   ApplicationRunner执行启动加载任务...");
  }

  @Override
  public void run(String... args) throws Exception {
    System.out.println("服务启动RunnerTest    CommandLineRunner 执行启动加载任务...");
    }
  }
}

2 指定执行顺序

当项目中同时实现了ApplicationRunner和CommondLineRunner接口时,可使用Order注解或实现Ordered接口来指定执行顺序,值越小越先执行。

3 原理

SpringApplication 的run方法会执行afterRefresh方法。

afterRefresh方法会执行callRunners方法。

callRunners方法会调用所有实现ApplicationRunner和CommondLineRunner接口的方法。

到此这篇关于springboot项目启动后执行方法的三种方式的文章就介绍到这了,更多相关springboot启动后执行方法内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Springboot启动执行特定代码的方式汇总

    目录 实现InitializingBean接口或使用@PostConstruct注解 实现ApplicationListener接口 实现CommandLineRunner或ApplicationRunner 接口 实现InitializingBean接口或使用@PostConstruct注解 实现InitializingBean如下 public class AnotherExampleBean implements InitializingBean { @Override public vo

  • springboot 项目容器启动后如何自动执行指定方法

    目录 springboot 项目容器启动后自动执行指定 springboot 容器及启动过程 问题1:为什么要启动父子两个容器? 问题2:在什么时候启动父容器? 问题3:父容器和子容器的区别? 问题4:怎么保证父容器启动过程中 问题5:容器实际通过什么来管理bean springboot 项目容器启动后自动执行指定 我们需要写一个class去让它实现ApplicationRunner,然后重写它的run方法就行了,注意类加上@Component注解 注意:这个class需要写在和Applicat

  • springboot启动前执行方法的四种方式总结

    目录 第一种  @PostConstruct注解 第二种  实现InitializingBean接口 第三种 实现BeanPostProcessor接口 第四种  在启动类run之前执行方法 总结 第一种  @PostConstruct注解 @Configuration public class Test1 { @Autowired private Environment environment; @PostConstruct public void test(){ String propert

  • springBoot启动时让方法自动执行的几种实现方式

    在springBoot中我们有时候需要让项目在启动时提前加载相应的数据或者执行某个方法,那么实现提前加载的方式有哪些呢?接下来我带领大家逐个解答 1.实现ServletContextAware接口并重写其setServletContext方法 @Component public class TestStarted implements ServletContextAware { /** * 在填充普通bean属性之后但在初始化之前调用 * 类似于initializingbean的afterpro

  • SpringBoot启动时自动执行代码的几种实现方式

    目录 前言 java自身的启动时加载方式 static代码块 构造方法 Spring启动时加载方式 代码测试 总结 前言 目前开发的SpringBoot项目在启动的时候需要预加载一些资源.而如何实现启动过程中执行代码,或启动成功后执行,是有很多种方式可以选择,我们可以在static代码块中实现,也可以在构造方法里实现,也可以使用@PostConstruct注解实现. 当然也可以去实现Spring的ApplicationRunner与CommandLineRunner接口去实现启动后运行的功能.在

  • springboot项目启动后执行方法的三种方式

    目录 1 方法 方法1:spring的ApplicationListener< ContextRefreshedEvent>接口 方法2:springboot的ApplicationRunner接口 方法3:springboot的CommandLineRunner接口 2 指定执行顺序 3 原理 springboot项目启动后执行方法,有三种实现方式. 1 方法 ApplicationListener< ContextRefreshedEvent> 不推荐 ApplicationL

  • SpringBoot 在项目启动之后执行自定义方法的两种方式小结

    目录 SpringBoot 项目启动之后执行自定义方法的两种方式 方式一 实现 CommandLineRunner 接口 方式二 实现 ApplicationRunner 接口 二者区别 Springboot 项目启动后执行某些自定义代码 SpringBoot 项目启动之后执行自定义方法的两种方式 在测试配置中心的配置时,想在项目启动成功之后打印配置项,然后需要执行自定义的类 一般项目中也会在这个地方进行初始化数据的一些操作 方式一 实现 CommandLineRunner 接口 自定义类并实现

  • Spring Boot 项目启动自动执行方法的两种实现方式

    目录 实际应用场景: 第一种实现ApplicationRunner接口 第二种实现CommandLineRunner接口 对比: 注意: 实际应用场景: springboot项目启动成功后执行一段代码,如系统常量,配置.代码集等等初始化操作:执行多个方法时,执行顺序使用Order注解或Order接口来控制. Springboot给我们提供了两种方式 第一种实现ApplicationRunner接口 package org.mundo.demo.core; import org.springfra

  • Springboot启动后立即某个执行方法的四种方式

    目录 注解@PostConstruct CommandLineRunner接口 实现ApplicationRunner接口 实现ApplicationListener 四种方式的执行顺序 总结 最新需要在项目启动后立即执行某个方法,然后特此记录下找到的四种方式 注解@PostConstruct 使用注解@PostConstruct是最常见的一种方式,存在的问题是如果执行的方法耗时过长,会导致项目在方法执行期间无法提供服务. @Component public class StartInit {

  • 解决Springboot项目启动后自动创建多表关联的数据库与表的方案

    熬夜写完,尚有不足,但仍在努力学习与总结中,而您的点赞与关注,是对我最大的鼓励! 在一些本地化项目开发当中,存在这样一种需求,即开发完成的项目,在第一次部署启动时,需能自行构建系统需要的数据库及其对应的数据库表. 若要解决这类需求,其实现在已有不少开源框架都能实现自动生成数据库表,如mybatis plus.spring JPA等,但您是否有想过,若要自行构建一套更为复杂的表结构时,这种开源框架是否也能满足呢,若满足不了话,又该如何才能实现呢? 我在前面写过一篇 Activiti工作流学习笔记(

  • 解决SpringBoot项目启动后网页显示Please sign in的问题

    Springboot启动项目后网页显示[Please sign in] 遇到的情况解决办法解决效果根本原因(依赖导错了)根本解决办法 遇到的情况 启动SpringBoot后,访问http://127.0.0.1:8080/t02/index,确莫名其妙的进入到了Please sign in页面. 解决办法 仔细看了下idea控制台的信息,发现出现了一个security password,原来是进入到了一个安全拦截界面,我们输入idea控制台打印的密码即可,username是user. 解决效果

  • Java项目开发中实现分页的三种方式总结

    目录 前言 使用 1.SpringDataJPA分页 2.MyBatis分页 3.Hutools工具类分页 总结 前言 Java项目开发中经常要用到分页功能,现在普遍使用SpringBoot进行快速开发,而数据层主要整合SpringDataJPA和MyBatis两种框架,这两种框架都提供了相应的分页工具,使用方式也很简单,可本人在工作中除此以外还用到第三种更方便灵活的分页方式,在这里一同分享给大家. 使用 主要分为SpringDataJPA分页.MyBatis分页.Hutools工具类分页几个部

  • Java实现指定线程执行顺序的三种方式示例

    本文实例讲述了Java实现指定线程执行顺序的三种方式.分享给大家供大家参考,具体如下: 方法一:通过共享对象锁加上可见变量来实现. public class MyService { private volatile int orderNum = 1; public synchronized void methodA() { try { while (orderNum != 1) { wait(); } for (int i = 0; i < 2; i++) { System.out.printl

  • 详解SpringBoot修改启动端口server.port的四种方式

    方式一: 配置文件 application.properties server.port=7788 方式二: java启动命令 # 以应用参数的方式 java -jar <path/to/my/jar> --server.port=7788 # 或以 JDK 参数的方式 java -Dserver.port=7788 -jar <path/to/my/jar> 方式三: 环境变量 SERVER_PORT Linux: SERVER_PORT=7788 java -jar <p

随机推荐