SpringMVC关于AOP拦截controller的注意事项
2017-12-12 11:47
459 查看
在网上也找了不少资料,但是感觉网上都没说清楚,在这里我把自己亲自验证的结果和配置方法在这里详细的讲解:
首先aop切面是可以拦截controller层的,有以下三个条件:
1.Controller层和你自己定义的切面,必须在相同的spring上下文中(context):
2.通常大家会把 controllers 定义在dispatch-servlet.xml 或者 xxx-servlet.xml 这样的配置文件中,但是把自定义的切面放在spring的主配置文件 applicationContext.xml中。这样子导致controller和你的切面不在同一个context中,从而你的切面类逻辑不会拦截对应的controller:
3.当spring初始化MVC的context的时候,它会同时为controller 创建代理,但是如果自定义切面没有和mvc在同一个context中,那么你的切面是不会去拦截这些controller的:
综合上面的三点:具体到我们如何实践呢?
(1)maven管理jar包,引入aop和aspectj的相关jar包:
(2)springmvc配置文件:
配置文件中需要把切面类和controller放在同一个spring的xml配置文件中:
首先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 4000 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>
相关文章推荐
- SpringMVC关于AOP拦截controller的注意事项
- Spring Aop拦截controller配置注意事项
- Springmvc关于拦截所有网址的一个致命错误,需注意!
- 使用 aop拦截 springMVC的controller并获取请求参数及返回结果
- Springmvc AOP拦截Controller获取session(自定义注解的方式)
- java SpringmvcAOP拦截Controller,获取session(自定义注解的方式)
- SpringMVC的AOP拦截controller
- 在springMVC中使用AOP拦截controller
- SpringMVC中配置AOP拦截controller 失效
- springmvc - 关于Controller中方法参数类型的几点注意
- springmvc配置DispatcherServlet拦截url注意事项
- SpringMVC中关于@Resource与@Autowired注解的区别和使用注意事项
- 关于NSPredicate的其他说明和注意事项,以及技巧
- 关于apk 反编译的使用和注意事项
- 当项目使用SpringMvc时拦截器的注意事项
- 关于java多线程网络编程的注意事项!
- 20160512关于springMvc权限拦截问题
- 某高校关于2011年学生注册注意事项的通知
- php关于session应注意的事项
- 关于使用存储过程的一些好处以及注意事项