Spring integrates mybatis use <context:property-placeholder> pit _java

Source: Internet
Author: User

Background

A recent project to be online requires the development of a data migration program. The main function of the program is to be a database of data, query out after a series of processing to import another database. Take into account the development of convenient and fast. Naturally thought to integrate with spring and MyBatis. Even with MyBatis automated code generation, you can save a lot of DAO layer development.

Integrated Pits

Previous projects: Similar programs have been used before to modify the configuration of spring and MyBatis integration directly. The previous consolidation configuration was this:

1, taking into account the database URL, user name password can be configured, put this information into the properties file. Used in the spring configuration file.

<context:property-placeholder location= "Classpath:config.properties"/>

2. The consolidated configuration of MyBatis and spring in the spring configuration file is this

<bean id= "Sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" >
<property name= " DataSource "ref=" DataSource "/>
</bean>
<bean class=" Org.mybatis.spring.mapper.MapperScannerConfigurer ">
<property name=" Basepackage "value=" Com.lagou.chat.record.transfer.dao "/>

The above configuration is no problem. So you 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, natural need two data sources (record and IM), the old configuration made the following modifications

1, the use of properties file configuration unchanged

2, before because of a data source (a sqlsessionfactory), so there is no mapperscannerconfigurer under the configuration <property name= "Sqlsessionfactory" ref= " Sqlsessionfactory "/>. Because Sqlsessionfactory is used by default. But now two data sources are not specified to cause confusion. So the configuration is modified as follows

 <bean id=" Record_sqlsessionfactory "class=" Org.mybatis.spring.SqlSessionFactoryBean "> <property name=" dataSource "ref=" Record_datasource "/> </ bean> <bean id= "config1" class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name= " Basepackage "value=" Com.xxx.util.rollback.record.dao "/> <property name=" sqlsessionfactory "ref=" Record_ Sqlsessionfactory "/> </bean> <bean id=" im_sqlsessionfactory "class=" Org.mybatis.spring.SqlSessionFactoryBean "> <property name=" dataSource "ref=" Im_datasource "/> </bean > <bean id= "config2" class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name= " Basepackage "value=" Com.xxx.util.rollback.im.dao "/> <property name=" sqlsessionfactory "ref=" im_ Sqlsessionfactory "/> </bean> 

As a result, attributes such as ${jdbc.url},${jdbc.name} in the spring configuration file cannot be replaced by the specified values in properties when the new project is run. At first it was natural to think that spring and MyBatis were the reason for integration, so I was constantly checking to see if the spring configuration file was wrong and 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, naturally it is impossible to find solutions. Had to resort to the Internet. Finally, I found the answer.

Modified: The configuration needs to be changed as follows, the problem is resolved:

 <bean id=" Record_sqlsessionfactory "class=" Org.mybatis.spring.SqlSessionFactoryBean "> <property name=" dataSource "ref=" Record_datasource "/> </ bean> <bean id= "config1" class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name= " Basepackage "value=" Com.xxx.util.rollback.record.dao "/> <property name=" Sqlsessionfactorybeanname "value=" Record_sqlsessionfactory "/> </bean> <bean id=" im_sqlsessionfactory "class=" Org.mybatis.spring.SqlSessionFactoryBean "> <property name=" dataSource "ref=" Im_datasource "/> </bean > <bean id= "config2" class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name= " Basepackage "value=" Com.xxx.util.rollback.im.dao "/> <property name=" 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

Reason

In spring, when the org.mybatis.spring.mapper.MapperScannerConfigurer is used for automatic scanning, the sqlsessionfactory is set. may result in Propertyplaceholderconfigurer invalidation, that is, using expressions such as ${jdbc.username}, you will not be able to get the content in the properties file.

This is due to the fact that Mapperscannerconigurer is actually in the process of parsing the load bean definition phase, which, if set sqlsessionfactory, would lead to early initialization of some classes, Propertyplaceholderconfigurer has not had time to replace the variable in the definition, resulting in an expression being copied as a string. However, if you do not set the Sqlsessionfactory property, you must ensure that sessionfactory in spring name must be sqlsessionfactory, otherwise it will not be automatically injected.

The above is a small set to introduce spring integration MyBatis use <context:property-placeholder> Pit, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.