SSM failure of integration affairs

In the integration of SSM after the test Affairs, found that the addition to the transaction service layer does not take effect later reference Bowen, found to be scanning package when, springmvc configuration file is also scanned, and then spring the configuration file is also scanned, the following reference Bowen said package transaction after springmvc scan will fail validation is to have this impact.

Only the configuration package scan SpringMVC

SpringMVC test only scans the package, the package will now scan Spring commented, found that the default transaction failure.

(1) spring core profile applicationCotext.xml Pack scan commented

1     <!-- 开启包扫描 -->
2     <!--<context:component-scan base-package="com.boe.web"/>-->
3     <!--<context:component-scan base-package="com.boe.service"/>-->
4     <!--<context:component-scan base-package="com.boe.dao"/>-->

(2) springmvc scans were coated core profile

1      <context:component-scan base-package="com.boe.dao"></context:component-scan>
2      <context:component-scan base-package="com.boe.service"></context:component-scan>
3      <context:component-scan base-package="com.boe.web"></context:component-scan>

(3) service layer manufacturing method of a User manually add a runtime exception, but is abnormal after the addition of a user, other code omitted.

1     @Override
2     @Transactional(propagation = Propagation.REQUIRED)
3     public int registUser(User user) {
4         int addResult = userMapper.addUser(user);
5         int i=1/0;
6         return addResult;
7     }

(4) After a normal start tomcat server, browser access to the server to add users, execution error, but the data is normally inserted, the transaction is not in force.

Browser error prompt, restful style used here to submit parameters, parameters 'youngchaolin' and '88':

Normal database insert:

 

Although it may look to add a transaction propagation properties on the service layer method, but the transaction will not roll.

Pack scan arranged only in Spring

(1) Similar to the above, where the configuration packet for doing a full scan in applicationContext.xml file, comment out the springmvc, continue testing. Because springmvc profile does not scan package, so with access to the relevant notes @RequestMapping not entered into force, the browser can not add, where the use junit test. Get web layer controller, direct method calls regist its body to add users, add users need to call the service layer so you can see the impact.

1     @Test
2     public void test06(){
3         FirstController controller=context.getBean("userController",FirstController.class);
4         User user=new User();
5         user.setName("铁扇公主");
6         user.setAge(200);
7         String s = controller.regist(user);
8     }

(2) service layer unchanged, still perform error.

(3) database and found a transaction to take effect, Princess Iron Fan, 200-year-old data is not inserted.

 

 Package described here will be effective scanning configuration in spring core configuration file, but could not access the page, because the web layer packet scanning configuration in the spring core configuration file, modify the configuration in springmvc layer packet scanning for web, do a test.

Only scan web layer packet springmvc

(1) springmvc scanning configuration package

1      <!--开启包扫描-->
2      <!--<context:component-scan base-package="com.boe.dao"></context:component-scan>-->
3      <!--<context:component-scan base-package="com.boe.service"></context:component-scan>-->
4      <context:component-scan base-package="com.boe.web"></context:component-scan>

(2) spring packet scan files

1     <!-- 开启包扫描 -->
2     <!--<context:component-scan base-package="com.boe.web"/>-->
3     <context:component-scan base-package="com.boe.service"/>
4     <context:component-scan base-package="com.boe.dao"/>

(3) normal start tomcat server, but you can access the server discovery server returned an exception.

(4) view the database end data discovery is not inserted, the transaction into effect, data messi + 99 is not inserted. 

in conclusion

applicationContext.xml spring configuration file to configure the transaction, if you want the transaction to take effect, firms in the bag on the spring configuration file scanning, web package generally use springmvc annotations, and therefore need to be scanned in springmvc package configuration file. 

Reference Hirofumi:

(1) https://www.cnblogs.com/fanzuojia/p/10470182.html

(2) https://www.cnblogs.com/youngchaolin/p/10628776.html

Guess you like

Origin www.cnblogs.com/youngchaolin/p/11648289.html