Spring import配置文件使用占位符
来源:互联网 发布:通达信软件编程 编辑:程序博客网 时间:2024/05/21 22:17
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>
启动配置文件加载正常。
- Spring import配置文件使用占位符
- spring配置文件<import>标签中使用${}占位符获得配置文件的属性值
- spring配置文件<import>标签中使用${}占位符获得配置文件的属性值
- Spring配置文件使用占位符配置
- 使用import简化Spring配置文件
- spring 配置文件中的占位符 使用 context:property-placeholder
- spring 配置文件中的占位符 使用 context:property-placeholder
- spring 配置文件中的占位符 使用 context:property-placeholder
- spring 配置文件中的占位符 使用 context:property-placeholder
- 使用import简化spring的配置文件
- 使用import简化spring的配置文件
- 使用import简化spring的配置文件
- 使用import简化spring的配置文件
- 使用import简化spring的配置文件
- 使用import简化spring的配置文件
- 关于 Spring 中使用 context:property-placeholder 属性占位符设置配置文件
- spring+mybatis框架配置文件中使用${}占位符报错
- import 简化Spring 配置文件
- 【c++】友元关系和子类函数构造函数的写法
- Hdu 6148 Valley Numer 数位DP
- 10个Eclipse珍藏插件推荐
- 树莓派3 搭建bt下载机---利用 transmission-daemon
- php中获取主机名、协议及IP地址的方法 及mysql 1024错误解决
- Spring import配置文件使用占位符
- JavaScript常用字符串处理函数【属性+方法】
- Orange'S 第一章实验
- maven eclipse 配置 问题总结
- 多线程 等待/通知机制的实现
- Druid-目前最好的连接池
- 学习 Shell —— 认识 shell
- Core ML学习
- C