Use the ${} placeholder in spring profiles <import> tags to get the property values of the configuration file

Source: Internet
Author: User

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

    1. Provides implementation classes that implement the Applicationcontextinitializer interface

    2. Configuration in Web. XML, see the

    3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.