原因:Controller 由 DispatcherServlet 建立,屬於子容器,只能讀取 servlet-context.xml(或 *-servlet.xml)中定義的 PropertyPlaceholderConfigurer。
修復:在對應 Servlet 配置文件添加:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
<value>classpath:config.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
原因:Classpath 下存在同名 config.properties(如 JAR 包內部與外部配置目錄各一份),Spring 預設只載入第一個。
現象日誌:
Loading properties file from URL [jar:file:/.../xxxx-common-1.5.7.jar!/config.properties]
Loading properties file from URL [file:/home/admin/creative-task/conf/config.properties]
修復:使用 classpath*: 前綴強制合併所有同名文件:
<context:property-placeholder ignore-unresolvable="true"
location="classpath:/jdbc.properties, classpath*:/config.properties"/>
Loading properties file from URL,缺失則檢查路徑拼寫、打包結構。org.springframework.core.env.PropertySourcesPropertyResolver,觀察 Found key 'xxx' in [localProperties]。applicationContext.xml 用 context:property-placeholder 載入 classpath*: 多路徑。file:/conf/ > classpath*: > JAR 內部,避免版本衝突。