Springboot 2使用SpringApplication

SpringApplication 使用静态方法 SpringApplication.run(MySpringConfiguration.class, args); 使用构造器 SpringApplication app = new SpringApplication(MySpringConfiguration.class);...

SpringApplication

使用静态方法

SpringApplication.run(MySpringConfiguration.class, args);

使用构造器

SpringApplication app = new SpringApplication(MySpringConfiguration.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);

使用 builder

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

1、失败分析器

初始化实现了 FailureAnalyzer 接口的失败分析器,可以在启动失败时,打印错误日志和解决操作方法。比如启动端口被占用时打印如下日志:

2019-08-07 10:22:32.534 ERROR 2616 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

The Tomcat connector configured to listen on port 1111 failed to start. The port may already be in use or the connector may be misconfigured.

Action:

Verify the connector's configuration, identify and stop any process that's listening on port 1111, or configure this application to listen on another port.

2、自定义 Banner

可以将 banner.txt 文件添加到类路径或配置 spring.banner.location 属性指定该文件的路径来自定义 Banner。 如果文件的编码不是 UTF-8,则可以配置 spring.banner.charset 指定编码。 除了文本文件,还可以将 banner.gif、banner.jpg、banner.png 图片文件添加到类路径,或者配置 spring.banner.image.location 指定图片路径。 图片会转换为 ASCII 图形后,在打印 banner.txt 之前打印。

还可以在 banner.txt 添加变量,比如 ${spring-boot.version} 获取当前 Springboot 版本。

可以通过编码或者配置的方式指定 Banner 的输出方式:

编码方式:SpringApplication.setBanner(Mode.CONSOLE)SpringApplicationBuilder.bannerMode(Mode.CONSOLE)
配置文件:spring.main.banner-mode=console

  • OFF:禁止打印
  • CONSOLE:用 System.out 答应到控制台
  • LOG:打印到日志文件

3、应用程序事件和监听器

可以使用 SpringApplication.addListeners() 或 SpringApplicationBuilder.listeners() 注册事件监听器;
还可以创建文件 META-INF/spring.factories,key 是 org.springframework.context.ApplicationListener,value 是监听器实现类,示例如下;

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

应用程序事件的发送顺序

  1. ApplicationStartingEvent 在应用启动但是还没做任何处理(除了监听器和初始化器)之前发送
  2. ApplicationEnvironmentPreparedEvent 在上下文需要的环境 Environment 已知,上下文创建之前发送
  3. ApplicationPreparedEvent 在刷新开始之前,加载 bean 定义之后发送
  4. ApplicationStartedEvent 在刷新上下文之后,调用应用程序和命令行运行程序之前发送
  5. ApplicationReadyEvent 在调用应用程序和命令行运行程序之后发送。表示应用程序已准备好为请求提供服务。
  6. ApplicationFailedEvent 启动发生异常时发送

监听器相关原理见 Spring事件监听器源码

4、Web 环境

指定 web 环境:SpringApplication.setWebApplicationType(WebApplicationType.SERVLET)SpringApplicationBuilder.web(WebApplicationType.SERVLET)

WebApplicationType

  • NONE:非 web 环境,上下文使用AnnotationConfigApplicationContext
  • SERVLET:Spring MVC 环境,上下文使用AnnotationConfigServletWebServerApplicationContext
  • REACTIVE:Spring WebFlux 环境,上下文使用AnnotationConfigReactiveWebServerApplicationContext

5、指定参数 args

如使用 debug 模式运行程序:java -jar myproject.jar --debug

6、使用 ApplicationRunner 和 CommandLineRunner

实现这两个接口,可以在应用程序启动后,执行一些操作,默认会先执行 ApplicationRunner。

7、应用退出

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

此外,如果 bean 希望在调用SpringApplication.exit()时返回特定的退出码,则可以实现org.springframework.boot.ExitCodeGenerator接口。 然后将退出代码传递给System.exit(),如下所示:

@SpringBootApplication
public class ExitCodeApplication {

    @Bean
    public ExitCodeGenerator exitCodeGenerator() {
        return () -> 42;
    }

    public static void main(String[] args) {
        System.exit(SpringApplication.exit(SpringApplication.run(Bootstrap.class, args)));
    }

}

ExitCodeGenerator接口也可以通过异常方式实现。发生此类异常时,Spring Boot 返回实现的getExitCode()方法的退出码。

8、管理功能

通过指定spring.application.admin.enabled属性,可以为应用程序启用管理功能。 这会在MBeanServer平台上公开SpringApplicationAdminMXBean。 可以使用此功能远程管理 Spring Boot 应用程序。 此功能对于任何服务包装器的实现也很有用。

注意:启用此功能时要小心,因为 MBean 公开了一种关闭应用程序的方法。

  • 发表于 2019-08-14 10:00
  • 阅读 ( 318 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除