1 問題描述
當使用Spring AOP對Controller層的Controller類的方法進行切面攔截,不起作用。AOP配置沒有任何問題。
2 排查過程
- Spring AOP配置沒有任何問題;【正常】
- 斷點調試:Spring源碼斷點調試,在調用Controller方法時,Controller的實例被JDK進行動態(tài)代理了;【不正常】
- Spring默認的代理方式為JDK動態(tài)代理;【正常】
3 解決問題
AOP有的人說攔截不到Controller。有的人說想攔AnnotationMethodHandlerAdapter截到Controller必須得攔截org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
。
首先AOP可以攔截到Controller的,這個是毋容置疑的其次須攔截AnnotationMethodHandlerAdapter也不是必須的
。最起碼我沒有驗證成功過這個。這個方式就不在這兒介紹說明了。
AOP之所以有的人說攔截不到Controller, 原因是該注解的Controller已被spring容器內(nèi)部代理了。我們只要把它交給cglib代理就可以了。Spring MVC的配置文件dispatcher-servlet.xml:
<!-- 通知spring使用cglib而不是jdk的來生成代理方法 AOP可以攔截到Controller -->
<aop:aspectj-autoproxy proxy-target-class="true" />
4 問題總結
Spring MVC 和 Spring 整合的時候,SpringMVC的dispatcher-servlet.xml文件中配置掃描包,不要包含 service的注解,Spring的applicationContext.xml文件中配置掃描包時,不要包含controller的注解,如下所示:
Spring MVC dispatcher-servlet.xml:
<context:component-scan base-package="com.qding">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
Spring MVC啟動時的配置文件,包含組件掃描、url映射以及設置freemarker參數(shù),讓Spring不掃描帶有@Service注解的類。為什么要這樣設置?因為springmvc.xml與applicationContext.xml不是同時加載,如果不進行這樣的設置,那么,Spring就會將所有帶@Service注解的類都掃描到容器中,等到加載applicationContext.xml的時候,會因為容器已經(jīng)存在Service類,使得cglib將不對Service進行代理,直接導致的結果就是在applicationContext 中的事務配置不起作用,發(fā)生異常時,無法對數(shù)據(jù)進行回滾。以上就是原因所在。
同樣的在Spring的applicationContext.xml配置如下:
<context:component-scan base-package="com.qding">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<context:component-scan/> 掃描指定的包中的類上的注解,常用的注解有:
@Controller 聲明Action組件
@Service 聲明Service組件 @Service("myMovieLister")
@Repository 聲明Dao組件
@Component 泛指組件, 當不好歸類時.
@RequestMapping("/menu") 請求映射
@Resource 用于注入,( j2ee提供的 ) 默認按名稱裝配,@Resource(name="beanName")
@Autowired 用于注入,(srping提供的) 默認按類型裝配
@Transactional( rollbackFor={Exception.class}) 事務管理
@ResponseBody
@Scope("prototype") 設定bean的作用域