config.name=configname
config.password=configpassword
@Configuration @ConfigurationProperties(prefix = "config") @PropertySource("classpath:/config.properties") public class Config { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
@EnableConfigurationProperties({Config.class})
使用的时候直接注入即可
@Autowired
Config config;
@ComponentScans({@ComponentScan("com.dx.controller"), @ComponentScan("app.config")})
@Configuration /** * 进行包扫描 * @author Administrator * */ /* @ComponentScan(basePackages = "com.dxz.demo.configuration") */ /** * 导入其他spring配置文件 * @author Administrator * */ /* @ImportResource("classpath:applicationContext-configuration.xml") */ /** * 导入其他@Configuration * @author Administrator * */ /* @Import(UserConfig.class) */ public class SpringConfig { public SpringConfig() { System.out.println("初始化spring配置文件SpringConfig"); } @Bean(name = "role") public Role role() { return new Role(); } /** * 嵌套@Configuration 必须时静态类 * @author Administrator * */ /* * @Configuration static class DatabaseConfig { //@Bean } */ public static void main(String[] args) { //获取SpringConfig配置文件,已注解的方式@Configuration ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); Role role = (Role) context.getBean("role"); } }
@Value("${test.u}")
private String name = "用户名";
properties 获取pom中的信息
pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-clound.version>Greenwich.SR1</spring-clound.version>
<eureka-server.version>2.1.1.RELEASE</eureka-server.version>
<spring-boot.version>2.1.3.RELEASE</spring-boot.version>
<hostname>127.0.0.1</hostname>
</properties>
注意其中原来的@hostname@我已经改成了${hostname}这种方式。
如果项目不能运行,那么需要重新mvn -clean然后再mvn -compile。
如果按照上面这种方式如何获取呢?
那么在pom.xml文件添加一个插件(plugin),原来的变量声明还是不变:如下
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
@Configuration @ConditionalOnProperty(value = "parentName.sonName") public class parentNameConfig { } .yml配置如下: parentName: sonName: true //正常 parentName: sonName: //正常,空字符时 parentName: sonName: false //失败 parentName: sonName: null //正常 parentName: sonName: 123 //正常 把第二行换成: @ConditionalOnProperty(value = "parentName.sonName",havingValue = "123") .yml配置如下: parentName: sonName: 123 //正常 parentName: sonName: 1234 //失败 parentName: sonName: false //失败 把第二行换成: @ConditionalOnProperty(value = "parentName.sonName",havingValue = "false") .yml配置如下: parentName: sonName: false //正常 把第二行换成: @ConditionalOnProperty(prefix = "parentName",name = "sonName",havingValue = "123") .yml配置如下: parentName: sonName: 123 //正常 parentName: sonName: 1234 //失败,与havingValue给定的值不一致 把第二行换成: @ConditionalOnProperty(prefix = "parentName",name = "sonName",havingValue = "123",matchIfMissing = true) .yml配置如下: 不配置相关参数 //正常,当matchIfMissing = true时,即使没有该parentName.sonName属性也会加载正常 把第二行换成: @ConditionalOnProperty(prefix = "parentName",name = "sonName",havingValue = "123",matchIfMissing = false) //这里matchIfMissing默认为false,写不写都行 .yml配置如下: 不配置相关参数 //失败,当matchIfMissing = false时,必须要有对应的property parentName: sonName: 123 //正常 把第二行换成: @ConditionalOnProperty(prefix = "parentName.", name = "sonName")//prefix带了.(点) .yml配置如下: parentName: sonName: true //正常 parentName: sonName: 123 //正常 把第二行换成: @ConditionalOnProperty(prefix = "parentName",value = {"sonName2"},name = {"sonName"}) .yml配置如下: parentName: sonName: true //项目Debug启动失败,The name and value attributes of @ConditionalOnProperty are exclusive //@ConditionalOnProperty的name和value属性是互斥的,不能同时出现 把第二行换成: @ConditionalOnProperty(prefix = "parentName",name = {"sonName"}) .yml配置如下: parentName: sonName: true //正常 把第二行换成: @ConditionalOnProperty(prefix = "parentName",name = {"sonName","flag"}) //name中的属性需要两个都存在且都不为false才会加载正常 .yml配置如下: parentName: sonName: true flag: true //正常 parentName: sonName: true flag: 123 //正常 parentName: sonName: true flag: false //失败 parentName: sonName: false flag: false //失败 把第二行换成: @ConditionalOnProperty(prefix = "parentName", name = {"sonName", "flag"}, havingValue = "false") parentName: sonName: false flag: false //正常 把第二行换成: @ConditionalOnProperty(prefix = "parentName", name = {"sonName", "flag"}, havingValue = "123")//parentName.sonName和parentName.flag的值都要与havingValue的一致才行 parentName: sonName: 123 flag: 1234 //失败 parentName: sonName: 123 flag: 123 //正常 parentName: sonName: 123 //失败,缺少parentName.flag 把第二行换成: @ConditionalOnProperty(prefix = "parentName", name = {"sonName", "flag"}, havingValue = "123",matchIfMissing = true)//matchIfMissing = true允许缺少 parentName: sonName: 123 //正常 .yml配置如下: 不配置相关参数 //正常 把第二行换成: @ConditionalOnProperty(prefix = "parentName", name = {"sonName", "flag"}, havingValue = "123") .yml配置如下: 不配置相关参数 //失败
@Value("${aligenie.sologan:This is default value}")
多行字符串属性
如果有一个非常长的属性值,可以考虑将它分成几行以提高可读性。可以在application.properties文件中使用反斜杠字符换行。
aligenie.content=Bright moonlight in front of bed
ground frost
up at the bright moon
down at home
注入多值属性 arrays, list, set
应用中的某些属性可能会使用多个值。在这种情况下,可以指定由逗号分隔的值列表进行配置。
aligenie.numbers=1,2,3,4,5,6,1,2
TestController(@Value("${aligenie.numbers}") int[] numbers) { }
aligenie.lists=aa;bb;cc;dd
public TestController(
@Value("#{'${aligenie.lists}'.split(';')}") List<String> list) {
this.list = list;
log.debug("list", list);
}