Spring Boot中的Properties的使用詳解
簡介
本文我們將會討怎么在Spring Boot中使用Properties。使用Properties有兩種方式,一種是java代碼的注解,一種是xml文件的配置。本文將會主要關(guān)注java代碼的注解。
使用注解注冊一個Properties文件
注冊Properties文件我們可以使用@PropertySource 注解,該注解需要配合@Configuration 一起使用。
@Configuration @PropertySource("classpath:foo.properties") public class PropertiesWithJavaConfig { //... }
我們也可以使用placeholder來動態(tài)選擇屬性文件:
@PropertySource({ "classpath:persistence-${envTarget:mysql}.properties" }) @PropertySource也可以多次使用來定義多個屬性文件: @PropertySource("classpath:foo.properties") @PropertySource("classpath:bar.properties") public class PropertiesWithJavaConfig { //... }
我們也可以使用@PropertySources來包含多個@PropertySource:
@PropertySources({ @PropertySource("classpath:foo.properties"), @PropertySource("classpath:bar.properties") }) public class PropertiesWithJavaConfig { //... }
使用屬性文件
最簡單直接的使用辦法就是使用@Value注解:
@Value( "${jdbc.url}" ) private String jdbcUrl;
我們也可以給屬性添加默認(rèn)值:
@Value( "${jdbc.url:aDefaultUrl}" ) private String jdbcUrl;
如果要在代碼中使用屬性值,我們可以從Environment API中獲?。?/p>
@Autowired private Environment env; ... dataSource.setUrl(env.getProperty("jdbc.url"));
Spring Boot中的屬性文件
默認(rèn)情況下Spring Boot 會讀取application.properties文件作為默認(rèn)的屬性文件。當(dāng)然,我們也可以在命令行提供一個不同的屬性文件:
java -jar app.jar --spring.config.location=classpath:/another-location.properties
如果是在測試環(huán)境中,我們可以使用@TestPropertySource 來指定測試的屬性文件:
@RunWith(SpringRunner.class) @TestPropertySource("/foo.properties") public class FilePropertyInjectionUnitTest { @Value("${foo}") private String foo; @Test public void whenFilePropertyProvided_thenProperlyInjected() { assertThat(foo).isEqualTo("bar"); } }
除了屬性文件,我們也可以直接以key=value的形式:
@RunWith(SpringRunner.class) @TestPropertySource(properties = {"foo=bar"}) public class PropertyInjectionUnitTest { @Value("${foo}") private String foo; @Test public void whenPropertyProvided_thenProperlyInjected() { assertThat(foo).isEqualTo("bar"); } }
使用@SpringBootTest,我們也可以使用類似的功能:
@RunWith(SpringRunner.class) @SpringBootTest(properties = {"foo=bar"}, classes = SpringBootPropertiesTestApplication.class) public class SpringBootPropertyInjectionIntegrationTest { @Value("${foo}") private String foo; @Test public void whenSpringBootPropertyProvided_thenProperlyInjected() { assertThat(foo).isEqualTo("bar"); } }
@ConfigurationProperties
如果我們有一組屬性,想將這些屬性封裝成一個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=bar
Spring Boot將會自動將這些屬性文件映射成java bean的屬性,我們需要做的就是定義好prefix。
yaml文件
Spring Boot也支持yaml形式的文件,yaml對于層級屬性來說更加友好和方便,我們可以看下properties文件和yaml文件的對比:
database.url=jdbc:postgresql:/localhost:5432/instance database.username=foo database.password=bar secret: foo
database: url: jdbc:postgresql:/localhost:5432/instance username: foo password: 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
或者這樣:
export name=value
java -jar app.jar
環(huán)境變量有什么用呢? 當(dāng)指定了特定的環(huán)境變量時候,Spring Boot會自動去加載application-environment.properties文件,Spring Boot默認(rèn)的屬性文件也會被加載,只不過優(yōu)先級比較低。
## java代碼配置
除了注解和默認(rèn)的屬性文件,java也可以使用PropertySourcesPlaceholderConfigurer來在代碼中顯示加載:
@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; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springcloud Bus消息總線原理是實現(xiàn)詳解
Spring Cloud Bus 使用輕量級的消息代理來連接微服務(wù)架構(gòu)中的各個服務(wù),可以將其用于廣播狀態(tài)更改(例如配置中心配置更改)或其他管理指令,本文將對其用法進(jìn)行詳細(xì)介紹2022-09-09springboot整合xxl-job實現(xiàn)分布式定時任務(wù)的過程
XXL-JOB是一個分布式任務(wù)調(diào)度平臺,其核心設(shè)計目標(biāo)是開發(fā)迅速、學(xué)習(xí)簡單、輕量級、易擴(kuò)展,這篇文章主要介紹了springboot整合xxl-job分布式定時任務(wù),需要的朋友可以參考下2022-08-08