通常狀況下咱們在Spring的配置文件中使用<import>標籤是這樣的,<import resource="spring-other.xml">,可是最近項目中使用到其餘工程的依賴jar包,在本身的spring配置文件中須要這樣寫web
<context:property-placeholder location="classpath:eoa/eoa_config.properties" />spring
<import resource="classpath:spring-fs-db-${env}.xml" />app
其中env的值是從eoa_config.properties裏面獲取。
ide
若是是以上這種寫法,在啓動時spring報錯,沒法解析env,緣由很簡單,在import動做時在屬性文件加載以前。post
沒辦法只能翻spring源碼,ContextLoaderspa
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) {orm
if (ObjectUtils.identityToString(wac).equals(wac.getId())) {xml
// The application context id is still set to its original default value接口
// -> assign a more useful id based on available informationget
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">