Merge pull request #8972 from kwoyke/JAVA-1098

JAVA-1098: Add code for the Spring Boot Exit Codes
This commit is contained in:
Josh Cummings 2020-03-28 00:46:46 -06:00 committed by GitHub
commit af3e4351df
5 changed files with 85 additions and 1 deletions

View File

@ -11,3 +11,4 @@ This module contains articles about Spring Boot customization
- [Spring Boot: Configuring a Main Class](https://www.baeldung.com/spring-boot-main-class)
- [How to Define a Spring Boot Filter?](https://www.baeldung.com/spring-boot-add-filter)
- [Guide to the Favicon in Spring Boot](https://www.baeldung.com/spring-boot-favicon)
- [Spring Boot Exit Codes](https://www.baeldung.com/spring-boot-exit-codes)

View File

@ -0,0 +1,31 @@
package com.baeldung.exitcode.event;
import org.springframework.boot.ExitCodeEvent;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.EventListener;
@SpringBootApplication
public class ExitCodeEventDemoApplication {
public static void main(String[] args) {
System.exit(
SpringApplication.exit(
SpringApplication.run(ExitCodeEventDemoApplication.class, args)
)
);
}
@Bean
DemoListener demoListenerBean() {
return new DemoListener();
}
private static class DemoListener {
@EventListener
public void exitEvent(ExitCodeEvent event) {
System.out.println("Exit code: " + event.getExitCode());
}
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.exitcode.exception;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.ExitCodeExceptionMapper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class ExitCodeExceptionMapperDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ExitCodeExceptionMapperDemoApplication.class, args);
}
@Bean
CommandLineRunner createException() {
return args -> Integer.parseInt("test");
}
@Bean
ExitCodeExceptionMapper exitCodeToExceptionMapper() {
return exception -> {
// set exit code based on the exception type
if (exception.getCause() instanceof NumberFormatException) {
return 80;
}
return 1;
};
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.exitcode.generator;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ExitCodeGeneratorDemoApplication implements ExitCodeGenerator {
public static void main(String[] args) {
System.exit(
SpringApplication.exit(
SpringApplication.run(ExitCodeGeneratorDemoApplication.class, args)
)
);
}
@Override
public int getExitCode() {
return 42;
}
}

View File

@ -22,7 +22,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
- [An Introduction to Kong](https://www.baeldung.com/kong)
- [Spring Boot: Configuring a Main Class](https://www.baeldung.com/spring-boot-main-class)
- [A Quick Intro to the SpringBootServletInitializer](https://www.baeldung.com/spring-boot-servlet-initializer)
- [Guide to the Favicon in Spring Boot](https://www.baeldung.com/spring-boot-favicon)
- [Spring Shutdown Callbacks](https://www.baeldung.com/spring-shutdown-callbacks)
- [Container Configuration in Spring Boot 2](https://www.baeldung.com/embeddedservletcontainercustomizer-configurableembeddedservletcontainer-spring-boot)
- [Validation in Spring Boot](https://www.baeldung.com/spring-boot-bean-validation)