spring配置文件import标签中使用${}占位符获得配置文件的属性值
2019独角兽企业重金招聘Python工程师标准>>>
一般情况下我们在Spring的配置文件中使用<import>标签是这样的,<import resource="spring-other.xml">,但是最近项目中使用到其他工程的依赖jar包,在自己的spring配置文件中需要这样写
<context:property-placeholder location="classpath:eoa/eoa_config.properties" />
<import resource="classpath:spring-fs-db-${env}.xml" />
其中env的值是从eoa_config.properties里面获取。
如果是以上这种写法,在启动时spring报错,无法解析env,原因很简单,在import动作时在属性文件加载之前。
没办法只能翻spring源码,ContextLoader
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) {
if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
// The application context id is still set to its original default value
// -> assign a more useful id based on available information
String idParam = sc.getInitParameter(CONTEXT_ID_PARAM);
if (idParam != null) {
wac.setId(idParam);
}
else {
// Generate default id...
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
ObjectUtils.getDisplayString(sc.getContextPath()));
}
}
wac.setServletContext(sc);
String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM);
if (configLocationParam != null) {
wac.setConfigLocation(configLocationParam);
}
// The wac environment's #initPropertySources will be called in any case when the context
// is refreshed; do it eagerly here to ensure servlet property sources are in place for
// use in any post-processing or initialization that occurs below prior to #refresh
ConfigurableEnvironment env = wac.getEnvironment();
if (env instanceof ConfigurableWebEnvironment) {
((ConfigurableWebEnvironment) env).initPropertySources(sc, null);
}
customizeContext(sc, wac);
wac.refresh();
}
initPropertySources这个方法可以自己定义属性文件的加载时机。
@Override
public void initPropertySources(ServletContext servletContext, ServletConfig servletConfig) {
WebApplicationContextUtils.initServletPropertySources(getPropertySources(), servletContext, servletConfig);
}
也就是说我们需要在getPropertySources()方法调用之前,就已经做好属性文件的加载顺序。
spring提供了ApplicationContextInitializer这个接口,实现该接口
public class CustomerApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext>{
private static Logger logger = LoggerFactory.getLogger(CustomerApplicationContextInitializer.class);
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
ResourcePropertySource propertySource = null;
try {
propertySource = new ResourcePropertySource("classpath:eoa/eoa_config.properties");
} catch (IOException e) {
logger.error("eoa_config.properties is not exists");
}
applicationContext.getEnvironment().getPropertySources().addFirst(propertySource);
}
}
此外,还需要在web.xml中配置这个类
<context-param>
<param-name>contextInitializerClasses</param-name>
<param-value>com.lfex.eoa.base.CustomerApplicationContextInitializer</param-value>
</context-param>
因为在customizeContext(sc, wac);会调用所有的contextInitializerClasses中的initialize方法。
至此,问题解决了,总结一下分为以下几步
提供实现ApplicationContextInitializer接口的实现类
在web.xml中配置,见上
在spring的主配置文件中使用<import resource="spring-${env}.xml">
转载于:https://my.oschina.net/u/2375016/blog/506874
spring配置文件import标签中使用${}占位符获得配置文件的属性值相关推荐
- c语言中占位符,Java C# C语言中的占位符
一般拼接一段字符串在编程中是很常见的事,下面简单做个总结: 什么是占位符?占位符就是先占住一个固定的位置,等着你再往里面添加内容的符号. 1.Java中处理方法: package com.amos; ...
- UITextView中的占位符
我的应用程序使用UITextView . 现在,我希望UITextView具有类似于可以为UITextField设置的占位符. 这个怎么做? #1楼 简单的方法,只需使用以下UITextViewDel ...
- php 什么是占位符,php中的占位符
Java C# C语言中的占位符 一般拼接一段字符串在编程中是很常见的事,下面简单做个总结: 什么是占位符?占位符就是先占住一个固定的位置,等着你再往里面添加内容的符号. 1.Java中处理方法: p ...
- Mybatis中的#{}占位符
Mybatis中的#{}占位符 在Mybatis中配置SQL时,可以使用#{}格式的占位符来表示SQL语句中的参数,在占位符的大括号中,当抽象方法只有1个基本值(基本数据类型对应的值,和String) ...
- 安卓开发中的占位符在布局XML中使用
安卓开发中的占位符在布局XML中使用 刚开始学Android,实现用户注册登录功能EditText使用占位符. == 普通的英文半角空格 == == == no-break space ( ...
- %s——字符串中的占位符
%s在字符串中表示占位符 public static void main(String[] args) {String s = "姓名:%s,性别:%s";String name ...
- 在python中的占位符中、请你选出不属于占位符的选项_知到智慧树My College English Diary作业题库答案...
[单选题]如果有3个进程共享同一程序段,而且每次最多允许两个进程进入该程序段,则信号量的初值应设置为( ) A. 3 B. 2 C. 1 D. 0 [单选题]患者男性,32岁,突发寒战.高热,咳嗽.右 ...
- 微信小程序js数组初始化_微信小程序开发之改变data中数组或对象的某一属性值...
前言:在小程序的开发中,我们在view中便利data中数组或对象时,很多情况下需要在js中动态改变数组或者对象中某一香的属性值. 效果图: 我给大家总结了案例如下: wxml如下: {{item.we ...
- spring 之 import标签、alias标签、beans标签 解析
继续接着Spring 加载.解析applicationContext.xml 流程解析 import . alias.beans 标签. DefaultBeanDefinitionDocumentRe ...
最新文章
- 真实工作经验总结——案例解析企业选型操作步骤
- 文档过期打不开怎么办_网络上下载文档,常见的3个问题,一招教你快速解决!...
- Spring Boot 简单集成 Liquibase
- altium 去掉部分铺铜_干货|HFSS器件导入Altium进行PCB制作教程!!!
- php生成excel教程,php生成EXCEL的东东
- 电脑技巧:Win10自带存储感知功能给电脑磁盘瘦身
- 第一百零七期:她说,嫁人就选程序员!
- 5专题总结-数据结构
- [UE4]更新UI的三种方式
- N皇后问题12 · N-Queens
- IMO FTPC Part 3-A、B和F级分隔耐火性能测试
- 程序员如何财务自由【原创】
- 小米10pro手机电路图 主板元件位号图
- 计算机时区找不到北京,emwin7时区/em 怎么没有北京时间了-win7时区,win7系统怎么添加北京时区...
- JS-Global对象
- 【经济学视频课程】科斯定理的本质…
- android 填充内存方法,安卓快速填满手机内存(转载)
- c语言乒乓球比赛相关的拓展程序,乒乓球编排软件.pdf
- 毫米波电路的PCB设计和加工(第一部分)
- 数字IC设计学习笔记_静态时序分析STA_伪路径False Paths