aop-aspectJ的通知不被执行

首先,确保配置文件都已经是正确的。
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配置文件中,就可以了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值