當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring import配置文件使用占位符
生活随笔
收集整理的這篇文章主要介紹了
Spring import配置文件使用占位符
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
轉(zhuǎn)載自?Spring import配置文件使用占位符
import使用占位符
連接池切換導(dǎo)入配置的代碼:
<import resource="classpath:META-INF/spring/spring-${db.connection.pool}.xml" />在配置文件添加配置
db.connection.pool=druid啟動直接報錯,讀取不到配置,因為屬性文件的加載在import配置文件之后。
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'db.connection.pool' in value "classpath:META-INF/spring/spring-${db.connection.pool}.xml"所以,要在應(yīng)用啟動的時候添加屬性
1、添加AppContextInitializer啟動類:
public class AppContextInitializerimplements ApplicationContextInitializer<ConfigurableApplicationContext> { ? ?private Logger logger = Logger.getLogger(AppContextInitializer.class); ? ?@Overridepublic void initialize(ConfigurableApplicationContext applicationContext) {ResourcePropertySource propertySource = null;try {propertySource = new ResourcePropertySource("classpath:config/db-config.properties");} catch (IOException e) {logger.error("加載配置文件[config/db-config.properties]失敗");}applicationContext.getEnvironment().getPropertySources().addFirst(propertySource);} }2、在web.xml中添加配置:
context-param> ?<param-name>contextInitializerClasses</param-name> ?<param-value>com.example.AppContextInitializer</param-value> ? </context-param>啟動配置文件加載正常。
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎
總結(jié)
以上是生活随笔為你收集整理的Spring import配置文件使用占位符的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring AOP注解为什么失效?90
- 下一篇: 获取Spring的Application