Skip to content

Commit cf2ebbc

Browse files
thombergssnicoll
authored andcommitted
Improve ExitCodeGenerator doc
See gh-9740
1 parent 76c21ea commit cf2ebbc

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,29 @@ callbacks (such as the `DisposableBean` interface, or the `@PreDestroy` annotati
336336
be used.
337337

338338
In addition, beans may implement the `org.springframework.boot.ExitCodeGenerator`
339-
interface if they wish to return a specific exit code when the application ends.
339+
interface if they wish to return a specific exit code when `SpringApplication.exit()`
340+
is called. This exit code can then be passed to `System.exit()` to pass it to the outside.
341+
342+
[source,java,indent=0]
343+
----
344+
@SpringBootApplication
345+
public class ExitCodeApplication {
346+
347+
public static void main(String[] args) {
348+
System.exit(SpringApplication.exit(SpringApplication.run(ExitCodeApplication.class, args)));
349+
}
350+
351+
@Bean
352+
public ExitCodeGenerator exitCodeGenerator(){
353+
return () -> 42;
354+
}
355+
356+
}
357+
----
358+
359+
Also, the `ExitCodeGenerator` interface may be implemented by exceptions. When such an exception is
360+
encountered, Spring Boot will return the exit code provided by the implemented `getExitCode()` method
361+
to the outside caller.
340362

341363

342364

0 commit comments

Comments
 (0)