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

weixin_38085456 2019-05-23 09:08:42
spring mvc 项目使用 @PropertySource 读取项目src/main/resource 路径下的配置文件 @PropertySource({"classpath*:aa.properties"}) 但是启动报错 file not found path /classpath:aa.properties 调试 发现路径 是 src/main/webapp/classpath:aa.properties 求大神解释,,为什么不是从 classpath 下面读取 ??
...全文
1450 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38112495 2019-06-03
  • 打赏
  • 举报
回复
网上找了很多,都是说加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(); } }

435

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧