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的起步依赖和自动配置
随机推荐
- PHPstorm 运行时出现 Not Fount 解决办法
PHPstorm 对默认 localhost 有解析问题,需要手动配置 打开菜单栏 Tools->Deployment->configuration,如下图: 我这是配置完的,你的应该什么 ...
- Java多线程之线程的启动
Java多线程之线程的启动 一.前言 启动线程的方法有如下两种. 利用Thread 类的子类的实例启动线程 利用Runnable 接口的实现类的实例启动线程 最后再介绍下java.util.concu ...
- eslint语法规范
规则 缩进使用两个空格. eslint: indent function hello (name) { console.log('hi', name) } 1 2 3 字符串使用单引号,除 ...
- HashMap并发下死循环问题解析
首先小伙伴要明确:死循环问题在JDK 1.8 之前是存在的,JDK 1.8 通过增加loHead和loTail进行了修复. 在JDK 1.7及之前 HashMap在并发情况下导致循环问题,致使服务器c ...
- docker运行原理与使用总结
docker运行原理概述 Client-Server架构 docker守护进程运行在宿主机上systemctl start docker daemon进程通过socket从客户端(docker命令)接 ...
- StackOverflow 周报 - 与高关注的问题过过招(Java)
本篇文章是 Stack Overflow 周报的第二周,共收集了 4 道高关注的问题和对应的高赞回答.公众号「渡码」为日更,欢迎关注. DAY1. serialVersionUID 的重要性 关注: ...
- 蚂蚁SOFA系列(1) - 聊聊SOFA的模块化
作者:404,转载请注明出处.欢迎关注公众号:404P. SOFA是蚂蚁自研的一套金融级分布式中间件,目前正在逐步向业界开源.SOFA的全称有两个,最早是Service Oriented Fabric ...
- Mysql高手系列 - 第7篇:玩转select条件查询,避免踩坑
这是Mysql系列第7篇. 环境:mysql5.7.25,cmd命令中进行演示. 电商中:我们想查看某个用户所有的订单,或者想查看某个用户在某个时间段内所有的订单,此时我们需要对订单表数据进行筛选,按 ...
- 【Offer】[28] 【对称的二叉树】
题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 请实现一个函数,用来判断一-棵二叉树是不是对称的.如果一棵二叉树和它的镜像一样,那么它是对称的.  牛客网刷题地址 思路分析 利用前序 ...
- NameNode数据存储
HDFS架构图 HDFS原理 1) 三大组件 NameNode. DataNode .SecondaryNameNode 2)NameNode 存储元数据(文件名.创建时间.大小.权限.文件与blo ...