天天看點

由Could not resolve placeholder 'xxx.xxx' in string value "${xxx.xxx}引發的項目中配置檔案那些事

近日在編寫基于SOA的商城項目,項目由父工程及若幹子子產品構成,在編寫過程中,為了互相協作及維護友善,于是将一些連接配接位址資訊抽取出來單獨放在配置檔案中,并且放到了common公用子產品内

由Could not resolve placeholder 'xxx.xxx' in string value "${xxx.xxx}引發的項目中配置檔案那些事

在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. 解決沖突:

添加了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後面的星号很重要。