最近在出些關于SSM環境搭建的教程,在我們搭建SSM架構,一般都無法避免需要加載多個xxxx.properties配置檔案。
也許很多人不注意,就會出現類似以下的錯誤:
Could not resolve placeholder 'redis.maxIdle' in string value "${redis.maxIdle}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'redis.maxIdle' in string value "${redis.maxIdle}"
這種是什麼情況,就是因為加載的配置檔案項 讀取不了值;
為什麼讀取不了值,因為spring掃描加載的時候隻讀取了一份xxxx.properties配置檔案,是以在後面配置加載的xxxx.properties配置檔案上面的值基本都拿不到了。
這種情況,需要這麼設定:
情景: 現在我需要加載jdbc.properties配置檔案和 redis.properties配置檔案。
那麼在配置加載jdbc.properties的時候,加上ignoreUnresolvablePlaceholders ,設定為true,如下:
<!--資料庫配置 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:jdbc.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
然後在配置加載 redis.properties的時候,也一樣加上,如下:
<!--引入Redis配置檔案-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:redis.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
這樣,就解決了加載多個配置檔案的問題了。
PS:關于ignoreUnresolvablePlaceholders設定,其實如果你僅僅需要加載2份 properties配置檔案,那麼你第一份設定true,第二份設定false或者ture都可以。 直白點,你最後一份可以設定為false,但是我建議直接全部設定true。