javascript
Spring Boot中的Properties
文章目錄
- 簡(jiǎn)介
- 使用注解注冊(cè)一個(gè)Properties文件
- 使用屬性文件
- Spring Boot中的屬性文件
- @ConfigurationProperties
- yaml文件
- Properties環(huán)境變量
- java代碼配置
Spring Boot中的Properties
簡(jiǎn)介
本文我們將會(huì)討怎么在Spring Boot中使用Properties。使用Properties有兩種方式,一種是java代碼的注解,一種是xml文件的配置。本文將會(huì)主要關(guān)注java代碼的注解。
使用注解注冊(cè)一個(gè)Properties文件
注冊(cè)Properties文件我們可以使用@PropertySource 注解,該注解需要配合@Configuration 一起使用。
@Configuration @PropertySource("classpath:foo.properties") public class PropertiesWithJavaConfig {//... }我們也可以使用placeholder來(lái)動(dòng)態(tài)選擇屬性文件:
@PropertySource({ "classpath:persistence-${envTarget:mysql}.properties" })@PropertySource也可以多次使用來(lái)定義多個(gè)屬性文件:
@PropertySource("classpath:foo.properties") @PropertySource("classpath:bar.properties") public class PropertiesWithJavaConfig {//... }我們也可以使用@PropertySources來(lái)包含多個(gè)@PropertySource:
@PropertySources({@PropertySource("classpath:foo.properties"),@PropertySource("classpath:bar.properties") }) public class PropertiesWithJavaConfig {//... }使用屬性文件
最簡(jiǎn)單直接的使用辦法就是使用@Value注解:
@Value( "${jdbc.url}" ) private String jdbcUrl;我們也可以給屬性添加默認(rèn)值:
@Value( "${jdbc.url:aDefaultUrl}" ) private String jdbcUrl;如果要在代碼中使用屬性值,我們可以從Environment API中獲取:
@Autowired private Environment env; ... dataSource.setUrl(env.getProperty("jdbc.url"));Spring Boot中的屬性文件
默認(rèn)情況下Spring Boot 會(huì)讀取application.properties文件作為默認(rèn)的屬性文件。當(dāng)然,我們也可以在命令行提供一個(gè)不同的屬性文件:
java -jar app.jar --spring.config.location=classpath:/another-location.properties如果是在測(cè)試環(huán)境中,我們可以使用@TestPropertySource 來(lái)指定測(cè)試的屬性文件:
@RunWith(SpringRunner.class) @TestPropertySource("/foo.properties") public class FilePropertyInjectionUnitTest {@Value("${foo}")private String foo;@Testpublic void whenFilePropertyProvided_thenProperlyInjected() {assertThat(foo).isEqualTo("bar");} }除了屬性文件,我們也可以直接以key=value的形式:
@RunWith(SpringRunner.class) @TestPropertySource(properties = {"foo=bar"}) public class PropertyInjectionUnitTest {@Value("${foo}")private String foo;@Testpublic void whenPropertyProvided_thenProperlyInjected() {assertThat(foo).isEqualTo("bar");} }使用@SpringBootTest,我們也可以使用類(lèi)似的功能:
@RunWith(SpringRunner.class) @SpringBootTest(properties = {"foo=bar"}, classes = SpringBootPropertiesTestApplication.class) public class SpringBootPropertyInjectionIntegrationTest {@Value("${foo}")private String foo;@Testpublic void whenSpringBootPropertyProvided_thenProperlyInjected() {assertThat(foo).isEqualTo("bar");} }@ConfigurationProperties
如果我們有一組屬性,想將這些屬性封裝成一個(gè)bean,則可以考慮使用@ConfigurationProperties。
@ConfigurationProperties(prefix = "database") public class Database {String url;String username;String password;// standard getters and setters }屬性文件如下:
database.url=jdbc:postgresql:/localhost:5432/instance database.username=foo database.password=barSpring Boot將會(huì)自動(dòng)將這些屬性文件映射成java bean的屬性,我們需要做的就是定義好prefix。
yaml文件
Spring Boot也支持yaml形式的文件,yaml對(duì)于層級(jí)屬性來(lái)說(shuō)更加友好和方便,我們可以看下properties文件和yaml文件的對(duì)比:
database.url=jdbc:postgresql:/localhost:5432/instance database.username=foo database.password=bar secret: foo database:url: jdbc:postgresql:/localhost:5432/instanceusername: foopassword: bar secret: foo注意yaml文件不能用在@PropertySource中。如果你使用@PropertySource,則必須指定properties文件。
Properties環(huán)境變量
我們可以這樣傳入property環(huán)境變量:
java -jar app.jar --property="value"~~shell
java -Dproperty.name=“value” -jar app.jar
環(huán)境變量有什么用呢? 當(dāng)指定了特定的環(huán)境變量時(shí)候,Spring Boot會(huì)自動(dòng)去加載application-environment.properties文件,Spring Boot默認(rèn)的屬性文件也會(huì)被加載,只不過(guò)優(yōu)先級(jí)比較低。
java代碼配置
除了注解和默認(rèn)的屬性文件,java也可以使用PropertySourcesPlaceholderConfigurer來(lái)在代碼中顯示加載:
@Bean public static PropertySourcesPlaceholderConfigurer properties(){PropertySourcesPlaceholderConfigurer pspc= new PropertySourcesPlaceholderConfigurer();Resource[] resources = new ClassPathResource[ ]{ new ClassPathResource( "foo.properties" ) };pspc.setLocations( resources );pspc.setIgnoreUnresolvablePlaceholders( true );return pspc; }本文的例子可以參考:https://github.com/ddean2009/learn-springboot2/tree/master/springboot-properties
更多精彩內(nèi)容且看:
- 區(qū)塊鏈從入門(mén)到放棄系列教程-涵蓋密碼學(xué),超級(jí)賬本,以太坊,Libra,比特幣等持續(xù)更新
- Spring Boot 2.X系列教程:七天從無(wú)到有掌握Spring Boot-持續(xù)更新
- Spring 5.X系列教程:滿足你對(duì)Spring5的一切想象-持續(xù)更新
- java程序員從小工到專(zhuān)家成神之路(2020版)-持續(xù)更新中,附詳細(xì)文章教程
更多教程請(qǐng)參考 flydean的博客
總結(jié)
以上是生活随笔為你收集整理的Spring Boot中的Properties的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Spring Boot的TestRest
- 下一篇: SpringBoot @Configur