Spring Boot中的Properties的使用详解
简介
本文我们将会讨怎么在SpringBoot中使用Properties。使用Properties有两种方式,一种是java代码的注解,一种是xml文件的配置。本文将会主要关注java代码的注解。
使用注解注册一个Properties文件
注册Properties文件我们可以使用@PropertySource注解,该注解需要配合@Configuration一起使用。
@Configuration
@PropertySource("classpath:foo.properties")
publicclassPropertiesWithJavaConfig{
//...
}
我们也可以使用placeholder来动态选择属性文件:
@PropertySource({
"classpath:persistence-${envTarget:mysql}.properties"
})
@PropertySource也可以多次使用来定义多个属性文件:
@PropertySource("classpath:foo.properties")
@PropertySource("classpath:bar.properties")
publicclassPropertiesWithJavaConfig{
//...
}
我们也可以使用@PropertySources来包含多个@PropertySource:
@PropertySources({
@PropertySource("classpath:foo.properties"),
@PropertySource("classpath:bar.properties")
})
publicclassPropertiesWithJavaConfig{
//...
}
使用属性文件
最简单直接的使用办法就是使用@Value注解:
@Value("${jdbc.url}")
privateStringjdbcUrl;
我们也可以给属性添加默认值:
@Value("${jdbc.url:aDefaultUrl}")
privateStringjdbcUrl;
如果要在代码中使用属性值,我们可以从EnvironmentAPI中获取:
@Autowired
privateEnvironmentenv;
...
dataSource.setUrl(env.getProperty("jdbc.url"));
SpringBoot中的属性文件
默认情况下SpringBoot会读取application.properties文件作为默认的属性文件。当然,我们也可以在命令行提供一个不同的属性文件:
java-jarapp.jar--spring.config.location=classpath:/another-location.properties
如果是在测试环境中,我们可以使用@TestPropertySource来指定测试的属性文件:
@RunWith(SpringRunner.class)
@TestPropertySource("/foo.properties")
publicclassFilePropertyInjectionUnitTest{
@Value("${foo}")
privateStringfoo;
@Test
publicvoidwhenFilePropertyProvided_thenProperlyInjected(){
assertThat(foo).isEqualTo("bar");
}
}
除了属性文件,我们也可以直接以key=value的形式:
@RunWith(SpringRunner.class)
@TestPropertySource(properties={"foo=bar"})
publicclassPropertyInjectionUnitTest{
@Value("${foo}")
privateStringfoo;
@Test
publicvoidwhenPropertyProvided_thenProperlyInjected(){
assertThat(foo).isEqualTo("bar");
}
}
使用@SpringBootTest,我们也可以使用类似的功能:
@RunWith(SpringRunner.class)
@SpringBootTest(properties={"foo=bar"},classes=SpringBootPropertiesTestApplication.class)
publicclassSpringBootPropertyInjectionIntegrationTest{
@Value("${foo}")
privateStringfoo;
@Test
publicvoidwhenSpringBootPropertyProvided_thenProperlyInjected(){
assertThat(foo).isEqualTo("bar");
}
}
@ConfigurationProperties
如果我们有一组属性,想将这些属性封装成一个bean,则可以考虑使用@ConfigurationProperties。
@ConfigurationProperties(prefix="database")
publicclassDatabase{
Stringurl;
Stringusername;
Stringpassword;
//standardgettersandsetters
}
属性文件如下:
database.url=jdbc:postgresql:/localhost:5432/instance database.username=foo database.password=bar
SpringBoot将会自动将这些属性文件映射成javabean的属性,我们需要做的就是定义好prefix。
yaml文件
SpringBoot也支持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环境变量
我们可以这样传入property环境变量:
java-jarapp.jar--property="value"
~~shell
java-Dproperty.name="value"-jarapp.jar
或者这样:
exportname=value
java-jarapp.jar
环境变量有什么用呢?当指定了特定的环境变量时候,SpringBoot会自动去加载application-environment.properties文件,SpringBoot默认的属性文件也会被加载,只不过优先级比较低。
##java代码配置
除了注解和默认的属性文件,java也可以使用PropertySourcesPlaceholderConfigurer来在代码中显示加载:
@Bean
publicstaticPropertySourcesPlaceholderConfigurerproperties(){
PropertySourcesPlaceholderConfigurerpspc
=newPropertySourcesPlaceholderConfigurer();
Resource[]resources=newClassPathResource[]
{newClassPathResource("foo.properties")};
pspc.setLocations(resources);
pspc.setIgnoreUnresolvablePlaceholders(true);
returnpspc;
}
本文的例子可以参考:https://github.com/ddean2009/learn-springboot2/tree/master/springboot-properties](https://github.com/ddean2009/learn-springboot2/tree/master/springboot-properties
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。