由Could not resolve placeholder 'xxx.xxx' in string value "${xxx.xxx}引发的项目中配置文件那些事

近日在编写基于SOA的商城项目,项目由父工程及若干子模块构成,在编写过程中,为了相互协作及维护方便,于是将一些连接地址信息抽取出来单独放在配置文件中,并且放到了common公用模块内
环境描述
在dubbo中两个xml文件需要各自引入自己的配置于是分别写了如下引入

<!-- application-dubbo.xml 中 -->  
<context:property-placeholder location="classpath:dubbo.properties" />  
  
<!--application-jedis.xml 中-->  
<context:property-placeholder location="classpath:jedis.properties" />  

于是启动dubbo服务:Main.main(args)。
控制台中报错:
Could not resolve placeholder ‘xxx.xxx’ in string value "${xxx.xxx}
经过调试,证明不是找不到文件,排除properites文件路径错误、拼写错误。
查阅网上资料有如下描述:
一定要记住,不管是在一个Spring文件还是在多个Spring文件被统一load的情况下,直接写:

<context:property-placeholder location="" />  
<context:property-placeholder location="" />   

是不允许的,这样的引入方式会造成冲突。

  1. 解决冲突:
<context:property-placeholder location="classpath:dubbo.properties" ignore-unresolvable="true" />

添加了ignore-unresolvable="true"属性,注意,所有引入的地方都要添加,即使一个添加另一个不添加也不行。

  1. 另外,对于web项目:如果项目中引入了其他jar的配置文件properties,那么可以在web.xml中配置:
<!-- 上下文参数 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<!-- classpath:表示从当前项目加载  classpath*表示从当前项目及依赖的jar中加载,当所依赖的工具类例如redis又想从中加载配置文件时,可以考虑此用法  -->
		<param-value>classpath*:applicationContext-*.xml</param-value>
	</context-param>

总结一下,项目中多个spring文件分别引入各自对应的properties或yml配置,应在引入出添加ignore-unresolvable属性值为true。
web项目需要引入依赖jar中的配置文件,则在web.xml中contextConfigLocation参数设置为classpath*:applicationContext-*.xml,classpath后面的星号很重要。

发布了27 篇原创文章 · 获赞 27 · 访问量 4280

猜你喜欢

转载自blog.csdn.net/qq_41788977/article/details/102599237
今日推荐