首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

只让run方法执行(从CommandLineRunner或ApplicationRunner),然后停止,而不启动Spring boot Application

要实现只让run方法执行并停止Spring Boot应用程序,而不启动整个应用程序,可以通过以下步骤来实现:

  1. 创建一个Spring Boot应用程序,并在主类中实现CommandLineRunner或ApplicationRunner接口。
  2. 在run方法中编写需要执行的代码逻辑。
  3. 在run方法的最后,调用System.exit(0)方法来停止应用程序。

下面是一个示例代码:

代码语言:txt
复制
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        // 在这里编写需要执行的代码逻辑

        // 停止应用程序
        System.exit(0);
    }
}

在上述示例中,我们创建了一个名为MyApplication的Spring Boot应用程序,并实现了CommandLineRunner接口。在run方法中,你可以编写需要执行的代码逻辑。最后,通过调用System.exit(0)方法来停止应用程序。

请注意,这种方式只会执行run方法并停止应用程序,不会启动整个Spring Boot应用程序的生命周期。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

CommandLineRunnerApplicationRunner接口的使用及源码解析

org.springframework.boot.ApplicationRunner CommandLineRunnerApplicationRunner 接口是在容器启动成功后的最后一步回调(类似开机自启动...接口被用作将其加入spring容器中时执行run方法。多个CommandLineRunner可以被同时执行在同一个spring上下文中并且执行顺序是以order注解的参数顺序一致。...System.out.println("values===="+applicationArguments.getOptionValues("developer.name")); } } 配置参数,然后执行启动类...扩展阅读 CommandLineRunnerApplicationRunner执行流程源码分析 用户只要实现这两个接口,其中的run方法就会在项目启动时候被自动调用,那么究竟是在什么时候调用的呢?...总结:通过以上分析可知,实现这两个接口的类,在ApplicationContext.run()方法里被执行

1.1K40

重学SpringBoot系列之生命周期内的拦截过滤与监听

但是排除,老项目向spring boot项目迁移融合,需要支持servlet的情况 实现 下面我们就看一下,在spring boot里面如何实现servlet。...深入理解 Spring 事件发布与监听 ---- 应用启动的监听 简介 Spring Boot提供了两个接口:CommandLineRunnerApplicationRunner,用于启动应用时做特殊处理...ApplicationRunner执行顺序,不能跨类保证顺序 ---- 总结 CommandLineRunnerApplicationRunner的核心用法是一致的,就是用于应用启动前的特殊代码执行...ApplicationRunner执行顺序先于CommandLineRunnerApplicationRunner将参数封装成了对象,提供了获取参数名、参数值等方法,操作上会方便一些。...分析一下:下面的代码是SpringBootApplication启动项目之后会执行的代码,大家看代码中通过一个遍历来启动CommandLineRunner或者ApplicationRunner

1.3K20

一张图帮你记忆,Spring Boot 应用在启动阶段执行代码的几种方式

前言 有时候我们需要在应用启动执行一些代码片段,这些片段可能是仅仅是为了记录 log,也可能是在启动时检查与安装证书 ,诸如上述业务要求我们可能会经常碰到 Spring Boot 提供了至少 5 种方式用于在应用启动执行代码...Boot 在应用上下文中找到 CommandLineRunner bean,它将会在应用成功启动之后调用 run() 方法,并传递用于启动应用程序的命令行参数 通过如下 maven 命令生成 jar...在重写的 run() 方法上有 throws Exception 标记,Spring Boot 会将 CommandLineRunner 作为应用启动的一部分,如果运行 run() 方法时抛出 Exception...并且每个参数可以有多个值在里面,因为 getOptionValues 方法返回 List数组 在重写的 run() 方法上有 throws Exception 标记,Spring Boot 会将 CommandLineRunner...作为应用启动的一部分,如果运行 run() 方法时抛出 Exception,应用将会终止启动 ApplicationRunner 也可以使用 @Order 注解进行排序,启动结果来看,它与 CommandLineRunner

1.8K20

Spring 全家桶之 Spring Boot 2.6.4(九)- 启动流程解析

一、Debug Spring Boot 启动流程 创建工程spring-boot-fundamental,添加基本依赖 Debug启动流程,在SpringApplication.run(AppApplication.class...META-INF/spring.factorues配置所有的ApplicationContextInitializer,然后保存起来 执行run()方法 在SpringApplication对象创建完成之后...,开始执行run()方法;重新启动Debug,进入run方法 此时SpringApplication对象已经创建好,run方法中的流程就是Spring Boot启动的流程。...二、Spring Boot 启动流程总结 run方法启动流程: 准备环境 执行ApplicationContextInitializer.initialize() 监听器SpringApplicationRunListener...,耗时:" + timeTaken); } } 实现自定义的ApplicationRunner ApplicationRunner接口包含了一个run()方法 自定义HalloApplicationRunner

68821

spring boot启动过程

启动过程 版本为spring boot 2.0.3 启动 首先在启动类使用main方法运行中进入run方法 springboot的启动类我们一般都会加上SpringBootApplication注解,其实他是几个注解的集合...进入真正调用的run方法继续查看 构造springapplication实例 然后进入构造方法 这里主要做了以下几件事: 判断当前应用的类型,也就是是否是web应用 this.webApplicationType...所以这里SpringApplicationRunListeners的用途和目的也比较明显了,它实际上是一个事件中转器,它能够感知到Spring Boot启动过程中产生的事件,然后有选择性的将事件进行中转...Runners可以是两个接口的实现类 org.springframework.boot.ApplicationRunner org.springframework.boot.CommandLineRunner...SpringApplication实例run方法执行过程 其中主要有一个SpringApplicationRunListeners的概念,它作为Spring Boot容器初始化时各阶段事件的中转器,将事件派发给感兴趣的

2K30

添加 SpringBoot 自定义启动代码的六种方式(上)

那么,怎么 spring 框架在启动执行这些固定的代码呢?事实上,这里面还有不少学问呢。 本文,我们就来详细介绍配置 SpringBoot 启动时动作的六种方法。 2....六种 SpringBoot 启动时运行代码的方法 下面六种方式都可以 SpringBoot 在启动时运行自定义的代码: 实现 CommandLineRunner 接口 实现 ApplicationRunner...只要我们的 bean 实现了这个接口,并覆写了 run 方法,SpringBoot 启动时就会自动调用 run 方法。...与 CommandLineRunner 不同,ApplicationRunnerrun 方法接收的参数是一个 ApplicationArguments 类的对象。...结语 本文,我们介绍了六种 SpringBoot 启动时运行代码的方法,并且详细介绍了其中两个可以获取和处理 spring 启动参数的方法

1K20

springbootapplication运行机制_航空器运行阶段是指什么

启动后阶段 1、SpringApplication准备阶段 本阶段涉及的范围run(String …)方法调用开始,到refreshContext(ConfigurableApplicationContext...3.2、执行ApplicationRunnerCommandLineRunner ApplicationRunnerCommandLineRunner均有run方法,在SpringApplication.run...()方法完成之前执行,其中CommandLineRunner接口提供简单的字符型数组作为参数,ApplicationRunner则使用ApplicationArguments。...当Spring应用上下文出现多个ApplicationRunnerCommandLineRunner Bean时,通过实现Ordered接口标注@Order注解的方式来控制他们的执行顺序。...CommandLineRunnerrun方法执行,然而他们均在SpringApplication和ConfigurableApplicationContext 准备妥当之后,并在SpringApplication.run

59130

如何在Springboot实现自定义初始化操作

Spring Boot应用程序启动完成后,ApplicationRunner接口的run方法会被自动调用,执行你在该方法中定义的任务。...CommandLineRunner 接口 CommandLineRunner 接口是 Spring Boot 中的一个功能性接口,用于在 Spring Boot 应用程序启动执行特定的任务代码。...这个接口只定义了一个方法 run(String... args),该方法Spring Boot 应用程序启动后会被自动调用,其中 args 参数是命令行参数。...Spring Boot启动时会自动检测所有实现了 CommandLineRunner 接口的 bean,并在应用程序启动后按照它们在 Spring 上下文中的注册顺序依次调用其 run 方法。...另外,需要注意的是,Spring Boot启动时会优先调用实现了 ApplicationRunner 接口的 bean 的 run 方法,如果没有找到 ApplicationRunner 类型的 bean

15810
领券