您的位置:首页 > 编程语言 > ASP

aop-aspectJ的通知不被执行

2016-11-02 10:33 267 查看

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

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配置文件中,就可以了。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring aspectj 通知