@PropertySource中的classpath通配符

dma*_*may 10 java spring config

我正在使用Spring Java配置来创建我的bean.但是这个bean在2个应用程序中很常见.两者都有一个属性文件abc.properties但具有不同的类路径位置.当我把类似的显式类路径

@PropertySource("classpath:/app1/abc.properties")
Run Code Online (Sandbox Code Playgroud)

然后它工作,但当我尝试使用通配符时

@PropertySource("classpath:/**/abc.properties")
Run Code Online (Sandbox Code Playgroud)

那它不起作用.我尝试了许多通配符的组合,但它仍然无效.通配符是否有效?@ProeprtySource 是否有任何其他方式可以读取标记为的类别的属性@Configurations.

Evg*_*eev 17

@PropertySource API: Resource location wildcards (e.g. **/*.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;
    }
Run Code Online (Sandbox Code Playgroud)


Mic*_*d a 7

附加到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;
  }

}
Run Code Online (Sandbox Code Playgroud)