使用通配符使用@PropertySource (SpringBoot) 读取多个属性文件 [英] Reading multiple properties files with @PropertySource (SpringBoot)using wildcard character
问题描述
我想从特定位置读取多个属性文件,例如 C:\config.我正在使用 @PropertySource 注释.有没有办法在 Springboot 中使用一些通配符读取这些文件,例如 (*.properties).所以我打算实现的是这样的
I want to read multiples properties files from a specific location, say C:\config. I'm taking help of @PropertySource annotation. Is there a way to read these files in Springboot using some wildcard character e.g (*.properties). So what I intended to achieve is something like this
@PropertySource("*.properties") })
public class SomeClass{
}
如果没有,有没有办法以编程方式创建这些 @PropertySource("foo.properties") 或 @PropertySource("bar.properties") 并将它们提供给 @PropertySources 以便我可以实现这一目标.
If not, Is there a way to create these @PropertySource("foo.properties") or @PropertySource("bar.properties") programmatically and provide them to @PropertySources so that I can achieve this.
@PropertySources({
@PropertySource("foo.properties"),
@PropertySource("bar.properties")
})
我想实现它的原因是将来如果我必须注入另一个属性,比如 future.properties,我不必修改 Java 文件.
The reason I want to achieve it so in future if I have to inject another property say future.properties, I do not have to modify the Java files.
推荐答案
注解为将 PropertySource 添加到 Spring 的环境提供了一种方便的声明机制.与@Configuration 类结合使用.
Annotation providing a convenient and declarative mechanism for adding a PropertySource to Spring's Environment. To be used in conjunction with @Configuration classes.
支持传统和基于 XML 的属性文件格式 — 例如,classpath:/com/myco/app.properties"或file:/path/to/file.xml".
Both traditional and XML-based properties file formats are supported — for example, "classpath:/com/myco/app.properties" or "file:/path/to/file.xml".
不允许使用资源位置通配符(例如 **/*.properties);每个位置必须评估为恰好一个 .properties 资源.
Resource location wildcards (e.g. **/*.properties) are not permitted; each location must evaluate to exactly one .properties resource.
${...} 占位符将针对已在环境中注册的任何/所有属性源进行解析.
${...} placeholders will be resolved against any/all property sources already registered with the Environment.
@Configuration
@PropertySources({
@PropertySource("classpath:test.properties"),
@PropertySource("classpath:test1.properties")
})
public class TestConfig {
//...
}
您可以使用通配符.但您需要考虑弃用 PropertyPlaceholderConfigurer.
You can using to wildcard character. but you need to consider PropertyPlaceholderConfigurer Deprecated.
已弃用.自 5.2 起;使用 org.springframework.context.support.PropertySourcesPlaceholderConfigurer 代替,它通过利用 Environment 和 PropertySource 机制更加灵活.
Deprecated. as of 5.2; use org.springframework.context.support.PropertySourcesPlaceholderConfigurer instead which is more flexible through taking advantage of the Environment and PropertySource mechanisms.
@Configuration
public class PropertyConfig {
@Bean
public PropertyPlaceholderConfigurer getPropertyPlaceholderConfigurer()
throws IOException {
PropertyPlaceholderConfigurer propertyConfigurer = new PropertyPlaceholderConfigurer();
propertyConfigurer.setLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/test/*.properties"));
return propertyConfigurer;
}
这篇关于使用通配符使用@PropertySource (SpringBoot) 读取多个属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!