SpringBoot客制化

如果SpringApplication的默认值不符合您的口味,那么可以创建一个本地实例并定制它。例如,要关闭banner,可以编写:

public static void main(String[] args) {SpringApplication app = new SpringApplication(MySpringConfiguration.class);app.setBannerMode(Banner.Mode.OFF);app.run(args);
}

Fluent Builder API

如果需要构建ApplicationContext层次结构(具有父级的多个上下文/或者,如果您更喜欢使用“流利的”构建器API,则可以使用

SpringApplicationBuilder。

SpringApplicationBuilder允许您将多个方法调用链接在一起,并包含父级以及允许您创建层次结构的子方法,如下面的示例所示:

new SpringApplicationBuilder().sources(Parent.class).child(Application.class).bannerMode(Banner.Mode.OFF).run(args);

应用事件及监听器

除了通常的Spring框架事件(如ContextRefreshedEvent)之外,还有SpringApplication发送一些附加的应用程序事件。

一、自定义监听器:

1、创建:

    META-INF/spring.factories

2、添加:

org.springframework.context.ApplicationListener=com.example.project.MyListener

二、应用程序事件发送顺序如下

Application events are sent in the following order, as your application runs:1、An ApplicationStartedEvent is sent at the start of a run, but before any processing except the registration of listeners and initializers.ApplicationStartedEvent在任何处理之前,程序开始运行时被发送,初始化和自定义注册监听事件除外2、An ApplicationEnvironmentPreparedEvent is sent when the Environment to be used in the context is known, but before the context is created.ApplicationEnvironmentPreparedEvent在上下文被创建之前,应用环境被已知的上下文环境中使用时被发送3、An ApplicationPreparedEvent is sent just before the refresh is started, but after bean definitions have been loaded.ApplicationPreparedEvent在刷新开始之前,beans加载之后被发送4、An ApplicationReadyEvent is sent after the refresh and any related callbacks have been processed to indicate the application is ready to service requests.ApplicationReadyEvent在刷新后被发送,并且任何相关的回调都已经被处理,表明该应用程序已经准备好处理服务请求5、An ApplicationFailedEvent is sent if there is an exception on startup.ApplicationFailedEvent如果启动时存在异常时被发送You often won’t need to use application events, but it can be handy to know that they exist. Internally, Spring Boot uses events to handle a variety of tasks.我们不需要使用应用程序事件,但是很方便的知道他们存在,在SpringBoot内部使用各种事件来处理各种

应用程序事件通过使用Spring框架的事件发布机制发送。其中一部分机制确保在子上下文中发布给侦听器的事件也被发布

任何祖先上下文中的侦听器。因此,如果应用程序使用层次结构在SpringApplication实例中,侦听器可以接收同一类型的多个实例应用程序事件。

允许侦听器区分上下文中的事件和后代上下文,它应该请求注入其应用程序上下文,然后比较与事件上下文一起注入的上下文。上下文可以通过实现applicationContextAware,如果侦听器是bean,则使用@autowired。

WEB 环境

SpringApplication试图代表您创建正确类型的ApplicationContext。

用于确定WebApplicationType的算法相当简单:

•如果存在Spring MVC,则AnnotationConfigServetWebServerApplicationContext被使用

•如果不存在Spring MVC且存在Spring WebFlux,则使用annotationconfigreactiveWebServerApplicationContext

•否则,将使用AnnotationConfigApplicationContext

这意味着,如果您使用的是SpringMVC和SpringWebFlux中的新WebClient默认情况下,将使用同一应用程序spring mvc。您可以通过调用setWebApplicationType(WebApplicationType)。

还可以完全控制调用setApplicationContextClass(…)。

访问应用参数

如果需要访问传递给SpringApplication.run(…),可以插入org.springframework.boot.applicationarguments bean。这个

ApplicationArguments接口提供对原始字符串[]参数以及已分析的选项和非选项参数,如下例所示:

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"]}
}

应用程序退出

每个SpringApplication都向JVM注册一个关闭挂钩,以确保ApplicationContext在退出时正常关闭。所有标准的Spring生命周期回调(例如可使用DisposableBean接口或@Predstroy注释)。

例子:

@SpringBootApplication
public class ExitCodeApplication {
@Bean
public ExitCodeGenerator exitCodeGenerator() {return () -> 42;}
public static void main(String[] args) {System.exit(SpringApplication.exit(SpringApplication.run(ExitCodeApplication.class, args)));}
}

SpringBoot2.1.5(13)--- SpringBoot 特性下相关推荐

  1. java13页_Java 13 新特性及实战案例

    近期 Java 界好消息频传.先是 Java 13 发布,接着 Eclipse 也发布了新版本表示支持新版本的Java 特性. 本文介绍了 Java 13 的新特性并展示了相关的示例. 2019年9月 ...

  2. SpringBoot2.1.5 (22)--- SpringBoot设置支持跨域请求

    SpringBoot2.1.5 (22)--- SpringBoot设置支持跨域请求 现代浏览器处于安全的考虑,在http/https请求时必须遵守同源策略,否则即使跨域的http/https 请求, ...

  3. SpringBoot2.1.5 (4)---SpringBoot 常用注解说明

    SpringBoot2.1.5 (4)---SpringBoot 常用注解说明 @SpringBootApplication Spring Boot的项目一般都会有*Application的入口类,入 ...

  4. 一篇文带你了解JDK 13新特性,保姆级教程!!!

    JDK 13新特性介绍 1.1 JDK 各版本主要特性回顾 JDK Version 1.0 1996-01-23 Oak(橡树) 初代版本,伟大的一个里程碑,但是是纯解释运行,使用外挂JIT,性能比较 ...

  5. 项目总结10:通过反射解决springboot环境下从redis取缓存进行转换时出现ClassCastException异常问题...

    通过反射解决springboot环境下从redis取缓存进行转换时出现ClassCastException异常问题 关键字 springboot热部署  ClassCastException异常 反射 ...

  6. Springboot工程下使用mybatis反向工程

    Springboot工程下使用mybatis逆向工程 1.引言 mybatis是目前很流行的持久层框架,其逆向工程更是大大缩减了我们的开发时间.所谓mybatis逆向工程,就是mybatis会根据我们 ...

  7. rhel6系统中,mysql 5.6复制新特性下主从复制配置[基于GTID]

    1.mysql5.6在复制方面的新特性: (1).支持多线程复制:事实上是针对每个database开启相应的独立线程,即每个库有一个单独的(sql thread).针对这样的改进,如果我们想实现多线程 ...

  8. springboot框架下利用websocket实现即时通讯

    springboot框架下利用websocket实现即时通讯(文章末尾有git项目打包文件,直接下载使用即可) 用websocket实现简单的在线聊天,先画个时序图,直观感受下流程 SystemCon ...

  9. springboot项目下关于网站访问量UV统计

    springboot项目下关于网站访问量UV统计 例,根据某篇文章的id,获取该文章一天内被浏览的次数,且不允许存在恶意刷新浏览次数 了解什么是UV统计 要想实现该功能,首先得了解什么是UV,PV,I ...

最新文章

  1. 有源淹没分析arcgis_基于ArcGIS的洪水淹没分析与三维模拟
  2. TensorFlow实战笔记(17)---TFlearn
  3. Can't connect to HTTPS URL because the SSL module is not available
  4. python3 数学常量
  5. 在 SAP Spartacus 里如何调用 hybris 里实现的自定义 API
  6. CSP2021NOIP2021游记
  7. Java Swing/AWT和GTK混合GUI编程
  8. 二套房贷款首付比例?
  9. JS脚本defer的作用
  10. 利用构造函数实现累加
  11. QCC512x QCC302x Earbud 工程增加三击事件
  12. c语言 pow函数及pow函数的错误情况
  13. 使用Voxelmorph配准IXI:数据预处理之颅骨去除及仿射对齐
  14. WEB集群实现LVS负载均衡+域名解析 经典案例——详解
  15. IDEA中如何实现git的cherry-pick可视化操作?
  16. 解决microk8s 报错error: You must be logged in to the server (Unauthorized)
  17. 基于低代码平台(Low Code Platform)开发中小企业信息化项目
  18. 西安公交车路线汇总(1)
  19. 2.深入浅出:晶体管共射极、共集电极、共基极接法的特点——参考《模拟电子技术基础》清华大学华成英主讲
  20. csgo社区服务器(csgo社区服务器点了没反应)

热门文章

  1. STM32F103系统滴答计时器
  2. 【openMV】算法矫正镜头+视频格式对应的分辨率
  3. OK6410开发板学习之外部中断(按键点亮led和蜂鸣器)
  4. 线程优先级抢占实验【RT-Thread学习笔记 3】
  5. 嵌入式Linux系统编程学习之十二守护进程
  6. 开题报告方案论证_【实验科研】我校五项省教育规划教研专项重点课题开题
  7. linux svn 拉取代码_svn快速入门指南
  8. 3.4.3 深度探索linux,3.2.4 vmlinux.bin的构建过程(3)
  9. spring-boot actuator(监控)配置和使用
  10. mysql 多列索引的生效规则,生成1000w数据的存储过程