Spring boot 梳理 - SpringApplication
- 简单启动方式
public static void main(String[] args) {
SpringApplication.run(MySpringConfiguration.class, args);
}- 调试方式启动
java -jar myproject-0.0.1-SNAPSHOT.jar --debug
- 高级启动方式
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
SpringApplication app=new SpringApplication(App.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
}
Web Environment
A
SpringApplication
attempts to create the right type ofApplicationContext
on your behalf. The algorithm used to determine aWebApplicationType
is fairly simple:- If Spring MVC is present, an
AnnotationConfigServletWebServerApplicationContext
is used - If Spring MVC is not present and Spring WebFlux is present, an
AnnotationConfigReactiveWebServerApplicationContext
is used - Otherwise,
AnnotationConfigApplicationContext
is used
This means that if you are using Spring MVC and the new
WebClient
from Spring WebFlux in the same application, Spring MVC will be used by default. You can override that easily by callingsetWebApplicationType(WebApplicationType)
.It is also possible to take complete control of the
ApplicationContext
type that is used by callingsetApplicationContextClass(…)
.- If Spring MVC is present, an
Accessing Application Arguments
- If you need to access the application arguments that were passed to
SpringApplication.run(…)
, you can inject aorg.springframework.boot.ApplicationArguments
bean. TheApplicationArguments
interface provides access to both the rawString[]
arguments as well as parsedoption
andnon-option
arguments, as shown in the following example: import org.springframework.boot.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*; @Component
public class MyBean { @Autowired
public MyBean(ApplicationArguments args) {
boolean debug = args.containsOption("debug");
List<String> files = args.getNonOptionArgs();
// if run with "--debug logfile.txt" debug=true, files=["logfile.txt"]
} }
- If you need to access the application arguments that were passed to
Using the ApplicationRunner or CommandLineRunner
If you need to run some specific code once the
SpringApplication
has started, you can implement theApplicationRunner
orCommandLineRunner
interfaces. Both interfaces work in the same way and offer a singlerun
method, which is called just beforeSpringApplication.run(…)
completes.The
CommandLineRunner
interfaces provides access to application arguments as a simple string array, whereas theApplicationRunner
uses theApplicationArguments
interface discussed earlier. The following example shows aCommandLineRunner
with arun
method:- If several
CommandLineRunner
orApplicationRunner
beans are defined that must be called in a specific order, you can additionally implement theorg.springframework.core.Ordered
interface or use theorg.springframework.core.annotation.Order
annotation. import org.springframework.boot.*;
import org.springframework.stereotype.*; @Component
public class MyBean implements CommandLineRunner { public void run(String... args) {
// Do something...
} }
Admin Features
- It is possible to enable admin-related features for the application by specifying the
spring.application.admin.enabled
property. This exposes theSpringApplicationAdminMXBean
on the platformMBeanServer
. You could use this feature to administer your Spring Boot application remotely. This feature could also be useful for any service wrapper implementation. - If you want to know on which HTTP port the application is running, get the property with a key of
local.server.port
. Take care when enabling this feature, as the MBean exposes a method to shutdown the application.
- It is possible to enable admin-related features for the application by specifying the
Application Exit
Each
SpringApplication
registers a shutdown hook with the JVM to ensure that theApplicationContext
closes gracefully on exit. All the standard Spring lifecycle callbacks (such as theDisposableBean
interface or the@PreDestroy
annotation) can be used.In addition, beans may implement the
org.springframework.boot.ExitCodeGenerator
interface if they wish to return a specific exit code whenSpringApplication.exit()
is called. This exit code can then be passed toSystem.exit()
to return it as a status code, as shown in the following example:- Also, the
ExitCodeGenerator
interface may be implemented by exceptions. When such an exception is encountered, Spring Boot returns the exit code provided by the implementedgetExitCode()
method. @SpringBootApplication
public class ExitCodeApplication { @Bean
public ExitCodeGenerator exitCodeGenerator() {
return () -> ;
} public static void main(String[] args) {
System.exit(SpringApplication
.exit(SpringApplication.run(ExitCodeApplication.class, args)));
} }
最新文章
- java aes_cbc_256 加密解密
- iOS推送通知
- osg::NodeVisitor中计算一个节点对应的世界变换矩阵、法向量、顶点坐标
- [HTTP2] HTTP1 probs and HTTP2 saves
- ☀【组件】getRequest
- MySQL从库忽略某些错误
- python 解析nginx 日志 url
- Linux内核互斥锁--mutex
- RxSwift 系列(一) -- Observables
- APP安全--网络传输安全 AES/RSA/ECC/MD5/SHA
- pwn-ROP
- 2019/2/11 LinuxRPM包管理
- 2018年尚硅谷《全套Java、Android、HTML5前端视频》
- SQL记录-小表join大表查询例子
- Delphi 10.3最新消息
- [CVE-2014-8959] phpmyadmin任意文件包含漏洞分析
- 使用RestTemplate调用接口上传文件
- 依赖注入(DI)在PHP中的实现
- LM算法的推导过程
- 2018.10.01 bzoj3237: [Ahoi2013]连通图(cdq分治+并查集)
热门文章
- python 10 动态参数
- unity之局域网
- Kth Minimum Clique_2019牛客暑期多校训练营(第二场)
- poj 3660Cow Contest
- codeforces 789 C. Functions again(dp求区间和最大)
- 手把手告诉你如何安装多个版本的node,妈妈再也不用担心版本高低引发的一系列后遗症(非常详细,非常实用)
- 对line-height的理解
- Python(Head First)学习笔记:六
- WInform中实现设置ZedGraph中曲线的X轴与Y轴的上限与下限
- 在阿里云服务器CentOS7安装mysql提示“No package mysql-server available上安装mysql遇到的问题