當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring import配置文件使用占位符
生活随笔
收集整理的這篇文章主要介紹了
Spring import配置文件使用占位符
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉載自?Spring import配置文件使用占位符
import使用占位符
連接池切換導入配置的代碼:
<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"所以,要在應用啟動的時候添加屬性
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>啟動配置文件加載正常。
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的Spring import配置文件使用占位符的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring AOP注解为什么失效?90
- 下一篇: 绝地求生要用什么配置才能稳定60帧?