spring boot 特性 - 基础

关键字



1. 上期回顾

上一节基本了解了一个spring boot项目的简单搭建过程,对spring boot也有了一个感性认识;也了解了过程中的环节通过什么样的方式解决了我们第一节提到的问题。

本节主要内容为简单了解spring boot的基础特性


2. 目标

      了解与SpringApplication类相关的基础特性

  • FailureAnalyzers
  • Banner
  • SpringApplication
  • Fluent builder API
  • Application events and listeners
  • Web environment
  • Accessing application arguments
  • Using the ApplicationRunner or CommandLineRunner
  • Application exit
  • Admin features
3. 特性
  • FailureAnalyzers
spring boot项目执行时会产生各种类型的exception,failureAnaylzer可以捕获到对应的异常,并把异常转换成指定的、可读的显示格式。

自定义:实现 接口 FailureAnalyzer,并在配置文件META-INF/spring.factories中注册自定义的  FailureAnalyzer类
org.springframework.boot.diagnostics.FailureAnalyzer=\
com.example.MyXXXXFailureAnalyzer
  • Banner
banner是项目的广告信息,可以是文本信息,也要以是图片(最终转成文本信息)。

文本信息:
      classpath下创建banner.txt文本文件,里面可以配置任何文本,同时可以通过占位符动态获取项目中的基本信息
    ${application.version}/${application.formatted-version}/${spring-boot.version}/${spring-boot.formatted-version}
具体含义参考官方文档

图片信息:
  除了使用文本文件,也可以指定一张图片。在classpath下创建banner.jpg/banner.png/banner.gif

除了以上banner内容,还有一些有用的配置:
spring.main.banner-mode=off #banner模式:off关闭、
banner.charset=UTF-8 # Banner file encoding.
banner.location=classpath:banner.txt # Banner file location.
banner.image.location=classpath:banner.gif # Banner image file location (jpg/png can also be used).
banner.image.width= # Width of the banner image in chars (default 76)
banner.image.height= # Height of the banner image in chars (default based on image height)
banner.image.margin= # Left hand image margin in chars (default 2)
banner.image.invert= # If images should be inverted for dark terminal themes (default false


  • springApplication
springApplication是spring boot项目的核心,run静态方法是项目启动入口。如果不希望通过配置文件配置,而是希望通过代码配置一些springApplication参数,如:listener,则可以自定义SpringApplication实例进行配置。

@SpringBootApplication
public class App implements CommandLineRunner
{
    public static void main( String[] args )
    {
        //项目启动入口:
        //下面这种实现方式,可以做更多的事:如:在项目启动前注册早期所需要的Listener
        SpringApplication springApplication = new SpringApplication(App.class);
        springApplication.addListeners(new MyApplicationStarting());
        springApplication.addListeners(new MyApplicationEnvironmentPrepared());
        springApplication.addListeners(new MyApplicationPrepared());
        springApplication.addListeners(new MyApplicationReady());
        springApplication.addListeners(new MyApplicationFailed());
        springApplication.run(args);

    }
}


  • Fluent builder API

流式api方式创建SpringAppliction并设置对应配置。
new SpringApplicationBuilder()
        .sources(Parent.class)
        .child(Application.class)
        .bannerMode(Banner.Mode.OFF)
        .run(args);

  • Application events and listeners


spring启动和运行过程中会发送不同的事件。可以通过配置对应的listener来获取spring事件。
首先,对于spring项目启动发送的事件,可以通过把listener配置成普通的bean即可
其次,对于spring项目启前或过程中的事件,早于ApplicationContext创建完成时间,所以需要提前注册
         A:META-INF/spring.factories配置文件中配置 org.springframework.context.ApplicationListener=com.demo.XXXXLisnter
        B:自定义  SpringApplication 实例,并配置对应的listener
         
        SpringApplication springApplication = new SpringApplication(App.class);
        springApplication.addListeners(new MyApplicationStarting());
        springApplication.run(args); 

 

典型的事件如下:
ApplicationStartingEvent
ApplicationEnvironmentPreparedEvent
ApplicationPreparedEvent
ApplicationReadyEvent
ApplicationFailedEvent

  • Web environment
web environment即web环境,决定了spring boot是否创建web项目,如果是web项目,则会加载spring MVC等web配置,以及启动内置的tomcat。

web environment默认是通过判断classpath中是否存在类: javax.servlet.Servlet或org.springframework.web.context.ConfigurableWebApplicationContext
可在通过springApplication.setWebEnvironment(false);手动设置此配置,会覆盖通过servlet类自动判断的设置


  • Accessing application arguments
访问main方法中的参数。有时需要在自定义的bean中使用从命令行传递到main方法中的参数,可以通过注入一个 ApplicationArguments Bean。
如下:
@Component
public class MyBean {

    @Autowired
    ApplicationArguments args

}


  • using the applicationListener or commondListener
 如果希望在项目启动后就执行一些特殊操作,则可以实现 ApplicationListener 或 CommondListener。此类bean会在ApplicationContext refresh后调用。
如果有多个此类bean,可以通过@ Order注解指定加执行顺序
import org.springframework.boot.*
import org.springframework.stereotype.*

@Component
public class MyBean implements CommandLineRunner {

    public void run(String... args) {
        // Do something...
    }

}


  • application exit
spring boot项目启动后,会向jvm中注册关半的回调钩子,以实现优雅关闭。当项目退出时,所有实现了 DisposableBean接口,或者配置了 @PreDestroy注解的bean都会被调用。

注意:当被直接强制kill时,这些回调会没有机会被调用,因此不要在这些回调中放置重要的业务逻辑


4.接下来

本节主要了解了spring boot的一些基础特性,下一节我们聊聊spring boot一个重要的特性:配置







  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值