springboot项目启动成功后执行一段代码的两种方式

springboot项目启动成功后执行一段代码的两种方式

实现ApplicationRunner接口

package com.lnjecit.lifecycle;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author lnj
 * createTime 2018-11-07 22:37
 **/
@Component
public class ApplicationRunnerImpl implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("通过实现ApplicationRunner接口,在spring boot项目启动后打印参数");
        String[] sourceArgs = args.getSourceArgs();
        for (String arg : sourceArgs) {
            System.out.print(arg + " ");
        }
        System.out.println();
    }
}

项目启动后,会打印如下信息:

实现CommandLineRunner接口

package com.lnjecit.lifecycle;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * @author lnj
 * createTime 2018-11-07 22:25
 **/
@Component
public class CommandLineRunnerImpl implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("通过实现CommandLineRunner接口,在spring boot项目启动后打印参数");
        for (String arg : args) {
            System.out.print(arg + " ");
        }
        System.out.println();
    }
}

两种实现方式的不同之处在于run方法中接收的参数类型不一样

指定执行顺序

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

案例地址

https://github.com/linj6/springboot-learn/tree/master/springboot-runner

参考资料

https://blog.csdn.net/zknxx/article/details/52196427

原文地址:https://www.cnblogs.com/zuidongfeng/p/9926471.html

时间: 2024-12-23 12:14:57

springboot项目启动成功后执行一段代码的两种方式的相关文章

SpringBoot项目启动成功,访问路径提示404

当SpringBoot项目启动成功后,访问Controller下的RequestMapping路径却发现无法访问,且提示Status=404 我的项目中controller下只有一个访问路径/hello,正常情况下当项目启动成功,会在控制台看到项目启动时扫描到该路径:@RequestMapping(value="/hello") ;但是我们看控制台此时并没有扫描到/hello路径 /hello路径无法成功访问 到这里就需要检查你的SpringBoot启动类是否与你的Controoler

执行xxx.sh脚本的两种方式

因公司测试环境的登录模式有2种,大佬们直接写了个脚本完成一键切换,看了其中的脚本文件,其中出现了send "sh out.sh\r":一直疑惑这里的sh out.sh的意思...查了资料才发现也是执行脚本,但与./执行脚本有些区别!!! 一.使用./执行脚本 前提:要求对应的.sh文件需有执行权限.如下图,需给文件赋予执行权限才能使用./来执行. 二.使用sh执行脚本 区别:.sh文件没有执行权限时,使用sh来执行.sh文件也可执行成功. 原文地址:https://www.cnblog

项目部署到tomcat6.0启动成功后访问页面报500错误解决方法

如题:项目部署到tomcat6.0启动成功后访问页面报500错误解决方法,很奇葩,启动的时候没有任何问题,但输入访问地址后报500,去年国庆放假前夕,为这个问题伤神了半天最后解决了,今天又碰到了,乍一看摸不着头,后面仔细回想了下,迅速解决了问题. 原因:项目里面的jar和tomcat里面lib里面的jar重复了. 解决方法: 1.要么更换tomcat7或之后的版本,之后的版本就不会出现这个问题. 2.去部署之后的tomcat的webapps\项目名\WEB-INF\lib 里面删掉jsp-api

Spring boot启动成功后输出提示

添加logback-spring.xml,将log输出到文件,控制台输出的level改为error因此只会出处banner src/main/resources/banner.txt的内容为 start... 但是输出完banner后,spring boot并没有启动完毕 因此,我想在Spring boot启动成功后输出提示 有两种方式 1.实现 ApplicationRunnerImpl eg: package com.example.demo.configure; import org.sp

在web项目启动时,执行某个方法

在web项目中有很多时候需要在项目启动时就执行一些方法,而且只需要执行一次,比如:加载解析自定义的配置文件.初始化数据库信息等等,在项目启动时就直接执行一些方法,可以减少很多繁琐的操作. 在工作中遇到了项目初始数据需要跟其他项目同步的问题,也就是说在项目部署后,启动的时候就要同步另外一个项目的数据,这里写了个简单的实例,用的是监听器机制,创建一个类实现ServletContextListener 接口,实现里面的contextInitialized和contextDestroyed方法. pac

springboot项目启动之后初始化自定义配置类

前言 今天在写项目的时候,需要再springboot项目启动之后,加载我自定义的配置类的一些方法,百度了之后特此记录下. 正文 方法有两种: 1. 创建自定义类实现 CommandLineRunner接口,重写run()方法.springboot启动之后会默认去扫描所有实现了CommandLineRunner的类,并运行其run()方法. @Component @Order(2) //通过order值的大小来决定启动的顺序 public class AskForLeave implements

第一个springboot项目启动报错Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

报错内容具体如下 *************************** APPLICATION FAILED TO START *************************** Description: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Failed to determine a

web项目启动时,执行某个方法

1.监听(Listener) web文件添加 <listener> <listener-class>cn.ro.common.InitListener</listener-class> </listener> 添加InitListener类,如下 package cn.ro.common; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener

springboot项目启动报错 url&#39; attribute is not specified and no embedded datasource could be configured

报错相关信息: 2019-07-22 17:12:48.971 ERROR 8312 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Failed to configure a DataSource: 'url' attribute is not