本人新手,在使用spring的AOP强大功能的时候遇到了一点小坑,在这里记录下来一来为了给自己提供记录,二来供后人参考。
鱼唇的我尝试着对controller进行aop的切片,然后一直成功不了,郁闷了好久好久。后来查阅资料发现:如果在springmvc的配置文件里面扫描了类,那么spring去扫描的时候发现内存中已经有了对象,就不会在对类进行aop增强。
所以当我们确定在那一层切入的时候,那么在springmvc的配置文件中,应该要排除欲切入的层。
也就是说,如果配置文件和我类似:
spring配置文件中剔除了controller注解:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <!-- 注解开启 --> <context:component-scan base-package="com.xxx.admin,com.xxx.core"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> </context:component-scan> <!-- aop 开启 --> <aop:aspectj-autoproxy proxy-target-class="true"/>
springMVC的配置文件中包含了controller注解:
<?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" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <!-- 注解开启 --> <mvc:annotation-driven/> <context:component-scan base-package="com.xxxx.admin" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>这样子的话,就不能对controller进行切面。<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/></context:component-scan>
参考:
https://www.oschina.net/question/222929_124314
http://www.cnblogs.com/guokai870510826/p/5981015.html
http://www.cnblogs.com/yxjdragon/p/5804198.html