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

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


<context:component-scan base-package= "org.cn.xxx">
<context:exclude-filter type ="annotation" expression= "org.springframework.stereotype.Service" />
</context:component-scan >

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


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

參考:http://blog.sina.com.cn/s/blog_5ddc071f0100uf7x.html。尤其是這段話:

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章