9.Spring Boot特性

weixin_34234823發表於2017-10-30

SpringApplication

  • SpringApplication類提供了一種從main()方法啟動Spring應用的便捷方式。在很多情況下,你只需委託給SpringApplication.run這個靜態方法:

    public static void main(String[] args){
         SpringApplication.run(MySpringConfiguration.class, args);
    }    
    
  • 當應用啟動時,你應該會看到類似下面的東西(這是何方神獸?):

      .   ____          _            __ _ _
        /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
        ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
        \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
        '  |____| .__|_| |_|_| |_\__, | / / / /
        =========|_|==============|___/=/_/_/_/
        :: Spring Boot ::        (v1.4.1.RELEASE)
    
        2017-10-30 11:47:08.134  INFO 45312 --- [           main] com.ozan.MyspringbootApplication         : Starting MyspringbootApplication on Think-PC with PID 45312 (D:\Git_Intellij\SpringbootDemo\target\classes started by Think in D:\Git_Intellij\SpringbootDemo)
        2017-10-30 11:47:08.151  INFO 45312 --- [           main] com.ozan.MyspringbootApplication         : The following profiles are active: test,devdb
        2017-10-30 11:47:08.988  INFO 45312 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4c40b76e: startup date [Mon Oct 30 11:47:08 CST 2017]; root of context hierarchy
    
  • 預設情況下會顯示INFO級別的日誌資訊,包括一些相關的啟動詳情,比如啟動應用的使用者等。

自定義Banner
  • 通過在classpath下新增一個banner.txt或設定banner.location來指定相應的檔案可以改變啟動過程中列印的banner。如果這個檔案有特殊的編碼,你可以使用banner.encoding設定它(預設為UTF-8)。
  • 在banner.txt中可以使用如下的變數:

注:如果想以程式設計的方式產生一個banner,可以使用SpringApplication.setBanner(…)方法。使用org.springframework.boot.Banner介面,實現你自己的printBanner()方法。

自定義SpringApplication
  • 如果預設的SpringApplication不符合你的口味,你可以建立一個本地的例項並自定義它。例如,關閉banner你可以這樣寫:

    public static void main(String[] args){
           SpringApplication app = new SpringApplication (MySpringConfiguration.class);
           app.setShowBanner(false);
           app.run(args);
    }
    

注:傳遞SpringApplication的構造器引數是spring beans的配置源。在大多數情況下,這些將是@Configuration類的引用,但它們也可能是XML配置或要掃描包的引用。

  • 你也可以使用application.properties檔案來配置SpringApplication。具體參考Externalized 配置。檢視配置選項的完整列表,可參考SpringApplication Javadoc.
流暢的構建API
  • 如果你需要建立一個分層的ApplicationContext(多個具有父子關係的上下文),或你只是喜歡使用流暢的構建API,你可以使用SpringApplicationBuilder。

  • SpringApplicationBuilder允許你以鏈式方式呼叫多個方法,包括可以建立層次結構的parent和child方法。

      new SpringApplicationBuilder()
          .showBanner(false)
          .sources(Parent.class)
          .child(Application.class)
          .run(args);
    

注:建立ApplicationContext層次時有些限制,比如,Web元件(components)必須包含在子上下文(child context)中,且相同的Environment既用於父上下文也用於子上下文中。具體參考SpringApplicationBuilder javadoc

Application事件和監聽器

  • 除了常見的Spring框架事件,比如ContextRefreshedEvent,一個SpringApplication也傳送一些額外的應用事件。一些事件實際上是在ApplicationContext被建立前觸發的。
  • 你可以使用多種方式註冊事件監聽器,最普通的是使用SpringApplication.addListeners(…)方法。在你的應用執行時,應用事件會以下面的次序傳送:
  1. 在執行開始,但除了監聽器註冊和初始化以外的任何處理之前,會傳送一個ApplicationStartedEvent。
  2. 在Environment將被用於已知的上下文,但在上下文被建立前,會傳送一個ApplicationEnvironmentPreparedEvent。
  3. 在refresh開始前,但在bean定義已被載入後,會傳送一個ApplicationPreparedEvent。
  4. 啟動過程中如果出現異常,會傳送一個ApplicationFailedEvent。

注:你通常不需要使用應用程式事件,但知道它們的存在會很方便(在某些場合可能會使用到)。在Spring內部,SpringBoot使用事件處理各種各樣的任務。

Web環境
  • 一個SpringApplication將嘗試為你建立正確型別的ApplicationContext。在預設情況下,使用
    AnnotationConfigApplicationContext或AnnotationConfigEmbeddedWebApplicationContext取決於你正在開發的是否是web應用。
  • 用於確定一個web環境的演算法相當簡單(基於是否存在某些類)。如果需要覆蓋預設行為,你可以使用setWebEnvironment(boolean webEnvironment)。通過呼叫setApplicationContextClass(…),你可以完全控制ApplicationContext的型別。

注:當JUnit測試裡使用SpringApplication時,呼叫setWebEnvironment(false)是可取的。

命令列啟動器
  • 如果你想獲取原始的命令列引數,或一旦SpringApplication啟動,你需要執行一些特定的程式碼,你可以實現CommandLineRunner介面。在所有實現該介面的Spring beans上將呼叫run(String… args)方法。

    import org.springframework.boot.*
    import org.springframework.stereotype.*
    @Component
    public class MyBean implements CommandLineRunner {
    public void run(String... args) {
        // Do something...
    }
    }
    
  • 如果一些CommandLineRunner beans被定義必須以特定的次序呼叫,你可以額外實現org.springframework.core.Ordered介面或使org.springframework.core.annotation.Order註解。

Application退出
  • 每個SpringApplication在退出時為了確保ApplicationContext被優雅的關閉,將會註冊一個JVM的shutdown鉤子。所有標準的Spring生命週期回撥(比如,DisposableBean介面或@PreDestroy註解)都能使用。
  • 此外,如果beans想在應用結束時返回一個特定的退出碼(exit code),可以實現
    org.springframework.boot.ExitCodeGenerator介面。

相關文章