SpringMVC关于AOP拦截controller的注意事项
来源:互联网 发布:radon变换算法 编辑:程序博客网 时间:2024/05/29 12:13
在网上也找了不少资料,但是感觉网上都没说清楚,在这里我把自己亲自验证的结果和配置方法在这里详细的讲解:
首先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>
阅读全文
0 0
- SpringMVC关于AOP拦截controller的注意事项
- SpringMVC关于AOP拦截controller的注意事项
- SpringMVC的AOP拦截controller
- Springmvc AOP拦截Controller获取session(自定义注解的方式)
- Spring Aop拦截controller配置注意事项
- 在springMVC中使用AOP拦截controller
- spring拦截器 拦截springMVC的controller
- Sping AOP用AspectJ注解的方式拦截不到SpringMVC的controller方法?
- 使用 aop拦截 springMVC的controller并获取请求参数及返回结果
- Spring aop拦截springmvc的controller请求方法,添加日志和统计方法执行时间
- 日志AOP拦截Controller
- SpringMVC拦截Controller方法
- SpringMVC 无法使用aop拦截的解决方案
- spring Aop拦截controller方法
- spring aop学习--拦截controller
- aop拦截service&&controller《一》
- aop拦截service&&controller《二》
- Spring AOP 对Spring MVC的Controller切面拦截不起作用
- 使用 SiC MOSFET:挑战与设计建议---凯利讯半导体
- JS——事件的追加与移除
- JavaScript
- linux-3.5内核编译失败的问题
- 顺序串基本运算
- SpringMVC关于AOP拦截controller的注意事项
- 《android framework常用api源码分析》之apk打包过程解析
- 2017年12月12日 qsort函数理解、多态的实验
- mysql锁表机制分析
- BlueBorne远程代码执行漏洞Poc实战(CVE-2017-0781)
- sqlserver与oralce之间的数据库链接的创建
- Promise到底是个什么
- 玩嵌入式,可以试试Micro Bit!
- python @classmethod和@staticmethod 装饰器使用