Springboot多属性文件配置
Springboot 多属性文件配置
配置文件后缀有两种: .properties和.yml
要完成多属性配置需要自定义PropertySourcesPlaceholderConfigurer 这个Bean
properties配置方法
/**
* 这里必须是static函数
* 如果不是 application.propertise 将读取不到
* application.properties 是默认加载的,这里配置自己的properties就好
* @return
*/
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
// properties 加载方式
Resource[] resources = {
//new ClassPathResource("application.properties"),
//ClassPathResource针对的是resource目录下的文件
new ClassPathResource("test.properties")
};
configurer.setLocations(resources);
return configurer;
}
注意这个Bean 的函数必须是static的,否则会加载不到application.properties中的内容
这里不需要将application.properties也加进来,因为application.properties是默认加进来的,这里只要写其他的属性文件就好了
yml配置方法
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
// yml 加载方式
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
//这里可以传入多个自定义属性文件
yaml.setResources(new ClassPathResource("test.yml"));
configurer.setProperties(yaml.getObject());
configurer.setLocations(resources);
return configurer;
}
yml 和properties的配置的注意点是一样的,只是需要YamlPropertiesFactoryBean来加载
2. 如果引用到属性文件中的值
test.propertise
grady.username=jiang
grady.password=1234567
TestConfig.java
@Configuration
// 这里不需要了
//@PropertySource("classpath:test.properties")
public class TestConfig {
@Value("${grady.username}")
private String username;
@Value("${grady.password}")
private String password;
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
这里用@Configuration或@Component都可以获得到值,
注意:这里不需要使用@PropertySource了,直接用就可以了
3. 在Controller中使用(其他地方也可,这里是举例)
public class UserController {
@Autowired
private TestConfig testConfig;
@Autowired
private SystemConfig systemConfig;
@PostMapping("/hello")
public String Hello() {
String datasourcePassword = systemConfig.getDatasourcePassword();
return "Hello World" + testConfig.getUsername() + " " + testConfig.getPassword()
+ " datasourcePassword= " + datasourcePassword;
}
postMan中的结果
Hello Worldjiang 1234567 datasourcePassword= Root123#
4. 更简洁的写法
@Configuration
public class SystemConfig {
private static List<Resource> resourceList = new ArrayList<>();
static {
resourceList.add(new ClassPathResource("test.properties"));
resourceList.add(new ClassPathResource("jdbc.properties"));
}
@Value("${spring.datasource.password}")
private String datasourcePassword;
/**
* 这里必须是static函数
* 如果不是 application.propertise 将读取不到
* application.properties 是默认加载的,这里配置自己的properties就好
* @return
*/
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
// properties 加载方式
configurer.setLocations(resourceList.stream().toArray(Resource[]::new));
return configurer;
}
public String getDatasourcePassword() {
return datasourcePassword;
}
}
本文作者:明月照江江的技术博客
本文链接:https://www.cnblogs.com/gradyblog/p/16152625.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步