gpt4 book ai didi

java - @PropertySource 中的类路径通配符

转载 作者:IT老高 更新时间:2023-10-28 13:48:09 28 4
gpt4 key购买 nike

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

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

然后它可以工作,但是当我尝试使用像

这样的通配符时
@PropertySource("classpath:/**/abc.properties")

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

最佳答案

@PropertySource API:不允许使用资源位置通配符(例如 **/*.properties);每个位置都必须精确评估为一个 .properties 资源。

解决方法:尝试

@Configuration
public class Test {

@Bean
public PropertyPlaceholderConfigurer getPropertyPlaceholderConfigurer()
throws IOException {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/**/abc.properties"));
return ppc;
}

关于java - @PropertySource 中的类路径通配符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14389995/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com