javascript
【译】Spring Boot Features
【更新中】本文大部分內容翻譯自官方文檔https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/html/spring-boot-features.html
文章目錄
- 1.Spring應用程序
- 1.1 啟動失敗
- 1.2 延遲初始化
- 1.3 自定義Banner
- 1.4 自定義SpringApplication
- 1.5 構建流式API
- 1.6 Application事件和監(jiān)聽器
- 1.7 Web Environment
- 1.8 訪問應用程序參數
- 1.9 使用ApplicationRunner或CommandLineRunner
- 1.10 應用程序退出
- 1.11 管理特性
- 2. 外部化配置
- 2.1 配置隨機值
- 2.2 訪問命令行屬性
- 2.3 應用程序屬性文件
- 2.4 Profile-specific屬性
- 2.5 占位符屬性
- 2.6 加密屬性
- 2.7 使用YAML代替Properties
- 2.7.1 加載YAML
- 2.7.2 將YAML作為Properties公開在Spring環(huán)境
- 2.7.3 Multi-profile YAML文檔
- 2.7.4 YAML的不足
- 2.8 類型安全的配置屬性
- 2.8.1 JavaBean屬性綁定
- 2.8.2 構造方法綁定
- 2.8.3 開啟```@ConfigurationProperties```注解類型
- 2.8.4 使用```@ConfigurationProperties```注解類型
- 2.8.5 第三方配置
- 2.8.6 寬松的綁定
- 2.8.7 合并復雜類型
- 2.8.8 屬性轉換
- 轉換時間(Converting durations)
- 轉換數據大小(Converting Data Sizes)
- 2.8.9 @ConfigurationProperties校驗
- 2.8.10 @ConfigurationProperties和@Value
- 3. 配置文件
- 3.1 添加活動的配置文件
- 3.2 以編程方式設置配置文件
- 3.3 Profile-specific配置文件
1.Spring應用程序
The SpringApplication類提供了一種方便的方法來引導從main()方法啟動的Spring應用程序。你可以委托給SpringApplication.run方法,如下所示:
public static void main(String[] args) {SpringApplication.run(Application20200106.class, args); }應用程序啟動,你應該看到類似以下輸出:
. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: v2.2.2.RELEASE2019-04-31 13:09:54.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) 2019-04-31 13:09:54.166 INFO 56603 --- [ main] ationConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6e5a8246: startup date [Wed Jul 31 00:08:16 PDT 2013]; root of context hierarchy 2019-04-01 13:09:56.912 INFO 41370 --- [ main] .t.TomcatServletWebServerFactory : Server initialized with port: 8080 2019-04-01 13:09:57.501 INFO 41370 --- [ main] o.s.b.s.app.SampleApplication : Started SampleApplication in 2.992 seconds (JVM running for 3.658)默認情況下,會顯示INFO日志消息,包括一些相關的啟動細節(jié),比如啟動應用程序的用戶。
如果你需要除了INFO級別的日志信息,你可以設置日志級別。
應用程序版本是從主程序類包中的實現版本確定的。
設置spring.main.log-startup-info為false可以關閉啟動日志信息記錄。
這還將關閉應用程序活動配置文件的日志記錄。
要在啟動期間添加額外的日志記錄,您可以在SpringApplication的子類中重寫logStartupInfo(boolean)。
1.1 啟動失敗
如果您的應用程序啟動失敗,注冊的FailureAnalyzers將有機會提供專用的錯誤消息和修復問題的具體操作。
例如,如果您在端口8080上啟動一個web應用程序,并且該端口已經在使用,您應該看到類似于以下消息:
Spring Boot提供了很多FailureAnalyzers實現,你也可以創(chuàng)建自定義的故障分析器
如果沒有故障分析器能處理異常,你也可以顯示完整的條件報告,以便更好地理解出錯的原因。您需要為org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener啟用debug屬性或啟用DEBUG日志記錄。
例如,如果您使用java -jar運行您的應用程序,您可以啟用debug屬性,如下所示:
java -jar myproject-0.0.1-SNAPSHOT.jar --debug1.2 延遲初始化
開啟延遲初始化可以減少應用程序所需的世界,在web應用程序中,啟用延遲初始化將導致在接收到HTTP請求之前許多與web相關的bean不會被初始化。
延遲初始化的缺點是較晚得發(fā)現應用程序中的問題。如果一個錯誤配置的bean是延遲初始化的,在啟動期間不會出現故障,故障會發(fā)生bean被初始化的時候。
還必須注意確保JVM有足夠的內存來容納應用程序的所有bean,而不僅僅是只容納那些在啟動期間初始化的bean。
由于這些原因,延遲初始化默認是禁用的,建議在開啟延遲初始化之前對JVM的堆大小進行微調(fine-tuning)。
用編程方式開啟延遲初始化:
使用SpringApplicationBuilder的lazyInitialization方法:
使用SpringApplication的setLazyInitialization方法:
SpringApplication springApplication = new SpringApplication(Application20200106.class); springApplication.setLazyInitialization(true);配置文件開啟延遲從初始化:
使用spring.main.lazy-initialization屬性:
如果你希望一部分bean使用延遲初始化,一部分bean禁用延遲初始化,你可以使用@Lazy(false)注解。
- lazy = true,表示延遲,默認為true
- lazy = false,表示不延遲
1.3 自定義Banner
在classpath中添加一個banner.txt文件或者設置spring.banner.location屬性來改變在啟動期間打印的banner。
如果文件編碼不是UTF-8,需要設置spring.banner.charset。
除了文本文件,還可以在classpath中添加banner.gif,banner.jpg,banner.png圖片文件。或者設置spring.banner.image.location屬性。
圖片被轉換成ASCII碼打印在任何文本banner上方。
在banner.txt文件中,可以使用下列占位符:
表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版本,如2.2.2.RELEASE。 |
| ${spring-boot.formatted-version} | 你在使用的Spring Boot版本,按照格式顯示(用圓括號括起來并且加上前綴v)。如v2.2.2.RELEASE。 |
| ${Ansi.NAME} (or ${AnsiColor.NAME}, ${AnsiBackground.NAME}, ${AnsiStyle.NAME}) | NAME是ANSI轉義碼的名稱,詳情參見AnsiPropertySource。 |
| ${application.title} | 應用程序的Title,和MANIFEST.MF一樣。如Implementation-Title: MyApp打印成MyApp。 |
如果你想用編程的方式生成一個banner,可以使用SpringApplication.setBanner(…)。
實現org.springframework.boot.Banner接口并重寫printBanner()方法。
You can also use the spring.main.banner-mode property to determine if the banner has to be printed on System.out (console), sent to the configured logger (log), or not produced at all (off).
你也可以使用spring.main.banner-mode屬性控制banner是否在System.out(console)被打印,或者發(fā)送到已配置的日志程序,或者關閉。
打印banner的bean被注冊成一個單例bean,名字為:springBootBanner。
1.4 自定義SpringApplication
如果默認的SpringApplication不是你的菜,你可以創(chuàng)建一個本地實例并對其設置。例如,關閉banner:
public static void main(String[] args) {SpringApplication springApplication = new SpringApplication(Application20200106.class);springApplication.setBannerMode(Banner.Mode.OFF);springApplication.run(args); }傳給SpringApplication的構造方法參數是Spring beans的配置源。在大多數情況下,都是引用@Configuration類,但是也可以引用XML配置或引用被掃描的包。
引用XML配置:
ResourceLoader resourceLoader = new ClassPathXmlApplicationContext("config/spring/user/applicationContext-user.xml"); SpringApplication springApplication = new SpringApplication(resourceLoader,Application20200106.class);引用被掃描的包:
配置類UserConfiguration:
package cn.shrmus.springboot.demo20200106.configuration;import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;@ComponentScan("cn.shrmus.springboot.demo20200106.user") @Configuration public class UserConfiguration { }啟動類Application20200106:
@Import(value = cn.shrmus.springboot.demo20200106.configuration.UserConfiguration.class) @SpringBootApplication public class Application20200106{public static void main(String[] args) {ResourceLoader resourceLoader = new AnnotationConfigApplicationContext("cn.shrmus.springboot.demo20200106.user"); // ResourceLoader resourceLoader = new AnnotationConfigApplicationContext(cn.shrmus.springboot.demo20200106.configuration.UserConfiguration.class);SpringApplication springApplication = new SpringApplication(resourceLoader,Application20200106.class);ConfigurableApplicationContext configurableApplicationContext = springApplication.run(args);UserController userController = configurableApplicationContext.getBean(UserController.class);System.out.println(userController);SpringApplication.exit(configurableApplicationContext);}也可以使用application.properties文件配置SpringApplication,詳情參考外部化配置。
要查看SpringApplication的完整配置,參考SpringApplicationJavadoc。
1.5 構建流式API
如果需要構建一個ApplicationCOntext層次結構(具有父/子關系的多個上下文),或者你更喜歡使用構建“流式”API,你可以使用SpringApplicationBuilder。
The SpringApplicationBuilderlets you chain together multiple method calls and includes parentand childmethods that let you create a hierarchy, as shown in the following example:
使用SpringApplicationBuilder將包含parent和child方法等多個方法的調用都鏈在一起,以此創(chuàng)建一個層次結構,如下:
new SpringApplicationBuilder().sources(Parent.class).child(Application.class).bannerMode(Banner.Mode.OFF).run(args);創(chuàng)建ApplicationContext層次結構會有一些限制,Web組件被包含在子上下文中,父上下文和子上下文都使用同一個Environment。閱讀SpringApplicationBuilderJavadoc查看詳情。
1.6 Application事件和監(jiān)聽器
除了常見的Spring框架事件(如ContextRefreshedEvent)外,SpringApplication還發(fā)送一些額外的應用程序事件。
有些事件是在創(chuàng)建ApplicationContext之前觸發(fā)的,所以不能將監(jiān)聽器注冊成@Bean,你可以使用SpringApplication.addListeners(…)方法或SpringApplicationBuilder.listeners(…)方法注冊它們。
如果你希望監(jiān)聽器能自動注冊,不管應用程序時如何創(chuàng)建的,你可以在META-INF/spring.factories文件中使用org.springframework.context.ApplicationListener添加監(jiān)聽器的引用。如下:
org.springframework.context.ApplicationListener=cn.shrmus.springboot.demo20200106.listener.MyListener在程序運行時,應用程序事件按一下順序發(fā)送:
上面的列表只包含綁定到SpringApplication中的SpringApplicationEvent。除此之外,以下事件會在ApplicationPreparedEvnet之后和ApplicationStartedEvent之前發(fā)布:
A WebServerInitializedEventis sent after the WebServeris ready. ServletWebServerInitializedEventand ReactiveWebServerInitializedEventare the servlet and reactive variants respectively.
通常不需要使用應用程序事件,但是知道它們的存在是很方便的。在內部,Spring Boot使用事件處理各種任務。
Application events are sent by using Spring Framework’s event publishing mechanism. Part of this mechanism ensures that an event published to the listeners in a child context is also published to the listeners in any ancestor contexts. As a result of this, if your application uses a hierarchy of SpringApplicationinstances, a listener may receive multiple instances of the same type of application event.
應用程序事件是使用Spring框架的事件發(fā)布機制發(fā)送的。此機制的一部分確保將事件發(fā)布到子上下文中的監(jiān)聽器,也能將事件發(fā)布到任何祖先上下文的監(jiān)聽器。因此,如果你的應用程序使用了SpringApplication實例的層次結構,則監(jiān)聽器可能會接收同一類型應用程序事件的多個實例。
To allow your listener to distinguish between an event for its context and an event for a descendant context, it should request that its application context is injected and then compare the injected context with the context of the event. The context can be injected by implementing ApplicationContextAwareor, if the listener is a bean, by using @Autowired.
允許監(jiān)聽器區(qū)分它的上下文事件和子上下文的事件,它應該請求注入它的應用程序上下文,然后將注入的上下文與事件的上下文比較。上下文可以通過ApplicationContextAware實現注入,或者,如果監(jiān)聽器是bean,可以通過@Autowired注入。
1.7 Web Environment
一個SpringApplication會替你創(chuàng)建正確類型的ApplicationContext。用于確定WebApplicationType的算法相當簡單:
- 如果存在Spring MVC,使用AnnotationConfigServletWebServerApplicationContext
- 如果不在存Spring MVC,但是存在Spring WebFlux,使用AnnotationConfigReactiveWebServerApplicationContext
- 否則,使用AnnotationConfigApplicationContext
這意味著如果你同一個應用程序中使用Spring MVC和來自于Spring WebFlux的WebClinet,那么默認情況下使用Spring MVC。
你也可以通過調用setWebApplicationType(WebApplicationType)來覆蓋它。
還可以通過調用setApplicationContextClass(…)完全控制ApplicationContext類型。
在Junit測試中使用ApplicationContext時,通常需要調用setWebApplicationType(WebApplicationType.NONE)。
1.8 訪問應用程序參數
如果你需要訪問傳遞給SpringApplication.run(…)的應用程序參數,則需要注入org.springframework.boot.ApplicationArguments。
The ApplicationArgumentsinterface provides access to both the raw String[]arguments as well as parsed optionand non-optionarguments.
接口ApplicationArguments提供對原始String[]參數和解析過的option和non-option參數的訪問。如下:
import org.springframework.boot.*; import org.springframework.beans.factory.annotation.*; import org.springframework.stereotype.*;@Component public class MyBean {@Autowiredpublic 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注解注入單個應用程序參數。
1.9 使用ApplicationRunner或CommandLineRunner
如果你需要在SpringApplication啟動后運行一些特定的代碼,你可以實現ApplicationRunner或者CommandLineRunner接口。
這兩個接口以相同的方式工作,并提供一個單一的run方法,這個方法在SpringApplication.run(…)結束之前被調用。
接口CommandLineRunner以簡單字符串數組的形式提供對應用程序參數的訪問,而ApplicationRunner使用談論過的ApplicationArguments。下面的例子展示帶run方法的CommandLineRunner:
import org.springframework.boot.*; import org.springframework.stereotype.*;@Component public class MyBean implements CommandLineRunner {public void run(String... args) {// Do something...} }如果定義了多個CommandLineRunner或ApplicationRunnerbean,必須按特定的順序調用它們。可以通過實現org.springframework.core.Ordered接口或org.springframework.core.annotation.Order注解控制調用順序。
1.10 應用程序退出
每個SpringApplication向JVM注冊一個shotdown hook確保ApplicationContext在退出時能夠優(yōu)雅地關閉。所有標準的Spring生命周期回調(例如DisposableBean接口或@PreDestroy注解)都會被使用。
另外,如果希望SpringApplication.exit()被調用時返回特殊的退出碼,可以實現org.springframework.boot.ExitCodeGenerator接口。這個退出碼會被傳遞給System.exit()作為狀態(tài)碼返回。如下:
@SpringBootApplication public class ExitCodeApplication {@Beanpublic ExitCodeGenerator exitCodeGenerator() {return () -> 42;}public static void main(String[] args) {System.exit(SpringApplication.exit(SpringApplication.run(ExitCodeApplication.class, args)));} }此外,接口ExitCodeGenerator可以由異常實現。當遇到這樣的異常,Spring Boot將返回退出碼,退出碼是實現這個接口時重寫getExitCode()方法提供的退出碼。
import org.springframework.boot.ExitCodeGenerator;public class CustomizingException extends Exception implements ExitCodeGenerator {@Overridepublic int getExitCode() {return 43;} }1.11 管理特性
通過指定spring.application.admin.enabled屬性來為應用程序開啟管理相關的特性。這將在MBeanServer平臺上公開SpringApplicationAdminMXBean。你可以使用這個特性來管理你的Spring Boot遠程應用程序。這個特性對于任何服務包裝器實現都是有用的。
如果你想知道應用程序在哪個HTTP端口上運行,獲取local.server.port屬性的值。
2. 外部化配置
Spring Boot允許您將配置外部化,以便可以在不同的環(huán)境中使用相同的應用程序代碼。你可以使用properties文件,YAML文件,環(huán)境變量和命令行參數外部化配置。屬性值可以通過使用@Value注解直接注入到bean中。通過Spring的Environment抽象訪問,或通過@ConfigurationProperties綁定到結構化對象。
Spring Boot使用一種非常特殊的PropertySource順序,其設計目的是允許合理地覆蓋值。屬性按以下順序排序:
舉一個具體的例子,假設你開發(fā)的一個@Component類使用name屬性,如下:
import org.springframework.stereotype.*; import org.springframework.beans.factory.annotation.*;@Component public class MyBean {@Value("${name}")private String name;// ...}在你的應用程序classpath(jar內)中的application.properties文件為name提供一個合理的默認屬性值。在新environment中運行時,可以在jar之外提供一個application.properties文件覆蓋name。對于一次性測試,可以使用特定的命令行開關啟動(java -jar app.jar --name="Spring")。
在帶有環(huán)境變量的命令行上提供SPRING_APPLICATION_JSON屬性,使用下面的UNIX shell:
$ SPRING_APPLICATION_JSON='{"acme":{"name":"test"}}' java -jar myapp.jar在前面的例子中,在Spring Environment以acme.name=test結束。你還可以在系統(tǒng)屬性中用spring.application.json提供JSON。如下:
$ java -Dspring.application.json='{"name":"test"}' -jar myapp.jar還可以使用命令行參數提供JSON,如下:
$ java -jar myapp.jar --spring.application.json='{"name":"test"}'還可以使用JNDI變量提供JSON,如下:
java:comp/env/spring.application.json2.1 配置隨機值
用于注入隨機值的RandomValuePropertySource是非常有用的(作為秘鑰或測試用例)。它可以生成integers,longs,uuids,strings。如下:
my.secret=${random.value} my.number=${random.int} my.bignumber=${random.long} my.uuid=${random.uuid} my.number.less.than.ten=${random.int(10)} my.number.in.range=${random.int[1024,65536]}語法:random.int*的語法是OPEN value (,max) CLOSE,其中OPEN,CLOSE是任意字符,value,max是整數。如果提供max,則value為最小值,max為最大值(不含)。
2.2 訪問命令行屬性
默認情況下,SpringApplication將任何命令行選項參數(參數以--開頭,如--server.port=9000)轉換為property,并將它們添加到Spring Environment中。
As mentioned previously, command line properties always take precedence over other property sources.
就如前面所說,命令行屬性始終優(yōu)先于其它屬性源。
如果不希望將命令行屬性添加到Environment中,可以使用SpringApplication.setAddCommandLineProperties(false)來禁用。
2.3 應用程序屬性文件
加載配置文件:SpringApplication從以下位置的application.properties文件中加載屬性并將屬性添加到Spring Environment中:
列表按優(yōu)先級排序(在列表中較高位置定義的屬性覆蓋在較低位置定義的屬性)。
你也可以使用YAML(‘yml’)文件替代’.properties’。
如果不想用application.properties作為配置文件名,你也可以指定spring.config.name環(huán)境屬性來切換到另一個文件名。還可以使用spring.config.location環(huán)境屬性來引用一個顯示的位置(以逗號分隔的目錄位置或文件路徑)。
通過spring.config.name指定一個文件名:
$ java -jar myproject.jar --spring.config.name=myproject通過spring.config.location指定文件位置:
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.propertiesspring.config.name和spring.config.location很早就用于確定必須加載哪些文件。它們必須定義為環(huán)境屬性(通常是操作系統(tǒng)環(huán)境變量,系統(tǒng)屬性,命令行參數)。
如果spring.config.location包含目錄(與文件相反),它們應該以/結尾(在運行時,在加載前附加由spring.config.name生成的名稱,包含profile-specific文件名)。spring.config.location中指定的文件按原樣使用,不支持profile-specific變體,并被任何profile-specific屬性覆蓋。
配置位置以相反的順序搜索。默認情況下,配置位置是classpath:/,classpath:/config/,file:./,file:./config/。搜索結果的順序如下:
當使用spring.config.location自定義配置位置時,它們替代默認的位置。例如,如果spring.config.location的值被配置成classpath:/custom-config/,file:./custom-config/,搜索順序如下:
作為選擇,當使用spring.config.additional-location自定義配置位置時,它們被使用除默認位置外。附加位置在默認位置之前被搜索。例如,如果附加位置classpath:/custom-config/,file:./custom-config/被配置,搜索順序如下:
This search ordering lets you specify default values in one configuration file and then selectively override those values in another. You can provide default values for your application in application.properties(or whatever other basename you choose with spring.config.name) in one of the default locations. These default values can then be overridden at runtime with a different file located in one of the custom locations.
這種搜索順序允許您在一個配置文件中指定默認值,然后有選擇地在另一個配置文件中覆蓋這些值。你可以在一個默認位置的application.properties中為你的應用程序提供默認值(或你選擇的任何其他基本的spring.config.name)。在運行時使用位于自定義位置中另一個文件覆蓋這些默認值。
如果使用環(huán)境變量而不是系統(tǒng)屬性,大部分操作系統(tǒng)不允許點分隔(period-separated)的鍵名,但你可以使用下劃線(SPRING_CONFIG_NAME代替spring.config.name)。
如果應用程序在容器中運行,然后可以使用JNDI屬性(在java:comp/env中)或servlet上下文初始化參數來代替環(huán)境變量或系統(tǒng)屬性。
2.4 Profile-specific屬性
除application.properties文件之外,profile-specific屬性也能通過使用application-{profile}.properties命名約定被定義。Environment有一組默認配置文件(默認情況下,[default]),如果沒有設置活動配置文件,就使用這些默認配置文件。換句話說,如果沒有配置文件被明確激活,那么屬性從application-default.properties被加載。
Profile-specific屬性從與標準application.properties相同的位置加載,使用profile-specific文件總是覆蓋non-specific文件,不管profile-specific文件位于打包的jar的內部或外部。
If several profiles are specified, a last-wins strategy applies. For example, profiles specified by the spring.profiles.activeproperty are added after those configured through the SpringApplicationAPI and therefore take precedence.
如果多個配置文件被指定,則應用最后配置的策略。例如,spring.profiles.active屬性指定的配置文件添加在通過SpringApplicationAPI配置之后,因此優(yōu)先。
假設application.properties中有如下配置:
spring.profiles.active=pro,dev最終生效的是application-dev.properties文件。
如果你在spring.config.location指定了一些文件,profile-specific的變體不被考慮。如果你想使用profile-specific屬性,在spring.config.location中使用目錄。
2.5 占位符屬性
文件application.properties中的值在使用時通過現有的Environment過濾,所以你可以返回到以前定義的值(例如,系統(tǒng)屬性中定義的值)。
app.name=MyApp app.description=${app.name} is a Spring Boot application您還可以使用此技術創(chuàng)建現有Spring Boot屬性的“短”變體。點擊howto.html查看詳情。
2.6 加密屬性
Spring Boot不提供任何對屬性值加密的支持,然而,它提供了修改Spring Environment中包含的值所必須的hook點。EnvironmentPostProcessor接口允許你在應用啟動之前操作(manipulate)Environment。點擊howto.html查看詳情。
如果您正在尋找一種安全的方式來存儲憑證和密碼,Spring Cloud Vault項目提供了在HashiCorp Vault中存儲外部化配置的支持。
2.7 使用YAML代替Properties
YAML是JSON的超集,因此是指定分層配置數據的一種方便的格式。當你的classpath中有SnakeYAML庫時,SpringApplication類自動支持YAML作為properties的另一種選擇。
SnakeYAML是由spring-boot-starter自動提供的。
2.7.1 加載YAML
Spring框架提供了兩個方便的類用于加載YAML文檔。YamlPropertiesFactoryBean將YAML加載為Properties,YamlMapFactoryBean將YAML加載為Map。
例如:參考以下YAML文檔:
environments:dev:url: https://dev.example.comname: Developer Setupprod:url: https://another.example.comname: My Cool App將上述YAML文檔轉換為下列properties:
environments.dev.url=https://dev.example.com environments.dev.name=Developer Setup environments.prod.url=https://another.example.com environments.prod.name=My Cool AppYAML列表用[index]作為引用,代表屬性鍵。如下:
my:servers:- dev.example.com- another.example.com將上述YAML文檔轉換為下列properties:
my.servers[0]=dev.example.com my.servers[1]=another.example.com通過使用Spring Boot的Binder工具(@ConfigurationProperties就是這么做的)來綁定這樣的屬性。如果在目標bean中有java.util.List(或者Set)類型的屬性,你需要提供setter或使用可變值初始化它。下面的例子綁定到上述屬性:
@ConfigurationProperties(prefix="my") public class Config {private List<String> servers = new ArrayList<String>();public List<String> getServers() {return this.servers;} }2.7.2 將YAML作為Properties公開在Spring環(huán)境
類YamlPropertySourceLoader可將YAML作為PropertySource公開在Spring環(huán)境中。這樣就可以使用帶有占位符語法的@Value注解來訪問YAML屬性。
2.7.3 Multi-profile YAML文檔
通過使用spring.profiles鍵,你可以在一個文件中指定多個profile-specific YAML文檔,指定文檔何時應用,如下:
server:address: 192.168.1.100 --- spring:profiles: development server:address: 127.0.0.1 --- spring:profiles: production & eu-central server:address: 192.168.1.120如果development配置文件是激活的,那么server.address的屬性值為127.0.0.1。
同理,如果production和eu-central配置文件是激活的,server.address的屬性值為192.168.1.120。
如果development,production和eu-central配置文件都沒有啟用,那么server.address的屬性值為192.168.1.100。
spring.profiles可以包含簡單的配置文件名(如production)或配置文件表達式。配置文件表達式允許表達更復雜的配置文件邏輯,如production & (eu-central | eu-west)。查看參考指南了解更多詳情。
如果在應用程序上下文啟動時沒有顯式激活配置文件,則默認配置文件將被激活。因此,在下列YAML中,我們?yōu)閟pring.security.user.password設置了一個僅在默認配置文件中可用的值:
server:port: 8000 --- spring:profiles: defaultsecurity:user:password: weak然而,在下面的例子中,密碼總是被設置,因為它沒有附加到任何配置文件中,而且它必須在所有其他配置文件中被顯式重置:
server:port: 8000 spring:security:user:password: weakSpring profiles designated by using the spring.profileselement may optionally be negated by using the !character. If both negated and non-negated profiles are specified for a single document, at least one non-negated profile must match, and no negated profiles may match.
使用spring.profiles元素指定的Spring配置文件通過使用!字符選擇性地否定。如果為單個文檔指定了否定配置文件和非否定配置文件,至少有一個非否定的配置文件必須匹配,并且否定的配置文件不能匹配。
2.7.4 YAML的不足
YAML文件不能通過@PropertySource注解加載。因此,在需要以這種方式加載值的情況下,需要使用屬性文件。
在profile-specific YAML文件中使用multi YAML文檔語法可能導致意外發(fā)生。如下:
application-dev.yml
server:port: 8000 --- spring:profiles: "!test"security:user:password: "secret"如果使用參數--spring.profiles.active=dev運行應用程序,你可能希望security.user.password設置為secret,但是并不會。
嵌套的文檔將被過濾,因為主文件名為application-dev.yml。它已經被認為是profile-specific,嵌套文檔將被忽略。
建議不要將profile-specific YAML文件和多個YAML文檔混合使用。只使用其中一個。
2.8 類型安全的配置屬性
使用@Value("${property}")注解注入配置屬性有時會很麻煩(cumbersome),特別(especially)是在處理多個屬性或數據本質上是分層的情況下。Spring Boot提供了另一種處理屬性的方法,允許強類型bean控制和驗證應用程序的配置。
請參見@Value和類型安全的配置屬性之間的區(qū)別。
2.8.1 JavaBean屬性綁定
可以綁定一個聲明標準JavaBean屬性的bean,如下面的例子所示:
package com.example;import java.net.InetAddress; import java.util.ArrayList; import java.util.Collections; import java.util.List;import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties("acme") public class AcmeProperties {private boolean enabled;private InetAddress remoteAddress;private final Security security = new Security();public boolean isEnabled() { ... }public void setEnabled(boolean enabled) { ... }public InetAddress getRemoteAddress() { ... }public void setRemoteAddress(InetAddress remoteAddress) { ... }public Security getSecurity() { ... }public static class Security {private String username;private String password;private List<String> roles = new ArrayList<>(Collections.singleton("USER"));public String getUsername() { ... }public void setUsername(String username) { ... }public String getPassword() { ... }public void setPassword(String password) { ... }public List<String> getRoles() { ... }public void setRoles(List<String> roles) { ... }} }上述POJO定義了下列屬性:
- acme.enabled,默認值為false。
- acme.remote-address,可以強制使用來自String的類型。
- acme.security.username,嵌套的"security"對象,其名稱由屬性的名稱決定。特別是(In particular),返回類型沒有被使用,可能是SecurityProperties。
- acme.security.password。
- acme.security.roles,默認是USER的String集合。
Spring Boot自動配置大量使用@ConfigurationProperties來輕松配置自動配置的bean。
類似于自動配置類,在Spring Boot中可用的@ConfigurationProperties類僅供內部使用。
The properties that map to the class, which are configured via properties files, YAML files, environment variables etc., are public API but the content of the class itself is not meant to be used directly.
映射到類的屬性(通過屬性文件、YAML文件、環(huán)境變量等配置)是公共API,但是類本身的內容并不意味著可以直接(directly)使用。
這種安排依賴于(relies on)默認的空構造函數,getter和setter通常是強制性的(mandatory),因為綁定是通過標準的Java bean屬性描述符進行的,就像在Spring MVC中一樣。在下列情況下可省略setter:
- 只要映射被初始化,就需要一個getter,但不一定是setter,因為綁定器可以對它們進行修改。
- 可以通過索引(通常使用YAML)或使用單個逗號分隔(comma-separated)的值(屬性)訪問集合和數組。在后一個情況下,setter是必需的。我們建議始終為此類型添加setter。如果你初始化一個集合,請確保它是可變的(not immutable)(如前面的實例所示)。
- 如果初始化了嵌套的POJO屬性(如前面示例中的Security字段),則不需要setter。如果你想用綁定器使用其默認構造方法動態(tài)(on the fly)創(chuàng)建一個實例,則需要setter。
有些人使用Lombok自動添加getter和setter。請確保Lombok不會為這樣的類型生成任何特定的(particular)構造方法,因為容器會自動使用它來實例化對象。
最后,只有標準Java Bean屬性被支持,不支持綁定靜態(tài)屬性。
2.8.2 構造方法綁定
上一節(jié)的示例可以在不變的情況下(in an immutable fashion)重寫,如下所示:
package com.example;import java.net.InetAddress; import java.util.List;import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConstructorBinding; import org.springframework.boot.context.properties.DefaultValue;@ConstructorBinding @ConfigurationProperties("acme") public class AcmeProperties {private final boolean enabled;private final InetAddress remoteAddress;private final Security security;public AcmeProperties(boolean enabled, InetAddress remoteAddress, Security security) {this.enabled = enabled;this.remoteAddress = remoteAddress;this.security = security;}public boolean isEnabled() { ... }public InetAddress getRemoteAddress() { ... }public Security getSecurity() { ... }public static class Security {private final String username;private final String password;private final List<String> roles;public Security(String username, String password,@DefaultValue("USER") List<String> roles) {this.username = username;this.password = password;this.roles = roles;}public String getUsername() { ... }public String getPassword() { ... }public List<String> getRoles() { ... }}}在這個設置中,@ConstructorBinding注解用于指示(indicate)應該使用構造函數綁定。
This means that the binder will expect to find a constructor with the parameters that you wish to have bound.
這意味著綁定器將期望找到帶參數的構造方法,而這些參數是已綁定的。
具有@ConstructorBinding類的內部類(如上面示例中的Security)也將通過其構造函數綁定。
可以使用@DefaultValue指定默認值,并用相同的轉換服務將String值強制(coerce)轉換為缺失屬性的目標類型。
要使用構造函數綁定,必須使用啟動@EnableConfigurationProperties或配置屬性掃描。由常規(guī)Spring機制創(chuàng)建的bean(如@Componentbean,通過(via)@Bean方法創(chuàng)建的bean,或者通過@Import加載的bean)不能使用構造函數綁定。
如果您的類有多個構造方法,您還可以直接在需要綁定的構造方法上使用@ConstructorBinding。
2.8.3 開啟@ConfigurationProperties注解類型
Spring Boot提供了綁定@ConfigurationProperties類型并將它們注冊為bean的基礎設施(infrastructure)。你可以逐個類地啟用配置屬性,或啟用與組件掃描工作方式類似的配置屬性掃描。
有時候,用@ConfigurationProperties注解的類不適合掃描,例如,如果您正在開發(fā)自己的自動配置,或者希望有條件地啟用它們。在這些情況下,使用@EnableConfigurationProperties注解指定要處理的類型列表。這個注解可以用在任何@Configuration類上,如下:
@Configuration(proxyBeanMethods = false) @EnableConfigurationProperties(AcmeProperties.class) public class MyConfiguration { }要想使用配置屬性掃描,要將@ConfigurationPropertiesScan注解添加到你的應用程序中。通常(typically),它被添加到使用@SpringBootApplication注解的主應用程序類中,但@ConfigurationPropertiesScan注解也可以被添加到任何@Configuration類中。默認情況下,將對聲明注解的類的包進行掃描。如果你想掃描特定的包,你可以按下面的例子來做:
@SpringBootApplication @ConfigurationPropertiesScan({ "com.example.app", "org.acme.another" }) public class MyApplication { }當@ConfigurationPropertiesbean使用配置屬性掃描或通過@EnableConfigurationProperties注冊時,這個bean戶有一個按照慣例的(conventional)名稱:<prefix>-<fqn>,其中<prefix>是在@ConfigurationProperties注解中指定的環(huán)境鍵前綴,<fqn>是bean的全限定名(the fully qualified name of the bean)。如果注解不提供任何前綴,則只使用bean的全限定名。
上述2.8.2的例子中的bean名是acme-com.example..AcmeProperties。
建議@ConfigurationProperties只處理(deal with)環(huán)境,特別(in particular)是不從上下文注入其他bean。對于極端(corner)情況,可以使用setter注入或框架提供的任何*Aware接口(例如你要訪問Environment可以使用EnvironmentAware)。如果你仍然要使用構造方法注入其他bean,則必須使用@Component注解配置屬性bean,并使用JavaBean屬性綁定。
2.8.4 使用@ConfigurationProperties注解類型
這種配置方式在SpringApplication外部的YAML配置中特別好用,如下面的例子所示:
# application.ymlacme:remote-address: 192.168.1.1security:username: adminroles:- USER- ADMIN# additional configuration as required要使用@ConfigurationPropertiesbean,你可以與任何其他bean相同的方式注入它們,如下所示:
@Service public class MyService {private final AcmeProperties properties;@Autowiredpublic MyService(AcmeProperties properties) {this.properties = properties;}//...@PostConstructpublic void openConnection() {Server server = new Server(this.properties.getRemoteAddress());// ...}}Using @ConfigurationProperties also lets you generate metadata files that can be used by IDEs to offer auto-completion for your own keys.
使用@ConfigurationProperties還可以生成元數據文件,IDE可以使用這些元數據文件為你的鍵提供自動完成功能。詳見appendix。
2.8.5 第三方配置
除了(as well as)使用@ConfigurationProperties注解類外,您還可以在公共@Bean方法上使用它。當你想將屬性綁定到超出你控制范圍外的第三方組件時,這樣做特別有用。
從Environment屬性配置bean,將@ConfigurationProperties添加到它的bean registration中,如下所示:
@ConfigurationProperties(prefix = "another") @Bean public AnotherComponent anotherComponent() {... }Any JavaBean property defined with the anotherprefix is mapped onto that AnotherComponentbean in manner similar to the preceding AcmePropertiesexample.
使用another前綴定義的任何JavaBean屬性都將以類似前面的(preceding)AcmeProperties示例的方式映射到AnotherComponentbean。
2.8.6 寬松的綁定
Spring Boot使用一些寬松的(relaxed)規(guī)則將Environment屬性綁定到@ConfigurationPropertiesbeans中,因此,Environment屬性名和bean屬性名不需要完全匹配(exact match)。有用的常見示例包括短橫線分隔的(dash-speparated)環(huán)境屬性(如context-path綁定到contextPath),以及大寫的(capitalized)環(huán)境屬性(如PORT綁定到port)。如下所示:
@ConfigurationProperties(prefix="acme.my-project.person") public class OwnerProperties {private String firstName;public String getFirstName() {return this.firstName;}public void setFirstName(String firstName) {this.firstName = firstName;}}使用上述代碼,可以使用以下屬性名:
表2 寬松的綁定
| acme.my-project.person.first-name | 短橫線分隔的(kebab case)命名方式,建議在.properties和.yml文件中使用。 |
| acme.myProject.person.firstName | 標準駝峰命名法(Standard camel case syntax)。 |
| acme.my_project.person.first_name | 下劃線命名法(Underscore notation),它是.properties和.yml文件中使用的另一種格式(alternative format)。 |
| ACME_MYPROJECT_PERSON_FIRSTNAME | 大寫命名法(upper case format),建議用于系統(tǒng)環(huán)境變量。 |
注解的prefix值必須是短橫線分隔的命名方式(小寫用-分隔,如acme.my-project..person)。
表3 每個屬性源的寬松的綁定規(guī)則
| Properties文件 | 駝峰命名法,短橫線分隔命名法,或下劃線命名法 | 使用[]的標準列表語法(syntax)或逗號分隔的值(comma-separated) |
| YAML文件 | 駝峰命名法,短橫線分隔命名法,或下劃線命名法 | 標準的YAML列表語法或逗號分隔的值 |
| 環(huán)境變量 | 用下劃線作為分隔符的大寫命名法。_不應在屬性名中使用 | 被下劃線環(huán)繞(surrounded)的數值(numeric values)。例如MY_ACME_1_OTHER = my.acme[1].other |
| 系統(tǒng)變量 | 駝峰命名法,短橫線分隔命名法,或下劃線命名法 | 使用[]的標準列表語法(syntax)或逗號分隔的值(comma-separated) |
建議屬性存儲為小寫的短橫線分隔的命名方法,如my.property-name=acme。
綁定到Map屬性時,如果key包含除了(other than)小寫字母,數字字符(alpha-numeric characters),或-之外的任何內容,你需要使用括號符號(bracket notation),以便原來的(original)值被保存(is preserved)。如果key沒有被[]環(huán)繞(is not surrounded),任何不是字母數字或-的字符都被刪除。例如,以下屬性綁定到Map:
acme:map:"[/key1]": value1"[/key2]": value2/key3: value3上面的屬性將綁定到以/key1、/key2和key3作為map中的key的Map。
For YAML files, the brackets need to be surrounded by quotes for the keys to be parsed properly.
YAML文件,要正確解析鍵,方括號(brackets)需要用引號(quotes)包圍。
2.8.7 合并復雜類型
當有多個列表配置在多個地方,通過覆蓋來替換整個列表。
例如,假設MyPojo對象的name和description屬性默認為null。下面展示一個AcmeProperties類中包含MyPojo列表:
@ConfigurationProperties("acme") public class AcmeProperties {private final List<MyPojo> list = new ArrayList<>();public List<MyPojo> getList() {return this.list;} }使用下面的配置:
acme:list:name: my namedescription: my description --- spring:profiles: dev acme:list:name: my another name如果dev配置文件不是活動的,AcmeProperties.list包含了一個MyPojo,如前面定義的那樣。如果dev配置文件被啟用,list仍然只包含一個元素(name值為my another name并且description值為null),該配置不會向list中添加第二個MyPojo實例,并且不會合并項(items)。
當List被指定在多個配置文件中時,具有最高優(yōu)先級的那個(并且只使用那個)被使用。如下所示:
acme:list:name: my namedescription: my descriptionname: another namedescription: another description --- spring:profiles: dev acme:list:name: my another name在前面的例子中,如果dev配置文件是活動的,AcmeProperties.list包含了一個MyPojo(name值為my another name并且description值為null)。對于YAML,可以使用逗號分隔的列表和YAML列表來完全覆蓋list的內容。
對于Map屬性,你可以綁定來自多個源的屬性值。但是,對于多個源中的相同屬性,將使用具有最高優(yōu)先級的屬性。下面展示一個AcmeProperties類中包含Map<String, MyPojo>:
@ConfigurationProperties("acme") public class AcmeProperties {private final Map<String, MyPojo> map = new HashMap<>();public Map<String, MyPojo> getMap() {return this.map;} }使用下面的配置:
acme:map:key1:name: my name 1description: my description 1 --- spring:profiles: dev acme:map:key1:name: dev name 1key2:name: dev name 2description: dev description 2如果dev配置文件不是活動的,AcmeProperties.map包含一個key1(name值為my name 1并且description值為my description 1)。如果dev文件被啟用,map包含兩個元素key1(name值為dev name 1并且description值為my description 1)和key2(name值為dev name 2并且description值為dev description 2)
前面的合并規(guī)則適用于來自所有屬性源的屬性,而不僅僅是YAML文件。
2.8.8 屬性轉換
Spring Boot綁定到@ConfigurationPropertiesbean時,它嘗試將外部應用程序屬性強制轉換為正確的類型。如果需要自定義類型轉換,你可以提供一個ConversionService類(使用命名為conversionService的類)或自定義屬性編輯器(通過CustomEditorConfigurer類)或自定義轉換器Converters(使用注釋為@ConfigurationPropertiesBinding的類)。
因為這個bean是在應用程序生命周期的早期請求的,因此確保限制ConversionService使用的依賴項。通常,任何你需要的依賴項可能在創(chuàng)建時沒有完全初始化。你可能希望重命名自定義ConversionService,如果不需要配置keys則強制執(zhí)行,并且只依賴于使用@ConfigurationPropertiesBinding的自定義轉換器。
轉換時間(Converting durations)
Spring Boot提供專門的工具來表達時間。如果你公開一個java.time.Duration屬性,在應用程序屬性中可使用以下格式:
- 常規(guī)long表達(使用毫秒(milliseconds)作為默認單元,除非已指定@DurationUnit)
- java.time.Duration使用的標準ISO-8601格式
- 一種更具可讀性的格式,值和單位是耦合(coupled)的(例如10s表示10秒)
如下所示:
@ConfigurationProperties("app.system") public class AppSystemProperties {@DurationUnit(ChronoUnit.SECONDS)private Duration sessionTimeout = Duration.ofSeconds(30);private Duration readTimeout = Duration.ofMillis(1000);public Duration getSessionTimeout() {return this.sessionTimeout;}public void setSessionTimeout(Duration sessionTimeout) {this.sessionTimeout = sessionTimeout;}public Duration getReadTimeout() {return this.readTimeout;}public void setReadTimeout(Duration readTimeout) {this.readTimeout = readTimeout;} }指定30秒后會話超時,30,PT30S,30s都可以。讀取超時為500ms,可以用500,PT0.5S,500ms。
還可以使用任何支持的單位,如:
- ns表示納秒(nanosecond)
- us表示微秒(microsecond)
- ms表示毫秒(millisecond)
- s表示秒(second)
- m表示分(minute)
- h表示時(hour)
- d表示天(day)
默認單位是毫秒,可以使用@DurationUnit覆蓋,如上面的例子所示。
如果你從以前的版本升級到使用Long來表示時間,如果切換到Duration的單位不是毫秒,請確保使用@DurationUnit定義這個單位。這樣做提供了一個透明的升級路徑,同時提供更豐富的格式。
轉換數據大小(Converting Data Sizes)
Spring框架有一個以字節(jié)表示大小的DataSize值類型。如果你公開一個DataSize屬性,在應用程序屬性中可使用以下格式:
- 常規(guī)long表達(使用字節(jié)作為默認單元,除非已指定@DataSizeUnit)
- java.time.Duration使用的標準ISO-8601格式
- 一種更具可讀性的格式,值和單位是耦合(coupled)的(例如10MB表示10兆)
如下所示:
@ConfigurationProperties("app.io") public class AppIoProperties {@DataSizeUnit(DataUnit.MEGABYTES)private DataSize bufferSize = DataSize.ofMegabytes(2);private DataSize sizeThreshold = DataSize.ofBytes(512);public DataSize getBufferSize() {return this.bufferSize;}public void setBufferSize(DataSize bufferSize) {this.bufferSize = bufferSize;}public DataSize getSizeThreshold() {return this.sizeThreshold;}public void setSizeThreshold(DataSize sizeThreshold) {this.sizeThreshold = sizeThreshold;} }指定10兆字節(jié)的緩沖區(qū)大小,10,10MB都可以。256字節(jié)的大小閾值可以指定為256,256B。
還可以使用任何支持的單位,如:
- B表示字節(jié)(byte)
- KB表示千字節(jié)(kilobyte)
- MB表示兆字節(jié)(megabyte)
- GB表示千兆字節(jié),吉字節(jié)(gigabyte)
- TB表示兆兆字節(jié),太字節(jié)(terabyte)
默認單位是字節(jié),可以使用@DataSizeUnit覆蓋,如上面的例子所示。
如果你從以前的版本升級到使用Long來表示字節(jié),如果切換到DataSize的單位不是字節(jié),請確保使用@DataSizeUnit定義這個單位。這樣做提供了一個透明的升級路徑,同時提供更豐富的格式。
2.8.9 @ConfigurationProperties校驗
當用Spring的@Validated注解類時,Spring引導嘗試驗證ConfigurationProperties類。你可以直接在配置類上使用JSR-303 javax.validation約束注解。要做到這一點,請確保兼容的JSR-303實現在classpath上,然后向字段中添加約束注解,如下所示:
@ConfigurationProperties(prefix="acme") @Validated public class AcmeProperties {@NotNullprivate InetAddress remoteAddress;// ... getters and setters}你還可以通過使用注解@Bean來觸發(fā)驗證,該方法使用@Validated創(chuàng)建配置屬性。
確保始終對嵌套(nested)屬性觸發(fā)驗證,即使沒有找到屬性,也必須使用@Valid注解關聯的(associated)字段。下面的例子建立在前面的AcmeProperties例子上:
@ConfigurationProperties(prefix="acme") @Validated public class AcmeProperties {@NotNullprivate InetAddress remoteAddress;@Validprivate final Security security = new Security();// ... getters and setterspublic static class Security {@NotEmptypublic String username;// ... getters and setters} }你還可以通過創(chuàng)建一個名為configurationPropertiesValidator的bean definition來添加自定義Spring Validator。@Bean方法應該聲明為靜態(tài)的。配置屬性驗證器(validator)是在應用程序生命周期的早期創(chuàng)建的,將@Bean方法聲明為靜態(tài)方法可以在創(chuàng)建bean的時候不用實例化(instantiate)@Configuration類。這樣做可以避免任何可能由早期實例化引起的問題。
模塊spring-boot-actuator包含一個公開所有@ConfigurationPropertiesbean的終端。將web瀏覽器指向/actuator/configprops或使用等效的JMX終端。點擊生產預備特性查看詳情。
2.8.10 @ConfigurationProperties和@Value
注解@Value是核心容器的功能,它不提供與類型安全配置屬性相同的特性。下表總結了@ConfigurationProperties和@Value支持的特性:
| 寬松的綁定(Relaxed binding) | Yes | No |
| 元數據支持(Meta-data support) | Yes | No |
| SpEL表達式 | No | Yes |
如果你為自己的組件定義了一組(a set of)配置keys,建議將它們分組到帶有@ConfigurationProperties注解的POJO中。你還應該注意(aware),由于@Value不支持寬松的綁定,因此如果你需要使用環(huán)境變量來提供值,那么它就不是一個好的選擇。
最后,雖然您可以在@Value中編寫SpEL表達式,但是這樣的表達式不會從應用程序屬性文件中處理。
3. 配置文件
Spring配置文件提供了一種方法來隔離(segregate)應用程序配置的各個部分,并使其僅在某些環(huán)境中可用。任何@Component、@Configuration或@ConfigurationProperties都可以標記為@Profile來限制它的加載時間,如下所示:
@Configuration(proxyBeanMethods = false) @Profile("production") public class ProductionConfiguration {// ...}如果ConfigurationProperties類是通過EnableConfigurationProperties注冊的而不是通過自動掃描注冊的,@Profile注解需要在具有@EnableConfigurationProperties注解的@Configuration類上指定。在掃描@ConfigurationProperties的情況下,可以在@ConfigurationProperties類本身上指定@Profile。
你可以使用spring.profiles.active``````Environment屬性指定哪些配置文件是活動的。你可以使用前面描述的任何方式指定屬性。例如,你可以在application.properties中包含它,如下所示:
spring.profiles.active=dev,hsqldb你還可以在命令行使用參數:--spring.profiles.active=dev,hsqldb
3.1 添加活動的配置文件
屬性spring.profiles.active遵循與其他屬性相同的順序規(guī)則:優(yōu)先使用優(yōu)先級最高的PropertySource。這意味著你可以在application.properties中指定活動配置文件,然后使用命令行開關替換它們。
有時,將profil-specific文件的屬性添加到active profiles比替換更有用。spring.profiles.include屬性可用于無條件地添加active profiles。SpringApplication入口點還有一個用于設置其他配置文件的Java API(也就是說,在spring.profiles.active屬性激活的對象之上)。參見SpringApplication中的setAdditionalProfiles()方法。
例如,當使用開關運行具有以下屬性的應用程序時,--spring.profiles.active=prod,proddb和prodmq配置文件也被激活:
--- my.property: fromyamlfile --- spring.profiles: prod spring.profiles.include:- proddb- prodmq請記住,可以在YAML文檔中定義spring.profiles屬性,以確定配置中何時包含此特定文檔。有關詳細信息,請閱讀howto.html。
3.2 以編程方式設置配置文件
在應用程序運行之前,可以通過調用SpringApplication.setAdditionalProfiles(…)以編程方式設置active profiles。也可以通過使用Spring的ConfigurableEnvironment接口來激活配置文件。
3.3 Profile-specific配置文件
Profile-specific的兩個不同的設置,application.properties(或application.yml)和通過@ConfigurationProperties引用的文件都被視為文件并加載。有關詳細信息,請閱讀Profile-specific屬性。
總結
以上是生活随笔為你收集整理的【译】Spring Boot Features的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jsp+ssm计算机毕业设计民宿客栈管理
- 下一篇: 如何用ChemDraw选择结构