@PropertySource中的classpath通配符
我正在使用Spring Java配置来创建我的bean。 但是这个bean在2个应用程序中很常见。 两者都有一个属性文件abc.properties但具有不同的类路径位置。 当我把类似的显式类路径
@PropertySource("classpath:/app1/abc.properties")
然后它工作,但当我尝试使用通配符时
@PropertySource("classpath:/**/abc.properties")
然后它不起作用。 我尝试了许多通配符的组合,但它仍然无法正常工作。 通配符是否在@ProeprtySource
是否有任何其他方法可以读取标记为@Configurations
@ProeprtySource
中的属性。
@PropertySource API: Resource location wildcards (eg **/*.properties) are not permitted; each location must evaluate to exactly one .properties resource.
Resource location wildcards (eg **/*.properties) are not permitted; each location must evaluate to exactly one .properties resource.
解决方法:试试
@Configuration public class Test { @Bean public PropertyPlaceholderConfigurer getPropertyPlaceholderConfigurer() throws IOException { PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); ppc.setLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/**/abc.properties")); return ppc; }
附加到dmay变通方法:
由于Spring 3.1 PropertySourcesPlaceholderConfigurer应该优先于PropertyPlaceholderConfigurer使用,并且bean应该是静态的。
@Configuration public class PropertiesConfig { @Bean public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() { PropertySourcesPlaceholderConfigurer propertyConfigurer = new PropertySourcesPlaceholderConfigurer(); propertyConfigurer.setLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/**/abc.properties")); return propertyConfigurer; } }