Spring Boot 退出代码生成器 - 实现不起作用

Ale*_*tti 4 java groovy spring-boot

我意识到在 SO 上有几个关于这个主题的类似未解决的问题,但是,我在 GitHub 上包含了一个可克隆的 MCVE,因此很容易重现问题场景。

设想

我正在尝试根据 Spring Boot文档实现一个 Spring Boot 退出代码生成器。看起来 Spring Boot 正在调用退出代码生成器,但应用程序以代码 0 退出,而不是给定的退出代码。我错过了什么?

@SpringBootApplication
@Configuration
@Slf4j
class App implements CommandLineRunner {

    @Autowired ConfigurableApplicationContext ctx

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

    @Override
    void run(String... args) throws Exception {
        SpringApplication.exit(ctx, new ExitCodeGenerator() {
            @Override
            int getExitCode() {
                log.info 'retrieving exit code.'
                return -1
            }
        })
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

C:\Users\pc\IdeaProjects\spring-exit-code-gen>gradlew bootRepackage

C:\Users\pc\IdeaProjects\spring-exit-code-gen>java -jar build/libs/spring-exit-code-gen-1-0.0.1-SNAPSHOT.jar

2017-09-13 12:26:53.819  INFO 2832 --- [           main] com.scarlatti.App                        : Starting App on pc-PC with PID 2832 (C:\Users\pc\IdeaProjects\spring-exit-code-gen\build\libs\spring-exit-code-gen-1-0.0.1-SNAPSHOT.jar started by pc in C:\Users\pc\IdeaProjects\spring-exit-code-gen\build\libs)
2017-09-13 12:26:53.824  INFO 2832 --- [           main] com.scarlatti.App                        : No active profile set, falling back to default profiles: default
2017-09-13 12:26:53.966  INFO 2832 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2752f6e2: startup date [Wed Sep 13 12:26:53 CDT 2017]; root of context hierarchy
2017-09-13 12:26:54.924  INFO 2832 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-09-13 12:26:54.962  INFO 2832 --- [           main] com.scarlatti.App                        : retrieving exit code.
2017-09-13 12:26:54.963  INFO 2832 --- [           main] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@2752f6e2: startup date [Wed Sep 13 12:26:53 CDT 2017]; root of context hierarchy
2017-09-13 12:26:54.964  INFO 2832 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
2017-09-13 12:26:54.969  INFO 2832 --- [           main] com.scarlatti.App                        : Started App in 1.945 seconds (JVM running for 3.132)

C:\Users\pc\IdeaProjects\spring-exit-code-gen>echo %ERRORLEVEL%
0
Run Code Online (Sandbox Code Playgroud)

从这里从 GitHub 克隆来尝试一下。

And*_*son 5

文档中所述,您需要使用调用System.exit结果来调用SpringApplication.exit. 像这样的东西:

@Override
void run(String... args) throws Exception {
    System.exit(SpringApplication.exit(ctx, new ExitCodeGenerator() {
        @Override
        int getExitCode() {
            log.info 'retrieving exit code.'
            return -1
        }
    }))
}
Run Code Online (Sandbox Code Playgroud)

  • 使用 lambda:`System.exit(SpringApplication.exit(context, () -> -1));` (2认同)