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
SpringApplicationattempts to create the right type ofApplicationContexton your behalf. The algorithm used to determine aWebApplicationTypeis fairly simple:- If Spring MVC is present, an
AnnotationConfigServletWebServerApplicationContextis used - If Spring MVC is not present and Spring WebFlux is present, an
AnnotationConfigReactiveWebServerApplicationContextis used - Otherwise,
AnnotationConfigApplicationContextis used
This means that if you are using Spring MVC and the new
WebClientfrom 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
ApplicationContexttype 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.ApplicationArgumentsbean. TheApplicationArgumentsinterface provides access to both the rawString[]arguments as well as parsedoptionandnon-optionarguments, 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
SpringApplicationhas started, you can implement theApplicationRunnerorCommandLineRunnerinterfaces. Both interfaces work in the same way and offer a singlerunmethod, which is called just beforeSpringApplication.run(…)completes.The
CommandLineRunnerinterfaces provides access to application arguments as a simple string array, whereas theApplicationRunneruses theApplicationArgumentsinterface discussed earlier. The following example shows aCommandLineRunnerwith arunmethod:- If several
CommandLineRunnerorApplicationRunnerbeans are defined that must be called in a specific order, you can additionally implement theorg.springframework.core.Orderedinterface or use theorg.springframework.core.annotation.Orderannotation. 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.enabledproperty. This exposes theSpringApplicationAdminMXBeanon 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
SpringApplicationregisters a shutdown hook with the JVM to ensure that theApplicationContextcloses gracefully on exit. All the standard Spring lifecycle callbacks (such as theDisposableBeaninterface or the@PreDestroyannotation) can be used.In addition, beans may implement the
org.springframework.boot.ExitCodeGeneratorinterface 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
ExitCodeGeneratorinterface 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的起步依赖和自动配置
随机推荐
- Contour 学习笔记(一):使用 Contour 接管 Kubernetes 的南北流量
原文链接:Contour 学习笔记(一):使用 Contour 接管 Kubernetes 的南北流量 在 Kubernetes 中运行大规模以 Web 为中心的工作负载,最关键的需求之一就是在 L7 ...
- [Python] 通过采集23万条数据,对《哪吒》影评分析
一.说明 数据来源:猫眼: 运行环境:Win10/Python3.7 和 Win7/Python3.5: 分析工具:jieba.WorldCloud.pyecharts和matplotlib: 程序基 ...
- Spring依赖注入浅析
1. 概念理解 依赖注入 谁注入谁? 依赖对象注入IoC容器. 控制反转 谁控制谁?控制什么? IoC容器控制对象,控制依赖对象的创建与注入. 为什么称为反转?创建.注入对象的控制权由程序员的主观意愿 ...
- SPOJ - 3267. D-query 主席树求区间个数
SPOJ - 3267 主席树的又一种写法. 从后端点开始添加主席树, 然后如果遇到出现过的元素先把那个点删除, 再更新树, 最后查询区间就好了. #include<bits/stdc++.h& ...
- Python学习之旅:使用virtualenv创建Python环境及PyQT5环境配置
一.写在前面 从学 Python 的第一天起,我就知道了使用 pip 命令来安装包,从学习爬虫到学习 Web 开发,安装的库越来越多,从 requests 到 lxml,从 Django 到 Flas ...
- 007 Python程序语法元素分析
目录 一.概述 二.程序的格式框架 2.1 代码高亮 2.2 缩进 2.3 注释 2.4 缩进.注释 三.命名与保留字 3.1 变量 3.2 命名 3.3 保留字 3.4 变量.命名.保留字 四.数据 ...
- lambda表达式不同对象根据对象某个属性去重
1.有时候有两个list对象,我们想要去重,比如: List<User> userList和List<Person>personList 想通过User的id和Person的i ...
- SSM框架学习笔记(一)
Spring框架 Spring :是一个开源框架,起初是为解决企业应用开发的复杂性而创建的,但是现在已经不止 企业级应用开发,Spring的核心就是提供了一个轻量级的控制反转和面向切面编程. SPri ...
- Java连载30-方法重载、方法递归
一.方法重载 1.又被称为overload 2.方法重载使用场景 功能类似的时候,尽可能仍方法名相同(但是功能不同或者不相似的时候,方法名尽量不同) 3.什么条件满足之后,可以构成方法重载 (1)在同 ...
- ELKBR部署检测项目日志
ELK filebeat:具有日志收集功能,相比logstash,+filebeat更轻量,占用资源更少,适合客户端使用. redis消息队列选型:Redis 服务器通常都是用作 NoSQL 数据库, ...