今天配置了半天,發現,事物不起效果,主要出現如下錯誤:
org.mybatis.spring.transaction.SpringManagedTransaction] - [JDBC Connection [com.jolbox.bonecp.ConnectionHandle@120fc40] will not be managed by SpringSqlsession [org.apache.ibatis.session.defaults.DefaultSqlSession@47eb1b] was not registered for synchronization because synchronization is not activeClosing non transactional SqlSession
后臺看到大象的博文才找到答案,意思就是要排出spring servlet對@service注解的掃描,不然會導致事物配置失效,過濾排除@service注解就行了。
Spring MVC啟動時的配置文件,包含組件掃描、url映射以及設置freemarker參數,讓spring不掃描帶有@Service注解的類。為什么要這樣設置?因為servlet-context.xml與service-context.xml不是同時加載,如果不進行這樣的設置,那么,spring就會將所有帶@Service注解的類都掃描到容器中,等到加載service-context.xml的時候,會因為容器已經存在Service類,使得cglib將不對Service進行代理,直接導致的結果就是在service-context中的事務配置不起作用,發生異常時,無法對數據進行回滾。另外能夠將REST URL解析為請求映射的是DefaultAnnotationHandlerMapping這個類,它在啟動時,對Controller中所有標注了@RequestMapping注解的方法,都放到了一個HandlerMapping對象中,當有請求時,就在這個對象中進行查找是否有與之匹配路徑的處理方法,有則執行,沒有就會輸出一個Not Page Found警告信息。
1 <!-- 定義控制器注解掃描包路徑,控制器注解為 @Controller ,必須排除@Service注解 -->2 <context:component-scan base-package="com.teshehui.product">3 <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" />4 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>5 </context:component-scan>
參考:Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解實例詳解(二)
新聞熱點
疑難解答