Their definition Propertyplaceholder Unable to complete the replacement task

Source: Internet
Author: User

Spring default propertyplaceholderconfigurer can only load the Properties grid style brief, and now we need to be able to allow a Hadoop-like XML configuration file to read configuration information from the full support of the class, and replaced the relevant bean placeholder, extended it, in detail such as the following:

public class Custompropertyplaceholderconfigurer extends Propertyplaceholderconfigurer {private resource[] locations; public void LoadProperties (Properties props) throws IOException {if (this.locations! = null) {Prop            Ertiespersister propertiespersister = new Defaultpropertiespersister ();                for (int i = 0; i < this.locations.length; i++) {Resource location = This.locations[i];                if (logger.isinfoenabled ()) {Logger.info ("Loading properties file from" + location);                } InputStream is = null;                    try {is = Location.getinputstream ();                    Propertiespersister.load (props, is);                    Configuration conf = squirrelconfiguration.create ();  map<string, string> map = Conf.listallconfentry ();    Read configuration information from Squirrel-site.xml for (map.entry<string, string> entry:map.entrySet ()) {                    Props.put (Entry.getkey (), Entry.getvalue ());                }} finally {if (is = null) is.close ();        }}}} public void Setlocations (resource[] locations) {super.setlocations (locations);    This.locations = Locations; }}




But execution has been error-free, ${jdbc_driver_name} has not been replaced. Discovered through a query. The original is to use Org.mybatis.spring.mapper.MapperScannerConfigurer in spring to do their own active scan. Set the sqlsessionfactory words. May cause Propertyplaceholderconfigurer to fail, that is, expressions such as ${jdbc_driver_name} will not be able to get to the content in the properties file. The cause of this is due to. Mapperscannerconigurer is actually parsed into the bean definition phase, which, if set sqlsessionfactory, causes some classes to be initialized in advance. This time,
Propertyplaceholderconfigurer has not had time to replace the variables in the definition. Causes the expression to be copied as a string. But assuming that the Sqlsessionfactory attribute is not set, it is necessary to ensure that the name sessionfactory in spring must be sqlsessionfactory. Otherwise, you cannot inject yourself voluntarily. or directly define Mapperfactorybean. or abandon their own active Proxy interface mode.


Can be changed as follows:

<bean id= "Propertyconfigurer" class= "Com.yowu.common.CustomPropertyPlaceholderConfigurer" > <property name            = "Locations" > <list> <value>classpath:important.properties</value> </list> </property> </bean><!--Configuring the thread pool--><bean id= "Taskexecutor" class= "org.spring Framework.scheduling.concurrent.ThreadPoolTaskExecutor "><!--thread pool maintains a minimum number of threads--><property name=" Corepoolsize "value="/><!--thread pool maintenance thread agreed spare time--><property name= "keepaliveseconds" value= "0"/><! --The maximum number of threads maintained by the thread pool--><property name= "maxpoolsize" value= "/><!--the buffer queue--><property name= used by the thread pool" Queuecapacity "value=" 0 "/></bean><bean id=" DataSource "class=" Org.apache.commons.dbcp.BasicDataSource "><property name=" Driverclassname "value=" ${squirrel_jdbc_driver} "/><property name=" url "value=" ${ Squirrel_jdbc_url} "/><property name=" username "value=" ${squirrel_Jdbc_username} "/><property name=" password "value=" ${squirrel_jdbc_password} "/><property name=" Validationquery "value=" Select 1 "/><property name=" InitialSize "value=" 5 "/><property name=" Testwhileidle "value=" true "/><property name=" Maxidle "value=" "/><property name=" MinIdle "value=" 5 "/ ><property name= "maxactive" value= "/><property name=" Removeabandonedtimeout "value="/>< Property Name= "Maxwait" value= "30000"/></bean> <bean id= "ysqlsessionfactory" class= " Org.mybatis.spring.SqlSessionFactoryBean "> <property name=" dataSource "ref=" DataSource "/> <prope             Rty name= "Mapperlocations" > <list> <value>classpath*:mybatis/*.xml</value> </list> </property> </bean> <bean id= "sqlsession" class= "org.mybatis.spring. Sqlsessiontemplate "> <constructor-arg index=" 0 "ref=" ysqlsessionfactory "&GT;&LT;/constructor-arg> <constructor-arg index= "1" value= "BATCH" ></constructor-arg> </bean> <bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name= "basepackage" value= "com. Yowu.dao, Com.yowu.app.repository.dao "/> <!--core is to add the following sentence.

The property that follows is value. Not ref, remember--<property name= "Sqlsessionfactorybeanname" value= "Ysqlsessionfactory"/> </bean>



By looking at Mapperscannerconfigurer source code discovery, there is actually a piece of code:

public void Postprocessbeandefinitionregistry (Beandefinitionregistry beandefinitionregistry) throws BeansException {    if (this.processpropertyplaceholders) {processpropertyplaceholders ();    } Scanner Scanner = new Scanner (beandefinitionregistry);    Scanner.setresourceloader (This.applicationcontext); Scanner.scan (Stringutils.tokenizetostringarray (This.basepackage, configurableapplicationcontext.config_location  _delimiters)); }/* * beandefinitionregistries is called early in application startup, before * beanfactorypostprocessors. This means, propertyresourceconfigurers would not have the been * loaded and any property substitution the This class ' pro Perties would fail. To avoid this, find * any propertyresourceconfigurers defined in the context and run them on this class ' Bean * defini tion.   Then update the values. */private void processpropertyplaceholders () {



copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Their definition propertyplaceholder unable to complete the replacement task

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.