<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 23\. SpringApplication SpringApplication類提供了一種方便的方法來引導將從main()方法啟動的Spring應用程序。 在許多情況下,您只需委派靜態SpringApplication.run()方法: ``` public static void main(String[] args) { SpringApplication.run(MySpringConfiguration.class, args); } ``` 當您的應用程序啟動時,您應該看到類似于以下內容: ``` . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: v1.5.2.RELEASE 2013-07-31 00:08:16.117 INFO 56603 --- [ main] o.s.b.s.app.SampleApplication : Starting SampleApplication v0.1.0 on mycomputer with PID 56603 (/apps/myapp.jar started by pwebb) 2013-07-31 00:08:16.166 INFO 56603 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6e5a8246: startup date [Wed Jul 31 00:08:16 PDT 2013]; root of context hierarchy 2014-03-04 13:09:54.912 INFO 41370 --- [ main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8080 2014-03-04 13:09:56.501 INFO 41370 --- [ main] o.s.b.s.app.SampleApplication : Started SampleApplication in 2.992 seconds (JVM running for 3.658) ``` 默認情況下,將顯示INFO 級別log消息,包括用戶啟動應用程序一些相關的啟動細節。 ### [](file:///C:/Users/geekidentity/AppData/Local/Youdao/YNote/markdown/index.html#231-啟動失敗)23.1 啟動失敗 如果您的應用程序無法啟動,則注冊的FailureAnalyzers會提供專門的錯誤消息和具體操作來解決問題。 例如,如果您在端口8080上啟動Web應用程序,并且該端口已在使用中,則應該會看到類似于以下內容的內容: ``` *************************** APPLICATION FAILED TO START *************************** Description: Embedded servlet container failed to start. Port 8080 was already in use. Action: Identify and stop the process that's listening on port 8080 or configure this application to listen on another port. ``` > Spring Boot提供了眾多的FailureAnalyzer實現,您可以非常容易地[添加自己的實現](http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/#howto-failure-analyzer)。 如果沒有故障分析器(analyzers)能夠處理異常,您仍然可以顯示完整的自動配置報告,以更好地了解出現的問題。 為此,您需要[啟用debug屬性](http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/#boot-features-external-config)或啟用org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer的[DEBUG日志](http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/#boot-features-custom-log-levels)。 例如,如果使用java -jar運行應用程序,則可以按如下方式啟用 debug: ``` $ java -jar myproject-0.0.1-SNAPSHOT.jar --debug ``` ### [](file:///C:/Users/geekidentity/AppData/Local/Youdao/YNote/markdown/index.html#232-自定義banner)23.2 自定義Banner 可以通過在您的類路徑中添加一個 banner.txt 文件,或者將banner.location設置到banner文件的位置來更改啟動時打印的banner。 如果文件有一些不常用的編碼,你可以設置banner.charset(默認為UTF-8)。除了文本文件,您還可以將banner.gif,banner.jpg或banner.png圖像文件添加到您的類路徑中,或者設置一個banner.image.location屬性。 圖像將被轉換成ASCII藝術表現,并打印在任何文字banner上方。 您可以在banner.txt文件中使用以下占位符: #### [](file:///C:/Users/geekidentity/AppData/Local/Youdao/YNote/markdown/index.html#表231-banner變量)表23.1\. banner變量 | 變量名 | 描述 | | --- | --- | | ${application.version} | 在MANIFEST.MF中聲明的應用程序的版本號。例如, Implementation-Version: 1.0 被打印為 1.0. | | ${application.formatted-version} | 在MANIFEST.MF中聲明的應用程序版本號的格式化顯示(用括號括起來,以v為前綴)。 例如 (v1.0)。 | | ${spring-boot.version} | 您正在使用的Spring Boot版本。 例如1.5.2.RELEASE。 | | ${spring-boot.formatted-version} | 您正在使用格式化顯示的Spring Boot版本(用括號括起來,以v為前綴)。 例如(v1.5.2.RELEASE)。 | | ${[Ansi.NAME](http://ansi.name/)} (or ${[AnsiColor.NAME](http://ansicolor.name/)}, ${[AnsiBackground.NAME](http://ansibackground.name/)}, ${[AnsiStyle.NAME](http://ansistyle.name/)}) | 其中NAME是ANSI轉義碼的名稱。 有關詳細信息,請參閱?[AnsiPropertySource](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot/src/main/java/org/springframework/boot/ansi/AnsiPropertySource.java)。 | | ${application.title} | 您的應用程序的標題在MANIFEST.MF中聲明。 例如Implementation-Title:MyApp打印為MyApp。 | > 如果要以編程方式生成banner,則可以使用SpringApplication.setBanner()方法。 使用org.springframework.boot.Banner 如接口,并實現自己的printBanner() 方法。 您還可以使用spring.main.banner-mode屬性來決定是否必須在System.out(控制臺)上打印banner,使用配置的logger(log)或不打印(off)。 ### [](file:///C:/Users/geekidentity/AppData/Local/Youdao/YNote/markdown/index.html#233-定制springapplication)23.3 定制SpringApplication 如果SpringApplication默認值不符合您的想法,您可以創建本地實例并進行自定義。 例如,關閉banner: ``` public static void main(String[] args) { SpringApplication app = new SpringApplication(MySpringConfiguration.class); app.setBannerMode(Banner.Mode.OFF); app.run(args); } ``` > 傳遞給SpringApplication的構造函數參數是spring bean的配置源。 在大多數情況下,這些將引用@Configuration類,但它們也可以引用XML配置或應掃描的包。 也可以使用application.properties文件配置SpringApplication。 有關詳細信息,請參見[第24章“外部配置”](http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/#boot-features-external-config)。 有關配置選項的完整列表,請參閱[SpringApplication Javadoc](http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/api/org/springframework/boot/SpringApplication.html)。 ### [](file:///C:/Users/geekidentity/AppData/Local/Youdao/YNote/markdown/index.html#234-流式構建-api)23.4 流式構建 API 如果您需要構建一個ApplicationContext層次結構(具有父/子關系的多個上下文),或者如果您只想使用“流式(fluent)”構建器API,則可以使用SpringApplicationBuilder。 SpringApplicationBuilder允許您鏈式調用多個方法,并包括允許您創建層次結構的父和子方法。 例如: ``` new SpringApplicationBuilder() .sources(Parent.class) .child(Application.class) .bannerMode(Banner.Mode.OFF) .run(args); ``` > 創建ApplicationContext層次結構時有一些限制,例如 Web組件必須包含在子上下文中,并且相同的環境將用于父和子上下文。 有關詳細信息,請參閱[SpringApplicationBuilder Javadoc](http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/api/org/springframework/boot/builder/SpringApplicationBuilder.html)。 ### [](file:///C:/Users/geekidentity/AppData/Local/Youdao/YNote/markdown/index.html#235-application-events-and-listeners)23.5 Application events and listeners 除了常見的Spring Framework事件(如?[ContextRefreshedEvent](http://docs.spring.io/spring/docs/4.3.7.RELEASE/javadoc-api/org/springframework/context/event/ContextRefreshedEvent.html))之外,SpringApplication還會發送一些其他應用程序事件。 > 在創建ApplicationContext之前,實際上觸發了一些事件,因此您不能在@Bean上注冊一個監聽器。 您可以通過SpringApplication.addListeners(...) 或SpringApplicationBuilder.listeners(...)方法注冊它們。 > 如果您希望自動注冊這些偵聽器,無論創建應用程序的方式如何,都可以將META-INF / spring.factories文件添加到項目中,并使用org.springframework.context.ApplicationListener引用您的偵聽器。 org.springframework.context.ApplicationListener=com.example.project.MyListener 當您的應用程序運行時,事件按照以下順序發送: 1. ApplicationStartingEvent在運行開始時發送,但在注冊偵聽器和注冊初始化器之后。 2. 當已經知道要使用的上下文(context)環境,并在context創建之前,將發送ApplicationEnvironmentPreparedEvent。 3. ApplicationPreparedEvent在啟動刷新(refresh)之前發送,但在加載了bean定義之后。 4. ApplicationReadyEvent在刷新之后被發送,并且處理了任何相關的回調以指示應用程序準備好服務請求。 5. 如果啟動時發生異常,則發送ApplicationFailedEvent。 > 一般您不需要使用應用程序事件,但可以方便地知道它們存在。 在內部,Spring Boot使用事件來處理各種任務。 ### [](file:///C:/Users/geekidentity/AppData/Local/Youdao/YNote/markdown/index.html#236-web-環境)23.6 Web 環境 SpringApplication將嘗試代表您創建正確類型的ApplicationContext。 默認情況下,將使用AnnotationConfigApplicationContext或AnnotationConfigEmbeddedWebApplicationContext,具體取決于您是否正在開發Web應用程序。 用于確定“Web環境”的算法是相當簡單的(基于幾個類的存在)。 如果需要覆蓋默認值,可以使用setWebEnvironment(boolean webEnvironment)。 也可以通過調用setApplicationContextClass() 對ApplicationContext完全控制。 > 在JUnit測試中使用SpringApplication時,通常需要調用setWebEnvironment() ### [](file:///C:/Users/geekidentity/AppData/Local/Youdao/YNote/markdown/index.html#237-訪問應用程序參數)23.7 訪問應用程序參數 如果您需要訪問傳遞給SpringApplication.run()的應用程序參數,則可以注入org.springframework.boot.ApplicationArguments bean。 ApplicationArguments接口提供對原始String []參數以及解析選項和非選項參數的訪問: ``` 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"] } } ``` > Spring Boot還將向Spring Environment 注冊一個CommandLinePropertySource。 這允許您也使用@Value注解注入應用程序參數。 ### [](file:///C:/Users/geekidentity/AppData/Local/Youdao/YNote/markdown/index.html#238-使用applicationrunner或commandlinerunner)23.8 使用ApplicationRunner或CommandLineRunner SpringApplication啟動時如果您需要運行一些特定的代碼,就可以實現ApplicationRunner或CommandLineRunner接口。 兩個接口都以相同的方式工作,并提供一個單獨的運行方法,這將在SpringApplication.run(...)完成之前調用。 CommandLineRunner接口提供對應用程序參數的訪問(簡單的字符串數組),而ApplicationRunner使用上述的ApplicationArguments接口。 ``` @Component public class MyBean implements CommandLineRunner { public void run(String... args) { // Do something... } } ``` 如果定義了若干CommandLineRunner或ApplicationRunner bean,這些bean必須按特定順序調用,您可以實現org.springframework.core.Ordered接口,也可以使用org.springframework.core.annotation.Order注解。 ### [](file:///C:/Users/geekidentity/AppData/Local/Youdao/YNote/markdown/index.html#239-application-exit)23.9 Application exit 每個SpringApplication將注冊一個JVM關閉鉤子,以確保ApplicationContext在退出時正常關閉。 可以使用所有標準的Spring生命周期回調(例如DisposableBean接口或@PreDestroy注釋)。 另外,如果希望在應用程序結束時返回特定的退出代碼,那么bean可以實現org.springframework.boot.ExitCodeGenerator接口。 ### [](file:///C:/Users/geekidentity/AppData/Local/Youdao/YNote/markdown/index.html#2310-管理功能)23.10 管理功能 可以通過指定spring.application.admin.enabled屬性來為應用程序啟用與管理相關的功能。 這會在平臺MBeanServer上暴露SpringApplicationAdminMXBean。 您可以使用此功能來遠程管理您的Spring Boot應用程序。 這對于任何服務包裝器(service wrapper)實現也是有用的。 > 如果您想知道應用程序在哪個HTTP端口上運行,請使用local.server.port鍵獲取該屬性。 > 啟用此功能時請小心,因為MBean公開了關閉應用程序的方法。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看