Spring MVC開啓註解

一、在spring的配置文件中配置:<context:annotation-config />該項配置只能應用於已經註冊的bean,比較難用,不深究。spring

二、在spring的配置文件中使用context:component-scan,該項能夠指明要掃描的package,並將標有@component註解的bean註冊到spring容器當中。express

根據網上的部分教程(我的未實踐確認過),由於use-default-filters默認爲true,配置了此項標籤後,系統會自動加載全部標有@component註解,包括@Service/@Repository,這會致使事務失效。將use-default-filters設爲false,並制定要掃描的filter,能夠修復此問題。.net

<!-- 使用Annotation自動註冊Bean,只掃描@Controller -->
<context:component-scan base-package="com.owlforest" use-default-filters="false">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
相關文章
相關標籤/搜索