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)));
} }
Spring boot 梳理 - SpringApplication的更多相关文章
- Spring Boot的SpringApplication类详解
相信使用过Spring Boot的开发人员,都对Spring Boot的核心模块中提供的SpringApplication类不陌生.SpringApplication类的run()方法往往在Sprin ...
- Spring boot 梳理 - 代码结构(Main类的位置)
Spring boot 对代码结构无特殊要求,但有个套最佳实践的推荐 不要使用没有包名的类.没有包名时,@ComponentScan, @EntityScan, or @SpringBootAppli ...
- Spring boot 梳理 - WebMvcConfigurer接口 使用案例
转:https://yq.aliyun.com/articles/617307 SpringBoot 确实为我们做了很多事情, 但有时候我们想要自己定义一些Handler,Interceptor,Vi ...
- Spring boot 梳理 - @Conditional
@Conditional(TestCondition.class) 这句代码可以标注在类上面,表示该类下面的所有@Bean都会启用配置,也可以标注在方法上面,只是对该方法启用配置. spring框架还 ...
- Spring boot 梳理 -@SpringBootApplication、@EnableAutoConfiguration与(@EnableWebMVC、WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter)
@EnableWebMvc=继承DelegatingWebMvcConfiguration=继承WebMvcConfigurationSupport 直接看源码,@EnableWebMvc实际上引入一 ...
- Spring boot 梳理 - 模版引擎 -freemarker
开发环境中关闭缓存 spring: thymeleaf: cache: false freemarker: cache: false Spring boot 集成 freemarker <dep ...
- Spring boot 梳理 - Spring boot 与 JSP
若使用Spring boot 开发web应用中使用jsp,需要打包成war,并部署到非嵌入式servlet容器中运行,在嵌入式servlet中无法运行,且需要匹配非嵌入式servlet版本与Sprin ...
- Spring boot 梳理 - Spring boot自动注册DispatcherServlet
spring boot提供的DispatcherServlet的name就是“dispatcherServlet”. 源码 public ServletRegistrationBean dispatc ...
- Spring boot - 梳理 - 根本上说,Spring Boot项目只不过是一个普通的Spring项目,只是使用了Spring Boot的起步依赖和自动配置
根本上说,Spring Boot项目只不过是一个普通的Spring项目,只是使用了Spring Boot的起步依赖和自动配置
随机推荐
- (八)分布式通信----主机Host
上节中有谈到的是通信主机(TransportHost),本节中主机(ServiceHost)负责管理服务的生命周期. 项目中将两个主机拆分开,实现不同的功能: 通信主机:用于启动通信监听端口: 生命周 ...
- JUC包Lock机制的支持--AQS
在上一次总结中,提到了JUC包下使用Lock接口实现同步的方法,以及和Synchronized关键字的一些比较,那么使用Lock完成锁机制的底层支持又是什么呢?总结如下: 1 AQS是什么 AQS是一 ...
- Java之封装,继承,多态
一,前言 今天总结一下关于Java的三大特性,封装,继承,多态.其实关于三大特性对于从事编程人员来说都是基本的了,毕竟只要接触Java这些都是先要认识的,接下来就系统总结一下. 二,封装 先来 ...
- 一文读懂HashMap
推荐 转载:https://www.jianshu.com/p/ee0de4c99f87
- 2019DX#6
Solved Pro.ID Title Ratio(Accepted / Submitted) 1001 Salty Fish 16.28%(7/43) OK 1002 Nonsense Tim ...
- B-generator 1_2019牛客暑期多校训练营(第五场)
题意 给出\(x0,x1,a,b\), \(x_i = a\cdot x_{i-1} + b\cdot x_{i-2}\),问\(x_n取模mod\) 题解 用十进制快速幂,二进制快速幂是每到下一位就 ...
- BZOJ3170 [Tjoi2013]松鼠聚会 切比雪夫距离 - 曼哈顿距离 - 前缀和
BZOJ3170 题意: 有N个小松鼠,它们的家用一个点x,y表示,两个点的距离定义为:点(x,y)和它周围的8个点即上下左右四个点和对角的四个点,距离为1.现在N个松鼠要走到一个松鼠家去,求走过的最 ...
- HDU-DuoXiao第二场hdu 6315 Naive Operations 线段树
hdu 6315 题意:对于一个数列a,初始为0,每个a[ i ]对应一个b[i],只有在这个数字上加了b[i]次后,a[i]才会+1. 有q次操作,一种是个区间加1,一种是查询a的区间和. 思路:线 ...
- CodeForces Round #514 (div2)
A:Cashier 题意:问可以休息多少次. 代码: #include<bits/stdc++.h> using namespace std; #define Fopen freopen( ...
- poj 1062 昂贵的聘礼(floyd path的应用)
题目链接:http://poj.org/problem?id=1062 题意就不解释了中问题. 这题用dfs也行.但是感觉还是floyd比较好一点主要是他们交易是有条件的交易总的等级差不能超过m 所以 ...