Spring integrated mybatis using <context:property-placeholder> pits

Source: Internet
Author: User


A data migration program needs to be developed for the recent project to go live. The main function of the program is to query the data in a database, and then import another database after a series of processing. Take into account the ease and speed of development. Naturally think of integrating with spring and mybatis. Even with MyBatis automatic code generation, you can save a lot of DAO layer development.

consolidation of the pits

Previous projects: Similar programs have been used in the past, and the integration of spring and MyBatis is directly modified. The previous consolidation configuration is this:

1, consider the database URL, user name password configurable, put this information into the properties file. Used in the spring configuration file.


<location= "Classpath:config.properties"/>

    2, the MyBatis and spring in the spring configuration file, the integrated configuration is this

<BeanID= "Sqlsessionfactory"class= "Org.mybatis.spring.SqlSessionFactoryBean">        < Propertyname= "DataSource"ref= "DataSource" />    </Bean>    <Beanclass= "Org.mybatis.spring.mapper.MapperScannerConfigurer">        < Propertyname= "Basepackage"value= "Com.lagou.chat.record.transfer.dao" />    </Bean> 

The above configuration is not a problem. So just copy the configuration directly to the new project

Current Project : Copy the configuration of the old project, but the new project to connect two databases, naturally need two data sources (record and IM), the old configuration made the following changes

1, the use of the properties file configuration is not changed

2, before because of a data source (a sqlsessionfactory), so there is no configuration under Mapperscannerconfigurer <property name= "sqlsessionfactory" ref= " Sqlsessionfactory "/>. Because Sqlsessionfactory is used by default. But now two data sources, not specifying definitely lead to confusion. So the configuration is modified to the following

    <BeanID= "Record_sqlsessionfactory"class= "Org.mybatis.spring.SqlSessionFactoryBean">        < Propertyname= "DataSource"ref= "Record_datasource" />    </Bean>    <BeanID= "CONFIG1"class= "Org.mybatis.spring.mapper.MapperScannerConfigurer">        < Propertyname= "Basepackage"value= "Com.xxx.util.rollback.record.dao" />        < Propertyname= "Sqlsessionfactory"ref= "Record_sqlsessionfactory"/>    </Bean>     <BeanID= "Im_sqlsessionfactory"class= "Org.mybatis.spring.SqlSessionFactoryBean">        < Propertyname= "DataSource"ref= "Im_datasource" />    </Bean>    <BeanID= "Config2"class= "Org.mybatis.spring.mapper.MapperScannerConfigurer">        < Propertyname= "Basepackage"value= "Com.xxx.util.rollback.im.dao" />        < Propertyname= "Sqlsessionfactory"ref= "Im_sqlsessionfactory"/>    </Bean> 

As a result, properties such as ${jdbc.url},${jdbc.name} in the spring configuration file cannot be replaced by the specified value in the properties when the new project is run. In the beginning, it was not surprising that spring and MyBatis were integrated, so I kept checking the spring configuration file for errors, whether the properties file was wrong, Whether the properties file is not referenced or the properties file is not compiled into the Classpath directory. Of course, analysis does not analyze the cause of the problem, and naturally there is no way to find a solution. Had to resort to the Internet. Finally, I found the answer.

  fix : The configuration needs to be changed to the following, the problem is resolved:

<BeanID= "Record_sqlsessionfactory"class= "Org.mybatis.spring.SqlSessionFactoryBean">        < Propertyname= "DataSource"ref= "Record_datasource" />    </Bean>    <BeanID= "CONFIG1"class= "Org.mybatis.spring.mapper.MapperScannerConfigurer">        < Propertyname= "Basepackage"value= "Com.xxx.util.rollback.record.dao" />        < Propertyname= "Sqlsessionfactorybeanname"value= "Record_sqlsessionfactory"/>    </Bean>     <BeanID= "Im_sqlsessionfactory"class= "Org.mybatis.spring.SqlSessionFactoryBean">        < Propertyname= "DataSource"ref= "Im_datasource" />    </Bean>    <BeanID= "Config2"class= "Org.mybatis.spring.mapper.MapperScannerConfigurer">        < Propertyname= "Basepackage"value= "Com.xxx.util.rollback.im.dao" />        < Propertyname= "Sqlsessionfactorybeanname"value= "Im_sqlsessionfactory"/>    </Bean> 

is to change the Sqlsessionfactory property to Sqlsessionfactorybeanname. Of course, you have to change ref to value. Because the Sqlsessionfactorybeanname property is a string type


When using Org.mybatis.spring.mapper.MapperScannerConfigurer for automatic scanning in spring, the sqlsessionfactory is set. May cause Propertyplaceholderconfigurer to fail, that is, an expression such as ${jdbc.username} will not be able to get to the content in the properties file.

This is caused by the fact that Mapperscannerconigurer is actually parsing the load 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, causing the expression to be copied as a string. However, if you do not set the Sqlsessionfactory property, you must ensure that the name Sessionfactory in spring will be sqlsessionfactory, otherwise it will not be automatically injected.


Spring integrated mybatis using <context:property-placeholder> pits

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.