当前位置:网站首页>23.9 application exit application exit
23.9 application exit application exit
2022-07-26 08:29:00 【csg103】
Every SpringApplication Will be in JVM Register a close hook on , In order to ensure that ApplicationContext Close gracefully on exit . All standard Spring Lifecycle Callback ( for example DisposableBean Interface , perhaps @PreDestroy notes ) You can use .
Besides ,bean It can also be realized org.springframework.boot.ExitCodeGenerator SHI
And add resource files application.properties:
name: JavaChen
Application Start class
@SpringBootApplication public class Springboot239Application implements CommandLineRunner { @Autowired private HelloWorldService helloWorldService; public static void main(String[] args) { SpringApplication.run(Springboot239Application.class, args); } @Override public void run(String... args) { System.out.println(this.helloWorldService.getMessage()); if (args.length > 0 && args[0].equals("exitcode")) { throw new ExitException(); } } }
If some CommandLineRunner beans Defined must be called in a specific order , You can achieve extra org.springframework.core.Ordered Interface or use @Order annotation .
utilize command-line runner This characteristic of , Then cooperate with dependency injection , You can first introduce some dependencies after the application starts bean, for example data source、rpc Services or other modules , The initialization of these objects can be placed in run In the method . however , It should be noted that , stay run Method, once any exception is encountered , Will make the application stop running , Therefore, it is best to make use of try/catch Statement processing may encounter exceptions .
Every SpringApplication In order to ensure ApplicationContext Gracefully closed , Will register a JVM Of shutdown hook . All standard Spring Lifecycle Callback ( such as ,DisposableBean Interface or @PreDestroy annotation ) Can use .
Besides , If beans Want to return a specific exit code at the end of the application , Can achieve org.springframework.boot.ExitCodeGenerator Interface , For example, in the example above ExitException Exception class :
public class ExitException extends RuntimeException implements ExitCodeGenerator { @Override public int getExitCode() { return 10; } }
@Component public class HelloWorldService { @Value("${name:World}") private String name; public String getMessage() { return "Hello " + this.name; https://github.com/csg103/springboot23-9} }
Source code address https://github.com/csg103/springboot23-9
边栏推荐
猜你喜欢

Code cloud change remote warehouse command

Understand microservices bit by bit

Special lecture 2 dynamic planning learning experience (should be updated for a long time)

QT uses QSS to make a beautiful login interface (hand-in-hand teaching)

Apple's tough new rule: third-party payment also requires a percentage, and developers lose a lot!
![[endnote] detailed explanation of document template layout syntax](/img/fd/2caf4ff846626411fe8468f870e66a.png)
[endnote] detailed explanation of document template layout syntax

Basic configuration of BGP

QSS add resource file of QT

全网最全:Mysql六种约束详解

22-07-12 personal training match 1 competition experience
随机推荐
Nodejs2day (modularization of nodejs, NPM download package, module loading mechanism)
import error: ‘Icon‘ is not exported from ‘antd‘. Import icon error
Status management bloc provider geTx
Why reserve a capacitor station on the clock output?
Basic configuration of BGP
JS工具函数大全
第四天作业
Burp suite Chapter 4 advanced options for SSL and proxy
监控用户积分变化的两种方式
Exam summary on July 13, 2022
Kotlin program control
23.5 event listeners of application events and listeners
Template summary
Burp Suite - Chapter 1 burp suite installation and environment configuration
Add in the registry right click to open in vscode
2022 / 7 / 16 exam summary
正则表达式作业
22-07-14 personal training match 2 competition experience
The first ide overlord in the universe, replaced...
2022/7/18 exam summary