use the ${} placeholder in the Spring Configuration File tab to obtain the property value of the configuration file _spring

Source: Internet
Author: User

Typically, 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"/>

The value of the Env is obtained from the eoa_config.properties.

If this is the case, spring will fail at startup and the Env cannot be resolved because it is very simple, before the property file is loaded in the import action.

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 are still set to its original Default value//-> assign a more useful ID based on available information String idparam = Sc.getinitpara
            meter (Context_id_param);
            if (idparam!= null) {Wac.setid (idparam); } else {//Generate default ID ... wac.setid (configurablewebapplicationcontext.application_con
            Text_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 calledIn any case, the context/be refreshed; Do it eagerly the ensure Servlet property sources are in place for//use in any post-processing or initialization tha
        T occurs below prior to #refresh configurableenvironment env = wac.getenvironment (); if (env instanceof configurablewebenvironment) {(configurablewebenvironment) env). Initpropertysources (SC, nu
        ll);
        } 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) {WebApp
    Licationcontextutils.initservletpropertysources (Getpropertysources (), ServletContext, servletConfig); }

That is, we need to do the loading order of the property files before the Getpropertysources () method is called. I don't think so.

Calls are required on the Propertyplaceholderconfigurer overloaded post processor beanfactorypostprocessor (Modify bean configuration information) postprocessbeanfactory Load Properties Property file before

Spring provides the interface for Applicationcontextinitializer, which implements the 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 initialize methods in the contextinitializerclasses are called.

The properties file is loaded before propertyplaceholderconfigurer the inherited Beanfactorypostprocessor Postprocessbeanfactory method

So far, the problem has been solved, summed up a few steps into the following

Provides implementation classes to implement Applicationcontextinitializer interfaces

Configure in Web.xml, see above

Use <import resource= "Spring-${env}.xml" in spring's master 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.