1.spring mvc 自動掃描注解的時候,不去掃描@Service
<!-- lang: xml -->
<context:component-scan base-package= "org.cn.xxx">
<context:exclude-filter type ="annotation" expression= "org.springframework.stereotype.Service" />
</context:component-scan >
2.spring 自動掃描注解的時候,不去掃描@Controller
<!-- lang: xml -->
<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中的事務配置不起作用,發生異常時,無法對資料進行復原。