@PropertySource中的classpath通配符 [英] classpath wildcard in @PropertySource

查看:3160
本文介绍了@PropertySource中的classpath通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Java配置来创建我的bean。
但是这个bean对于2个应用程序是通用的。
两者都有一个属性文件abc.properties但具有不同的类路径位置。
当我放置显式类路径时

I am using Spring Java config to create my bean. But this bean is common to 2 applications. Both have one property file abc.properties but with different classpath locations. When i put explicit classpath like

@PropertySource("classpath:/app1/abc.properties")

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

then it works but when i try to use wildcard like

@PropertySource("classpath:/**/abc.properties")

然后它不起作用。
我尝试了许多通配符的组合,但它仍然无效。
通配符是否在 @ProeprtySource
中有没有其他方法可以读取标有 @Configurations的类别中的属性

then it doesn't work. I try many combinations of wildcard but it still not working. Is wildcard works in @ProeprtySource Is there any other way to read to property in classed marked with @Configurations.

推荐答案

@PropertySource API:资源位置通配符(例如** / * .properties)是不允许的;每个位置必须评估一个.properties资源。

@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;
    }

这篇关于@PropertySource中的classpath通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆