首页 新闻 会员 周边

@PropertySource 无法读取到properties 配置文件

0
悬赏园豆:5 [已解决问题] 解决于 2021-03-12 16:20

spring mvc 项目使用 @PropertySource 读取项目src/main/resource 路径下的配置文件

@PropertySource({"classpath*:aa.properties"})

但是启动报错

file not found path /classpath:aa.properties
调试 发现路径 是 src/main/webapp/classpath
:aa.properties

求大神解释,,为什么不是从 classpath 下面读取 ??

远方的人的主页 远方的人 | 初学一级 | 园豆:4
提问于:2019-05-23 21:08

不要沉

远方的人 4年前
< >
分享
最佳答案
0

网上找了很多,都是说加classpath* , 然而加了依然报错:
Could not open ServletContext resource [/classpath*:xx.properties]
最后换成classth: 仍然报错:
Could not resolve placeholder 'x1' in string value "${x1}"
最后把@value("${x1}") 这个原因是项目中有多个PropertyPlaceholderConfigurer相关的配置。如xml 中配置了
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
在加上@PropertySource ,就是两个地方配置了。需要给propertyConfigurer 这个bean 加上<property name="ignoreUnresolvablePlaceholders" value="true" />
正确写法:
@Configuration
@PropertySource("classpath:xxx.properties") //此处不是 classpath*:
public class ParamConfig {

@Value("x1")  //此处不要用  ${}
private String x1;

public String getX1(){
    return x1;
}

//使用${} 必须加这个
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}

远方的人 | 初学一级 |园豆:4 | 2019-06-03 15:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册