I. Description of the problem
Project Framework uses SSM, integrated transaction rollback (see below), in unit testing, test transactions are valid, but when the actual project on-line, but there is no effect.
Second, the Integration method
Application-mybatis.xml (The following XML masks some irrelevant details)
<!--Data Connection Pool - <BeanID= "DataSource"class= "Com.alibaba.druid.pool.DruidDataSource"> < Propertyname= "Driverclassname"value= "${jdbc.driver.dev}"></ Property> < Propertyname= "url"value= "${jdbc.url.dev}"></ Property> < Propertyname= "username"value= "${jdbc.user}"></ Property> < Propertyname= "Password"value= "${jdbc.password}"></ Property> </Bean> <!--transaction manager - <BeanID= "TransactionManager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager"> < Propertyname= "DataSource"ref= "DataSource"></ Property> </Bean> <!--Transaction Configuration 1: Manual annotation Required - <!--Proxy-traget-class True to proxy the class and, if False, to proxy the interface, you need to add @Transactional annotations to the class or method when used. - <Tx:annotation-drivenTransaction-manager= "TransactionManager"Proxy-target-class= "true" />
Application-common.xml (the key is to have Spring management exclude controller parts)
<!--All packages under Com.mc.bsframe are automatically scanned, including the classes under the sub-package except @controller. - <Scpan:component-scanBase-package= "Com.mc.bsframe"> <Scpan:exclude-filtertype= "Annotation"expression= "Org.springframework.stereotype.Controller" /> <Scpan:exclude-filtertype= "Annotation"expression= "Org.springframework.web.bind.annotation.ControllerAdvice" /> </Scpan:component-scan>
Spring-mvc.xml (the key is to handle only the controller part)
<!--scans only classes that are annotated with a controller under Base-package. - <Context:component-scanBase-package= "Com.mc.bsframe.controller"> <Context:include-filtertype= "Annotation"expression= "Org.springframework.stereotype.Controller" /> <!--You must include Controlleradvice to handle global exceptions. - <Context:include-filtertype= "Annotation"expression= "Org.springframework.web.bind.annotation.ControllerAdvice" /> </Context:component-scan>
Basic about the configuration of the transaction as above, but I found that occasionally there will be a failure of the situation,
Third, analysis
Why JUNIT4 test is effective, guess because JUNIT4 is created under a context object, and the actual project is a spring context, a SPRINGMVC?
Iv. Methods of Solution
To add a configuration that excludes scan service in Spring-mvc.xml, the previous statement simply contains the controller and Controlleradvice, as follows:
<!--scans only classes that are annotated with a controller under Base-package. - <Context:component-scanBase-package= "Com.mc.bsframe.controller"> <Context:include-filtertype= "Annotation"expression= "Org.springframework.stereotype.Controller" /> <!--You must include Controlleradvice to handle global exceptions. - <Context:include-filtertype= "Annotation"expression= "Org.springframework.web.bind.annotation.ControllerAdvice" /> <!-- !!! It is best to add this to allow SPRINGMVC to rule out the service layer and avoid the problem of transaction failure. - <Context:exclude-filtertype= "Annotation"expression= "Org.springframework.stereotype.Service" /> </Context:component-scan>
MyBatis and SPRINGMVC integration transactions are valid under JUnit tests but are not valid in the actual project