In general we use the <import> tag in spring's configuration file, <import resource= "Spring-other.xml", but the most recent project uses a dependency jar package for other projects, You need to write this in your own spring configuration file
<context:property-placeholder location= "Classpath:eoa/eoa_config.properties"/>
<import resource= "Classpath:spring-fs-db-${env}.xml"/>
Where the value of env is obtained from the eoa_config.properties.
If this is the case above, spring error when starting, unable to parse Env, the reason is very simple, when the import action in the property file before loading.
No way can only turn spring source, 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'll be called in any case when the context
is refreshed; Do it eagerly the ensure Servlet property sources is
Use of any post-processing or initialization, occurs below prior to #refresh
Configurableenvironment env = wac.getenvironment ();
if (env instanceof configurablewebenvironment) {
((configurablewebenvironment) env). Initpropertysources (SC, NULL);
}
Customizecontext (SC, WAC);
Wac.refresh ();
}
initpropertysources This method can define the loading time of the property file itself.
@Override
public void Initpropertysources (ServletContext servletcontext, ServletConfig servletconfig) {
Webapplicationcontextutils.initservletpropertysources (Getpropertysources (), ServletContext, servletConfig);
}
That means we need to load the properties file before the Getpropertysources () method call.
Spring provides the Applicationcontextinitializer interface to implement this interface
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);
}
}
In addition, you need to configure this class in Web. xml
<context-param>
<param-name>contextInitializerClasses</param-name>
<param-value>com.lfex.eoa.base.CustomerApplicationContextInitializer</param-value>
</context-param>
because in Customizecontext (SC, WAC); all the initialize methods in the contextinitializerclasses are called .
At this point, the problem solved, summed up the following steps are divided into
Provides implementation classes that implement the Applicationcontextinitializer interface
Configuration in Web. XML, see the
Use <import resource= "Spring-${env}.xml" in spring's main configuration file >
Use the ${} placeholder in spring profiles <import> tags to get the property values of the configuration file