Enable Spring Boot Admin, Custom Exit Codes, and Startup Tracking
This guide shows how to activate Spring Boot's admin features via configuration, customize application shutdown with exit codes and DisposableBean, and monitor startup performance using ApplicationStartup and Actuator endpoints, all illustrated with practical code snippets and screenshots.
1. Admin Features
By setting the spring.application.admin.enabled property to true , Spring Boot exposes a SpringApplicationAdminMXBean on the platform MBeanServer, allowing remote management of the application.
<code>spring:
application:
admin:
enabled: true</code>After enabling, you can use JConsole to view and manage the MBean. The default JMX name is org.springframework.boot:type=Admin,name=SpringApplication .
From the operation menu you can shut down the application or retrieve configuration properties via getProperty .
Custom JMX name can be defined as follows:
<code>spring:
application:
admin:
enabled: true
jmx-name: com.pack:type=AKF,name=PackAPP</code>Note: the jmx-name format is xxx:type=yyy,name=zzz .
2. Application Exit
Each SpringApplication registers a JVM shutdown hook to ensure the ApplicationContext closes properly. Standard Spring lifecycle callbacks such as DisposableBean or the @PreDestroy annotation are invoked.
2.1 Custom Exit Code
<code>@Component
@Order(-2)
public class PackExitCodeGenerator implements ExitCodeGenerator {
@Override
public int getExitCode() {
System.out.println("PackExitCodeGenerator exit code 6...");
return 6;
}
}</code>Multiple ExitCodeGenerator implementations can be defined; Spring Boot returns the first non‑zero exit code based on the @Order value.
2.2 Custom DisposableBean
<code>@Component
public class AppComponent implements DisposableBean {
@Override
public void destroy() throws Exception {
System.out.println("Bean lifecycle ended...");
}
}</code>2.3 Define Exit Endpoint
<code>@Resource
private ConfigurableApplicationContext context;
@GetMapping("")
public Object exit() {
SpringApplication.exit(context);
return "exit";
}</code>2.4 Test
Calling the endpoint prints the following output in the console:
Before the application shuts down, Spring Boot publishes an ExitCodeEvent :
<code>@Component
public class PackAppExitListener implements ApplicationListener<ExitCodeEvent> {
@Override
public void onApplicationEvent(ExitCodeEvent event) {
System.out.printf("Detected application exit event, exit code: %d%n", event.getExitCode());
}
}</code>3. Application Startup Tracking
During startup, SpringApplication and ApplicationContext perform many tasks related to the application and bean lifecycles. The ApplicationStartup interface allows you to collect StartupStep data for analysis.
Example using BufferingApplicationStartup :
<code>SpringApplication application = new SpringApplication(SpringbootComprehensiveApplication.class);
application.setApplicationStartup(new BufferingApplicationStartup(2048));
application.run(args);</code>To view the collected data, add the Actuator dependency and access the /startup endpoint, which returns JSON with the full startup information.
<code><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency></code>You can also inject ApplicationStartup into your own beans for custom monitoring:
<code>@Resource
private ApplicationStartup startup;</code>Then call startup.start(...) and startup.end(...) to record custom steps.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.