javascript
Spring使用外部属性文件以及常见错误(内附大量图片,很有借鉴意义)
一、在 Spring Config 文件中配置 Bean 時(shí),有時(shí)候需要在 Bean 的配置里添加?系統(tǒng)部署的細(xì)節(jié)信息,如文件路徑,數(shù)據(jù)源配置信息。而這些部署細(xì)節(jié)實(shí)際上需要在配置文件外部來(lái)定義。
二、Spring 提供了一個(gè) PropertyPlaceholderConfigurer 的 BeanFactory 后置處理器。這個(gè)處理器允許用戶將 Bean 的配置部分內(nèi)容外移到屬性文件中,然后可以在 Bean 的配置文件
里使用形式為 ${var}的變量,PropertyPlaceholderConfigurer 從屬性文件里加載屬性,并使用這些屬性來(lái)替換變量。
三、Spring 還允許在屬性文件中使用 ${key},以屬性間的互相引用。
四、使用:需要注冊(cè)?PropertyPlaceholderConfigurer 。通過(guò)?<context:property-placeholder location="props.properties"/> 這種方式來(lái)指定屬性文件。
五、例子:
1:目錄結(jié)構(gòu)
2.:properties.xml
3.:Carmessage.properties
4.:測(cè)試
5:控制臺(tái)輸出
六、易錯(cuò)點(diǎn)
在Spring配置文件中出現(xiàn)通配符的匹配很全面, 但無(wú)法找到元素 'context:property-placeholder' 的聲明這個(gè)錯(cuò)誤:
其實(shí)主要是我們?cè)谝朊臻g時(shí)沒(méi)有正確引入它的DTD解析文件,當(dāng)然你必須在把Spring相應(yīng)的包導(dǎo)入正確的情況下。?
?解決方案就是如下添加以下文件:?
xmlns:context="http://www.springframework.org/schema/context"?
同時(shí)在xsi:schemaLocation這個(gè)字符串中添加context相關(guān)的解析文件?
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context
????????????? http://www.springframework.org/schema/context/spring-context-4.1.xsd"
?? ?xmlns:context="http://www.springframework.org/schema/context">
總結(jié)
以上是生活随笔為你收集整理的Spring使用外部属性文件以及常见错误(内附大量图片,很有借鉴意义)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Radio / Select 设置
- 下一篇: Spring Boot与Activiti