天天看点

SpringMVC关于AOP拦截controller的注意事项

在网上也找了不少资料,但是感觉网上都没说清楚,在这里我把自己亲自验证的结果和配置方法在这里详细的讲解:

首先aop切面是可以拦截controller层的,有以下三个条件:

1.Controller层和你自己定义的切面,必须在相同的spring上下文中(context):

Indeed your controller (annotated by @Controller) and your aspects (annotated by @Aspect) should be in the same Spring context. 
           

2.通常大家会把 controllers 定义在dispatch-servlet.xml 或者 xxx-servlet.xml 这样的配置文件中,但是把自定义的切面放在spring的主配置文件 applicationContext.xml中。这样子导致controller和你的切面不在同一个context中,从而你的切面类逻辑不会拦截对应的controller:

Usually people define their controllers in the dispatch-servlet.xml or xxx-servlet.xml and their service beans (including the aspects) in  the main applicationContext.xml. It will not work. 
           

3.当spring初始化MVC的context的时候,它会同时为controller 创建代理,但是如果自定义切面没有和mvc在同一个context中,那么你的切面是不会去拦截这些controller的:

When Spring initializes the MVC context, it will create a proxy for your controller but if your aspects are not in the same context, Spring will not create interceptors for them.
           

综合上面的三点:具体到我们如何实践呢?

(1)maven管理jar包,引入aop和aspectj的相关jar包:

<dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-aop</artifactId>  
            <version>${spring.version}</version>  
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
           

(2)springmvc配置文件:

配置文件中需要把切面类和controller放在同一个spring的xml配置文件中:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
                            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
                            http://www.springframework.org/schema/context    
                            http://www.springframework.org/schema/context/spring-context-3.1.xsd    
                            http://www.springframework.org/schema/mvc    
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
                            http://www.springframework.org/schema/aop 
                            http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
    <!--扫描@Controller注解 -->
    <context:component-scan base-package="com.min.controller">
        <context:include-filter type="annotation"
    expression="org.springframework.stereotype.Controller" />
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Service" />
    </context:component-scan>
    <!-- 开启asepctj注解扫描 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    ...
    省略
    ...
</beans>  
           

继续阅读