1. 程式人生 > >springmvc mybatis 基於全註解事務配置注意事項

springmvc mybatis 基於全註解事務配置注意事項

1.spring mvc 自動掃描註解的時候,不去掃描@Service

<!--  xml -->
<context:component-scanbase-package= "org.cn.xxx">
<context:exclude-filtertype ="annotation"expression= "org.springframework.stereotype.Service" />
</context:component-scan >

2.spring 自動掃描註解的時候,不去掃描@Controller

<!-- xml -->
<context:component-scanbase-package ="org.cn.xxx> <context:exclude-filter type ="annotation" expression= "org.springframework.stereotype.Controller" /> </context:component-scan >

Spring MVC啟動時的配置檔案,包含元件掃描、url對映以及設定freemarker引數,讓spring不掃描帶有@Service註解的類。為什麼要這樣設定?因為servlet-context.xml與service-context.xml不是同時載入,如果不進行這樣的設定,那麼,spring就會將所有帶@Service註解的類都掃描到容器中,等到載入service-context.xml的時候,會因為容器已經存在Service類,使得cglib將不對Service進行代理,直接導致的結果就是在service-context中的事務配置不起作用,發生異常時,無法對資料進行回滾。