首页 > 其他分享 >详谈SpringBoot启动项目后执行自定义方法的方式

详谈SpringBoot启动项目后执行自定义方法的方式

时间:2023-09-08 11:07:07浏览次数:35  
标签:CommandLineRunner SpringBoot 自定义 args class 详谈 ApplicationRunner public Applicat


在 main 启动函数中调用

这个是在所有启动后执行,也是常用之一。

@SpringBootApplication
public class ListenerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ListenerApplication.class, args);
        System.out.println("启动成功");
    }
}

实现 CommandLineRunner 接口

项目初始化完毕后才会调用方法,提供服务。好处是方法执行时,项目已经初始化完毕,是可以正常提供服务的。

@Component
public class CommandLineRunnerImpl implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println(Arrays.toString(args));
    }
}

实现 ApplicationRunner 接口

实现 ApplicationRunner 接口和实现 CommandLineRunner 接口基本是一样的。不同的是启动时传参的格式,CommandLineRunner 对于参数格式没有任何限制。

ApplicationRunner 接口参数格式必须是:key=value

@Component
public class ApplicationRunnerImpl implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Set<String> optionNames = args.getOptionNames();
        for (String optionName : optionNames) {
            List<String> values = args.getOptionValues(optionName);
            System.out.println(values.toString());
        }
    }
}

注意

CommandLineRunner 和 ApplicationRunner 默认是 ApplicationRunner 先执行,如果双方指定了@Order 则按照 @Order 的大小顺序执行,大的先执行。

实现 ApplicationListener 接口

实现 ApplicationListener 接口和实现 ApplicationRunner、CommandLineRunner 接口基本差不多,项目初始化完毕后才会调用方法,提供服务。

@Component
public class ApplicationListenerImpl implements ApplicationListener<ApplicationStartedEvent> {

    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        System.out.println("ApplicationStartedEvent");
    }

}

当以 @Component 方式配置时,事件触发顺序如下:

ApplicationListener<ServletWebServerInitializedEvent>
ApplicationListener<ContextRefreshedEvent>
ApplicationListener<ApplicationStartedEvent>
ApplicationListener<ApplicationReadyEvent>
ApplicationListener<ContextClosedEvent>

当通过 /META-INF/spring.factories 配置时,事件触发顺序如下:

org.springframework.context.ApplicationListener=com.xh.event.ApplicationListenerImpl

ApplicationListener<ApplicationStartingEvent>
ApplicationListener<ApplicationEnvironmentPreparedEvent>
ApplicationListener<ApplicationContextInitializedEvent>
ApplicationListener<ApplicationPreparedEvent>
ApplicationListener<ServletWebServerInitializedEvent>
ApplicationListener<ContextRefreshedEvent>
ApplicationListener<ApplicationStartedEvent>
ApplicationListener<ApplicationReadyEvent>
ApplicationListener<ContextClosedEvent>

注意 

  • 如果监听的是 ServletWebServerInitializedEvent、ContextRefreshedEvent、ApplicationStartedEvent 事件,则 ApplicationListener 会在 CommandLineRunner 和 ApplicationRunner 之前执行
  • 如果监听的是 ApplicationReadyEvent 事件,则 ApplicationListener 会在 CommandLineRunner 和 ApplicationRunner 之后执行
  • ContextClosedEvent 当调用关闭方法的时候,才会触发了 ContextClosedEvent 事件

实现 InitializingBean 接口

项目启动时,调用此方法,在 ServletWebServerInitializedEvent 事件前触发。

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

@PostConstruct 注解

使用注解 @PostConstruct 实现,不推荐使用。如果执行的方法耗时过长,会导致服务无法提供服务。

@Component
public class StartBoot {

    @PostConstruct
    public void init() throws InterruptedException {
        System.out.println("@PostConstruct");
    }
}

标签:CommandLineRunner,SpringBoot,自定义,args,class,详谈,ApplicationRunner,public,Applicat
From: https://blog.51cto.com/u_15856116/7407603

相关文章

  • springboot - idea - active: @profileActive@ 有时候 不识别 @ 导致启动失败
    1.背景有时候正常,有时候不行,特别是maven执行了clean命令后 2.解决右键执行一下这个即可 ......
  • pageoffice 6版本隐藏office工具栏和自定义按钮,并修改标题栏内容
    在实际项目集成调用PageOffice的过程中:(1)有时需要把Office的工具栏隐藏,比如只读模式打开文件的时候,Office工具栏上的按钮几乎都是灰掉的,此时显示Office工具栏没有任何意义,并且浪费了宝贵的界面空间。实现此功能只需在OnPageOfficeCtrlInit事件中调用js设置PageOffice控件的Office......
  • springboot策略模式
    一.定义接口publicinterfacePearlTaskService{IntegergetTaskType();Map<String,Integer>execute(LonguserId,GameTaskgameTask,StringgameCode);}二.定义抽象类@Slf4jpublicabstractclassPearlTaskStrategyimplementsPearlTaskService{protec......
  • 详谈Java中抽象类和接口的区别和使用场景
    抽象类和接口的区别抽象类和接口都是Java中实现多态的重要手段,但它们之间有很大的区别:抽象类是一个类,可以拥有成员变量和非抽象方法,而接口只能拥有常量和抽象方法。子类继承抽象类时,必须实现父类的抽象方法,而接口则需要实现所有定义的方法。一个类只能继承一个抽象类,但可以实现多个......
  • SpringBoot整合thymeleaf
    JavaEE领域有几种常用的模板引擎:Jsp,Thymeleaf,Freemarker,Velocity等.对于前端页面渲染效率来说JSP其实还是最快的,Velocity次之.Thymeleaf虽然渲染效率不是很快,但语法比较轻巧.Thymeleaf支持html5标准,Thymeleaf页面无需部署到servlet开发到服务器上,以.html后缀结......
  • springboot随项目启动,实时监控日志文件并进行操作
    项目中用到了一个开源音视频服务,但是同事的服务有可能导致开源服务崩溃,所以就写了一个实时监控开源服务输出日志的服务,如果日志中有error信息的话就自动重启那个开源服务。不过后来还是在项目中把这部分屏蔽了。 1@Component2publicclassFileWatcherRunnerimplem......
  • 【ROS2机器人入门到实战】控制OLED-自定义消息接口
    1.控制OLED-自定义消息接口写在前面当前平台文章汇总地址:ROS2机器人从入门到实战获取完整教程及配套资料代码,请关注公众号<鱼香ROS>获取教程配套机器人开发平台:两驱版|四驱版为方便交流,搭建了机器人技术问答社区:地址fishros.org.cn你好,我是爱吃鱼香ROS的小鱼。前面章节中我们使用......
  • springboot 服务端接口公网远程调试﹣实现 HTTP 服务监听【端口映射】
    前言前后端分离项目中,在调用接口调试时候,我们可以通过cpolar内网穿透将本地服务端接口模拟公共网络环境远程调用调试,本次教程我们以Java服务端接口为例。1.本地环境搭建1.1环境参数JDK1.8IDEASpringBootMavenTomcat9.0Postman1.2搭建springboot服务项目搭建一个springboot服务的......
  • springboot打包运行失败
    没有包含启动类的class添加mavenpom.xml;`org.springframework.bootspring-boot-maven-plugin${spring-boot.version}com.hidisp.HidispApplication<!--issue:SpringBoot-0.0.1-SNAPSHOT.jar中没有主清单属性 resolve:①注释掉,或者②将skip值改为......
  • springboot项目自动运行脚本
    注意文件格式unix格式(Windowscrlf换行符有不可见字符)#!/bin/sh#服务名(要与配置文件中的server名保持一致)APP_NAME=""#git本地仓库路径GIT_RESPOSITORY=""#配置文件存储位置PROFILE_LOCATION=""#配置文件名PROFILE_NAME=""#日志文件存储位置LOG_LOCATION="......