Spring import配置文件使用占位符
2024-08-26 08:40:47
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 AppContextInitializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
private Logger logger = Logger.getLogger(AppContextInitializer.class);
@Override
public 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>
启动配置文件加载正常。
推荐阅读
分享Java干货,高并发编程,热门技术教程,微服务及分布式技术,架构设计,区块链技术,人工智能,大数据,Java面试题,以及前沿热门资讯等。
最新文章
- Android studio下载依赖包很慢
- [ML] Naive Bayes for Text Classification
- ConstraintLayout
- 洛谷 P2590 [ZJOI2008]树的统计(树链剖分)
- [爬虫]scrapy框架
- oozie调度sqoop Job 数据库密码无法保存
- 牛客练习赛42 C 出题的诀窍
- f5 SSL及证书
- MySQL锁(行锁、表锁、页锁、乐观锁、悲观锁等)
- Hibernate Validator注解大全
- java判断集合是否相等
- 获取微信小程序源码
- tf.placeholder使用说明
- 华为荣耀V8这个7.0的系统的root
- mysql 源代码编绎
- 【协议篇】UDP
- 中文路径-接口路径url不能传输中文解决方案
- centos上yum安装nodeJS
- iOS之禁止所有输入法的表情
- uoj 36 玛里苟斯
热门文章
- 存取cookies
- gitlab fatal: Authentication failed for &#39;http://10.2.80.17:8090/yeyichao/201904041026PROj.git/&#39;
- qq传文件测试用例设计
- 解决:python安装mysqldb模块报 EnvironmentError: mysql_config not found
- [FW]修复ubutnu12.04+win7的grub2引导
- C#log4net的使用
- nginx之域名重定向
- Python之字符串转换为日期、结合时区的日期操作
- Cas 使用maven的overlay搭建开发环境 (二)
- 选择恐惧症的福音!教你认清MVC,MVP和MVVM(转:示例挺好,不太赞同画图)