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>

启动配置文件加载正常。

推荐阅读

干货:免费领取2TB架构师四阶段视频教程

面经:史上最全Java多线程面试题及答案

工具:推荐一款在线创作流程图、思维导图软件

分享Java干货,高并发编程,热门技术教程,微服务及分布式技术,架构设计,区块链技术,人工智能,大数据,Java面试题,以及前沿热门资讯等。

最新文章

  1. Android studio下载依赖包很慢
  2. [ML] Naive Bayes for Text Classification
  3. ConstraintLayout
  4. 洛谷 P2590 [ZJOI2008]树的统计(树链剖分)
  5. [爬虫]scrapy框架
  6. oozie调度sqoop Job 数据库密码无法保存
  7. 牛客练习赛42 C 出题的诀窍
  8. f5 SSL及证书
  9. MySQL锁(行锁、表锁、页锁、乐观锁、悲观锁等)
  10. Hibernate Validator注解大全
  11. java判断集合是否相等
  12. 获取微信小程序源码
  13. tf.placeholder使用说明
  14. 华为荣耀V8这个7.0的系统的root
  15. mysql 源代码编绎
  16. 【协议篇】UDP
  17. 中文路径-接口路径url不能传输中文解决方案
  18. centos上yum安装nodeJS
  19. iOS之禁止所有输入法的表情
  20. uoj 36 玛里苟斯

热门文章

  1. 存取cookies
  2. gitlab fatal: Authentication failed for &#39;http://10.2.80.17:8090/yeyichao/201904041026PROj.git/&#39;
  3. qq传文件测试用例设计
  4. 解决:python安装mysqldb模块报 EnvironmentError: mysql_config not found
  5. [FW]修复ubutnu12.04+win7的grub2引导
  6. C#log4net的使用
  7. nginx之域名重定向
  8. Python之字符串转换为日期、结合时区的日期操作
  9. Cas 使用maven的overlay搭建开发环境 (二)
  10. 选择恐惧症的福音!教你认清MVC,MVP和MVVM(转:示例挺好,不太赞同画图)