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的起步依赖和自动配置
随机推荐
- Hugo
快速开始 安装Hugo 1.二进制安装(推荐:简单.快速) 到 Hugo Releases 下载对应的操作系统版本的Hugo二进制文件(hugo或者hugo.exe) Mac下直接使用 ==Homeb ...
- Gym - 101252H
题意略. 思路:二分.注意当利率高且m比较小的时候,每个月的偿还可能会大于本金,所以我们二分的右边界应该要设为2 * 本金. 详见代码: #include<bits/stdc++.h> # ...
- three.js模拟实现太阳系行星体系
概况如下: 1.SphereGeometry实现自转的太阳: 2.RingGeometry实现太阳系星系的公转轨道: 3.ImageUtils加载球体和各行星贴图: 4.canvas中createRa ...
- Java8 日期 API 业务使用
最近在做账单结算业务,需要根据客户选择的结算方式来推算下一次结算日期以及该次结算日期段. 推算日期这样的业务直男君以前就写过,只不过使用的是熟悉的 java.util.date 和 java.util ...
- HTML(一)简介,元素
HTML简介 html实例: <!DOCTYPE html> 菜鸟教程 我的第一个标题 我的第一个段落 实例解析: <!DOCTYPE html> 声明为 HTML5 文档,不 ...
- 深度学习常见的优化方法(Optimizer)总结:Adam,SGD,Momentum,AdaGard等
机器学习的常见优化方法在最近的学习中经常遇到,但是还是不够精通.将自己的学习记录下来,以备不时之需 基础知识: 机器学习几乎所有的算法都要利用损失函数 lossfunction 来检验算法模型的优劣, ...
- lightoj 1030-B - Discovering Gold (概率dp)
题意:有一个直线的金矿,每个点有一定数量的金子:你从0开始,每次扔个骰子,扔出几点就走几步, 然后把那个点的金子拿走:如果扔出的骰子超出了金矿,就重新扔,知道你站在最后一个点:问拿走金 子的期望值是多 ...
- Python爬虫之小试牛刀——使用Python抓取百度街景图像
之前用.Net做过一些自动化爬虫程序,听大牛们说使用python来写爬虫更便捷,按捺不住抽空试了一把,使用Python抓取百度街景影像. 这两天,武汉迎来了一个德国总理默克尔这位大人物,又刷了一把武汉 ...
- Storm 系列(四)—— Storm 集群环境搭建
一.集群规划 这里搭建一个 3 节点的 Storm 集群:三台主机上均部署 Supervisor 和 LogViewer 服务.同时为了保证高可用,除了在 hadoop001 上部署主 Nimbus ...
- Springboot中RedisTemplate的操作
Springboot中RedisTemplate的操作 @Autowired private RedisTemplate redisTemplate; @Autowired private Strin ...