spring aop:aspectj-autoproxy 配置


经常遇到aop-aspectJ的通知不被执行的问题

解决方法:http://blog.csdn.net/qwdafedv/article/details/53005210

 

首先,确保配置文件都已经是正确的。

1、首先,把所写的通知所在的类交于spring来管理

<context:component-scan base-package="me.sui.user.aop" />

注意,其头部文件:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">

2、然后,应该还有一条

<aop:aspectj-autoproxy/>

之所以没有和上面放在一起,等会再说。

3、切面通知类

package me.sui.user.aop; import javax.servlet.http.HttpServletRequest; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; /** * 注册切面 * @author qubianzhong * */ @Aspect @Component public class RegisterAspect { /** * studentService */ @Autowired @Qualifier("studentService") private StudentService studentService; /** * 后置通知,用户注册成功后,将注册数据信息发送到神策数据进行分析 * @param joinPoint 接入点 * @param result 目标方法的返回结果 */ @AfterReturning(value="execution(* me.sui.user.rest.StudentRestController.register(..))", returning = "result") public void afterMethod(JoinPoint joinPoint,Object result){ if((boolean) result){ } } }

4、以上都准备妥当后,通知还不可以被执行,是因为下面这几个坑:

一、我所拦截的类,即被切的类,是个servlet;只有当切面类和被切面类都被spring来管理的时候,通知才可以使用。


二、基于第一条,如果你换成拦截器也是不行的。


三、如果你使用的是springMVC,你所拦截的切面类也是个controller,但是,还是不行,可能就是因为你把

<aop:aspectj-autoproxy/>

也放在了application.xml中了。可能是springmvc的bug吧。你把

<aop:aspectj-autoproxy/>

放到DispatcherServlet所对应的**-servlet.xml配置文件中,就可以了。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM