文章作者:Tyan
博客:noahsnail.com ?|? CSDN ?|? 簡書
Part IV. Spring Boot features
This section dives into the details of Spring Boot. Here you can learn about the key features that you will want to use and customize. If you haven’t already, you might want to read the Part II, “Getting started” and Part III, “Using Spring Boot” sections so that you have a good grounding of the basics.
這一部分進入Spring Boot細節(jié)部分。在這部分你會了解到你想使用和定制的一些重要特性。如果你還沒準備好杯活,你可以閱讀第二部分“Getting started”和第三部分“Using Spring Boot”御滩,可以對基礎(chǔ)知識有個較好的認識怎茫。
23. SpringApplication
The SpringApplication
class provides a convenient way to bootstrap a Spring application that will be started from a main()
method. In many situations you can just delegate to the static SpringApplication.run
method:
SpringApplication
提供了一種很方便的方式來引導Spring應(yīng)用,Spring應(yīng)用可以從main()
方法中啟動奴愉。許多情況下你可以委托給靜態(tài)方法SpringApplication.run
:
public static void main(String[] args) {
SpringApplication.run(MySpringConfiguration.class, args);
}
When your application starts you should see something similar to the following:
當你的應(yīng)用啟動時你應(yīng)該看到類似于下面的東西:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: v1.4.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)
By default INFO
logging messages will be shown, including some relevant startup details such as the user that launched the application.
默認情況下會輸出INFO
日志信息蔬螟,包括一些相關(guān)的啟動細節(jié)例如啟動應(yīng)用的用戶此迅。
23.1 Startup failure
If your application fails to start, registered FailureAnalyzers
get a chance to provide a dedicated error message and a concrete action to fix the problem. For instance if you start a web application on port 8080
and that port is already in use, you should see something similar to the following:
如果你的應(yīng)用啟動失敗,注冊FailureAnalyzers
有可能會提供專門的錯誤信息和解決這個問題的具體行動。例如邮屁,如果你啟動一個8080
端口的web應(yīng)用并且這個端口已經(jīng)被占用整袁,你應(yīng)該會看到類似于下面的內(nèi)容:
***************************
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 provides numerous
FailureAnalyzer
implementations and you can add your own very easily.
?
Spring Boot提供了許多
FailureAnalyzer
實現(xiàn),你可以很容易添加自己的FailureAnalyzer
實現(xiàn)佑吝。
If no failure analyzers are able to handle the exception, you can still display the full auto-configuration report to better understand what went wrong. To do so you need to enable the debug
property or enable DEBUG
logging for org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer
.
如果沒有失敗分析器能處理這個異常坐昙,你仍可以顯示完整的自動配置報告,從而更好的理解什么地方出問題了芋忿。為了實現(xiàn)這個你需要啟用debug
屬性或啟用org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer
的DEBUG
日志炸客。
For instance, if you are running your application using java -jar
you can enable the debug
property as follows:
例如,如果你使用java -jar
運行應(yīng)用戈钢,你可以用下面的形式啟用debug
屬性:
$ java -jar myproject-0.0.1-SNAPSHOT.jar --debug
23.2 Customizing the Banner
The banner that is printed on start up can be changed by adding a banner.txt
file to your classpath, or by setting banner.location
to the location of such a file. If the file has an unusual encoding you can set banner.charset
(default is UTF-8
). In addition to a text file, you can also add a banner.gif
, banner.jpg
or banner.png
image file to your classpath, or set a banner.image.location
property. Images will be converted into an ASCII art representation and printed above any text banner.
啟動時打印的標語可以通過在classpath中添加一個banner.txt
文件或者將banner.location
設(shè)置為banner.txt
文件的位置來修改痹仙。如果文件是一種不常見的編碼方式,你可以設(shè)置banner.charset
(默認是UTF-8
)殉了。除了文本文件之外开仰,你也添加一個banner.gif
,banner.jpg
或banner.png
圖像文件到classpath中薪铜,或者設(shè)置一個banner.image.location
屬性众弓。圖像將被轉(zhuǎn)換成ASCII藝術(shù)表示并打印在文本標語之上。
Inside your banner.txt
file you can use any of the following placeholders:
在banner.txt
內(nèi)部你可以使用下面的任何占位符:
Table 23.1. Banner variables
Variable | Description |
---|---|
${application.version} | The version number of your application as declared in MANIFEST.MF . For example Implementation-Version: 1.0 is printed as 1.0 . |
${application.formatted-version} | The version number of your application as declared in MANIFEST.MF formatted for display (surrounded with brackets and prefixed with v ). For example (v1.0 ). |
${spring-boot.version} | The Spring Boot version that you are using. For example 1.4.2.RELEASE . |
${spring-boot.formatted-version} | The Spring Boot version that you are using formatted for display (surrounded with brackets and prefixed with v ). For example (v1.4.2.RELEASE ). |
${Ansi.NAME} (or ${AnsiColor.NAME}, ${AnsiBackground.NAME}, ${AnsiStyle.NAME}) | Where NAME is the name of an ANSI escape code. See AnsiPropertySource for details. |
${application.title} | The title of your application as declared in MANIFEST.MF . For example Implementation-Title: MyApp is printed as MyApp . |
Table 23.1. Banner變量
Variable | Description |
---|---|
${application.version} | 你的應(yīng)用的版本號在MANIFEST.MF 中聲明隔箍。 例如Implementation-Version: 1.0 打印成1.0 . |
${application.formatted-version} | 在MANIFEST.MF 中的聲明的應(yīng)用版本號進行格式化顯示(加上前綴v 并用括號包裹)谓娃。例如(v1.0) 。 |
${spring-boot.version} | 你使用的Spring Boot版本蜒滩。例如1.4.2.RELEASE . |
${spring-boot.formatted-version} | 你使用的Spring Boot版本進行格式化顯示加上前綴v 并用括號包裹)滨达。例如(v1.4.2.RELEASE) 。 |
${Ansi.NAME} (or ${AnsiColor.NAME}, ${AnsiBackground.NAME}, ${AnsiStyle.NAME}) |
NAME 是ANSI轉(zhuǎn)義碼的名字俯艰。更多細節(jié)請看AnsiPropertySource 捡遍。 |
${application.title} | 在MANIFEST.MF 中聲明的應(yīng)用標題。例如Implementation-Title: MyApp 打印成MyApp . |
The
SpringApplication.setBanner(…?)
method can be used if you want to generate a banner programmatically. Use theorg.springframework.boot.Banner
interface and implement your ownprintBanner()
method.
?
如果你想自動生成一個標語你可以使用
SpringApplication.setBanner(…?)
方法蟆炊。使用org.springframework.boot.Banner
接口并實現(xiàn)你自己的printBanner()
方法稽莉。
You can also use the spring.main.banner-mode
property to determine if the banner has to be printed on (console), using the configured logger (log) or not at all (off).
你也可以使用spring.main.banner-mode
屬性來決定標語是否必須在System.out
(控制臺)上輸出瀑志,使用配置的日志(log)或一點也不用(off)涩搓。
The printed banner will be registered as a singleton bean under the name pringBootBanner
.
輸出的banner會注冊名字為pringBootBanner
的單例bean。
YAML maps
off
tofalse
so make sure to add quotes if you want to disable the banner in your application.
spring:
main:
banner-mode: "off"
?
如果你想在你的應(yīng)用中禁用banner劈猪,YAML會將
off
映射為false
昧甘,因此要確保添加引用。
spring:
main:
banner-mode: "off"
23.3 Customizing SpringApplication
If the SpringApplication
defaults aren’t to your taste you can instead create a local instance and customize it. For example, to turn off the banner you would write:
如果你不喜歡默認的SpringApplication
战得,你可以創(chuàng)建一個本地實例并定制它充边。例如,關(guān)閉你寫的banner:
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MySpringConfiguration.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
The constructor arguments passed to
SpringApplication
are configuration sources for spring beans. In most cases these will be references to@Configuration
classes, but they could also be references to XML configuration or to packages that should be scanned.
?
傳給
SpringApplication
的構(gòu)造函數(shù)參數(shù)是Spring beans配置源。在大多數(shù)情況下將會引用@Configuration
類浇冰,但它們也可以引用XML配置或應(yīng)該掃描的包贬媒。
It is also possible to configure the SpringApplication
using an application.properties
file. See Chapter 24, Externalized Configuration for details.
也可以使用application.properties
文件配置SpringApplication
。更多細節(jié)請看24章肘习,『外部配置』际乘。
For a complete list of the configuration options, see the SpringApplication
Javadoc.
完整的配置選項列表,請看SpringApplication
文檔漂佩。
23.4 Fluent builder API
If you need to build an ApplicationContext
hierarchy (multiple contexts with a parent/child relationship), or if you just prefer using a fluent
builder API, you can use the SpringApplicationBuilder
.
如果你需要構(gòu)建ApplicationContext
分層(多個具有父/子關(guān)系的上下文)脖含,或者你更喜歡使用fluent
的構(gòu)建器API,你可以使用SpringApplicationBuilder
投蝉。
The SpringApplicationBuilder
allows you to chain together multiple method calls, and includes parent
and child
methods that allow you to create a hierarchy.
SpringApplicationBuilder
允許你鏈接多個方法調(diào)用养葵,包括允許你創(chuàng)建分層的parent
和child
方法。
For example:
例如:
new SpringApplicationBuilder()
.sources(Parent.class)
.child(Application.class)
.bannerMode(Banner.Mode.OFF)
.run(args);
There are some restrictions when creating an
ApplicationContext
hierarchy, e.g. Web components must be contained within the child context, and the sameEnvironment
will be used for both parent and child contexts. See theSpringApplicationBuilder
Javadoc for full details.
?
當創(chuàng)建
ApplicationContext
分層時有一些限制瘩缆,例如关拒,子上下文必須包含web組件,父子上下文將使用同一個Environment
庸娱。更完整的細節(jié)請看SpringApplicationBuilder
文檔夏醉。
23.5 Application events and listeners
In addition to the usual Spring Framework events, such as ContextRefreshedEvent
, a SpringApplication
sends some additional application events.
除了平常的Spring框架事件之外,例如ContextRefreshedEvent
涌韩,SpringApplication
會發(fā)送一些其它的應(yīng)用事件畔柔。
Some events are actually triggered before the
ApplicationContext
is created so you cannot register a listener on those as a@Bean
. You can register them via theSpringApplication.addListeners(…?)
orSpringApplicationBuilder.listeners(…?)
methods.
If you want those listeners to be registered automatically regardless of the way the application is created you can add a
META-INF/spring.factories
file to your project and reference your listener(s) using theorg.springframework.context.ApplicationListener
key.
org.springframework.context.ApplicationListener=com.example.project.MyListener
?
在
ApplicationContext
創(chuàng)建之前實際上會觸發(fā)一些事件,因此你不能使用@Bean
來注冊這些監(jiān)聽器臣樱。你可以通過SpringApplication.addListeners(…?)
或SpringApplicationBuilder.listeners(…?)
方法來注冊這些監(jiān)聽器靶擦。
如果你想自動注冊這些監(jiān)聽器,不管上下文的創(chuàng)建方式雇毫,你可以在你的工程中添加
META-INF/spring.factories
文件玄捕,并通過org.springframework.context.ApplicationListener
作為key來引用你的監(jiān)聽器。
org.springframework.context.ApplicationListener=com.example.project.MyListener
Application events are sent in the following order, as your application runs:
- An
ApplicationStartedEvent
, but before any processing except the registration of listeners and initializers. - An
ApplicationEnvironmentPreparedEvent
is sent when theEnvironment
to be used in the context is known, but before the context is created. - An
ApplicationPreparedEvent
is sent just before the refresh is started, but after bean definitions have been loaded. - An
ApplicationReadyEvent
is sent after the refresh and any related callbacks have been processed to indicate the application is ready to service requests. - An
ApplicationFailedEvent
is sent if there is an exception on startup.
當你的應(yīng)用運行時棚放,應(yīng)用事件以下面的順序發(fā)送:
- 在運行啟動時發(fā)送
ApplicationStartedEvent
枚粘,除了監(jiān)聽器和初始化器注冊之外,在進行任何處理之前發(fā)送飘蚯。 - 當在上下文中使用的
Environment
已知時馍迄,發(fā)送ApplicationEnvironmentPreparedEvent
,但發(fā)送是在上下文創(chuàng)建之前局骤。 - 在再刷新啟動之前攀圈,但在bean定義加載之后,發(fā)送
ApplicationPreparedEvent
峦甩。 - 在再刷新之后赘来,發(fā)送
ApplicationReadyEvent
,任何相關(guān)的回調(diào)函數(shù)都處理完成之后,意味著應(yīng)用已經(jīng)準備處理服務(wù)請求了犬辰。 - 如果啟動時出現(xiàn)異常嗦篱,發(fā)送
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.
?
經(jīng)常你不需要使用應(yīng)用事件,但知道它們的存在是便利的幌缝。Spring Boot內(nèi)部使用事件來處理大量的任務(wù)默色。
23.6 Web environment
A SpringApplication
will attempt to create the right type of ApplicationContext
on your behalf. By default, an AnnotationConfigApplicationContext
or AnnotationConfigEmbeddedWebApplicationContext
will be used, depending on whether you are developing a web application or not.
SpringApplication
會嘗試創(chuàng)建代表你的合適的ApplicationContext
類型。默認情況下狮腿,會使用AnnotationConfigApplicationContext
或AnnotationConfigEmbeddedWebApplicationContext
腿宰,依賴于你是否在開發(fā)一個web應(yīng)用。
The algorithm used to determine a ‘web environment’ is fairly simplistic (based on the presence of a few classes). You can use setWebEnvironment(boolean webEnvironment)
if you need to override the default.
使用的決定web environment
的算法是相對簡單的(基于現(xiàn)有的一些類)缘厢。如果你需要覆寫默認值你可以使用setWebEnvironment(boolean webEnvironment)
吃度。
It is also possible to take complete control of the ApplicationContext
type that will be used by calling setApplicationContextClass(…?)
.
完全控制ApplicationContext
類型也是可能的,通過調(diào)用setApplicationContextClass(…?)
使用贴硫。
It is often desirable to call
setWebEnvironment(false)
when usingSpringApplication
within a JUnit test.
?
當在JUnit測試時使用
SpringApplication
椿每,經(jīng)常需要調(diào)用setWebEnvironment(false)
。
23.7 Accessing application arguments
If you need to access the application arguments that were passed to SpringApplication.run(…?)
you can inject a org.springframework.boot.ApplicationArguments
bean. The ApplicationArguments
interface provides access to both the raw String[]
arguments as well as parsed option
and non-option
arguments:
如果你需要訪問傳進SpringApplication.run(…?)
中的應(yīng)用參數(shù)英遭,你可以注入org.springframework.boot.ApplicationArguments
bean间护。ApplicationArguments
接口提供了訪問原始String[]
和轉(zhuǎn)換的option
,non-option
參數(shù)挖诸。
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 will also register a
CommandLinePropertySource
with the SpringEnvironment
. This allows you to also inject single application arguments using the@Value
annotation.
?
Spring Boot也在Spring
Environment
中注冊CommandLinePropertySource
汁尺。這也允許你使用@Value
注解注入單個應(yīng)對參數(shù)。
23.8 Using the ApplicationRunner or CommandLineRunner
If you need to run some specific code once the SpringApplication
has started, you can implement the ApplicationRunner
or CommandLineRunner
interfaces. Both interfaces work in the same way and offer a single run
method which will be called just before SpringApplication.run(…?)
completes.
如果你需要在SpringApplication
啟動時運行一些特定的代碼多律,你可以實現(xiàn)ApplicationRunner
或CommandLineRunner
接口痴突。這兩個接口以同樣方式工作,并有一個單獨的run
方法狼荞,在SpringApplication.run(…?)
之前會調(diào)用這個run
方法辽装。
The CommandLineRunner
interfaces provides access to application arguments as a simple string array, whereas the ApplicationRunner
uses the ApplicationArguments
interface discussed above.
CommandLineRunner
接口提供了對應(yīng)用參數(shù)的訪問,應(yīng)用參數(shù)作為一個簡單的字符串數(shù)組相味,而ApplicationRunner
使用前面描述的ApplicationArguments
接口拾积。
import org.springframework.boot.*
import org.springframework.stereotype.*
@Component
public class MyBean implements CommandLineRunner {
public void run(String... args) {
// Do something...
}
}
You can additionally implement the org.springframework.core.Ordered
interface or use the org.springframework.core.annotation.Order
annotation if several CommandLineRunner
or ApplicationRunner
beans are defined that must be called in a specific order.
另外,如果定義的CommandLineRunner
或ApplicationRunner
beans必須以指定順序調(diào)用丰涉,你可以實現(xiàn)org.springframework.core.Ordered
接口或org.springframework.core.annotation.Order
注解拓巧。
23.9 Application exit
Each SpringApplication
will register a shutdown hook with the JVM to ensure that the ApplicationContext
is closed gracefully on exit. All the standard Spring lifecycle callbacks (such as the DisposableBean
interface, or the @PreDestroy
annotation) can be used.
為了確保ApplicationContext
在關(guān)閉時安全退出, 每個SpringApplication
都會在JVM中注冊一個關(guān)閉鉤子昔搂。所有的標準Spring生命周期回調(diào)函數(shù)(例如DisposableBean
接口玲销,或@PreDestroy
注解)都會被使用输拇。
In addition, beans may implement the org.springframework.boot.ExitCodeGenerator
interface if they wish to return a specific exit code when the application ends.
另外摘符,當應(yīng)用退出時,如果想返回一個指定的退出碼,beans可以實現(xiàn)org.springframework.boot.ExitCodeGenerator
接口逛裤。
23.10 Admin features
It is possible to enable admin-related features for the application by specifying the spring.application.admin.enabled
property. This exposes the SpringApplicationAdminMXBean
on the platform MBeanServer
. You could use this feature to administer your Spring Boot application remotely. This could also be useful for any service wrapper implementation.
如果應(yīng)用想啟用admin相關(guān)的功能瘩绒,可以指定spring.application.admin.enabled
屬性。這會在平臺MBeanServer
上暴露SpringApplicationAdminMXBean
带族。你可以使用這個功能遠程的管理你的Spring Boot應(yīng)用锁荔。對于任何服務(wù)包裹的實現(xiàn)這是很有用的。
If you want to know on which HTTP port the application is running, get the property with key
local.server.port
.
?
如果你想知道應(yīng)用運行的HTTP接口蝙砌,通過關(guān)鍵字
local.server.port
可以得到這個屬性阳堕。
?
Take care when enabling this feature as the MBean exposes a method to shutdown the application.
?
當啟用這個功能時要非常小心,因為MBean會暴露一個關(guān)閉應(yīng)用的方法择克。