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

     
  • 相关阅读:
    交换排序------冒泡法 及其优化
    [编程题]最大和子矩阵
    [编程题] N阶楼梯上楼问题
    2017年东北大学计算机专业考博 面试编程题(某教授 实验室)
    幸运的袋子 (牛客网 16年网易内推 编程题)
    OpenvSwitch代码分析之bridge和port
    阅读书籍---程序员必读系列
    嵌入式开发之davinci--- 8168 电源调试总结
    嵌入式开发之davinci--- 8148/8168/8127 中的添加算饭scd 场景检测 文档简介
    嵌入式开发之davinci--- 8148/8168/8127 中的图像缩放sclr、swms之后出现图像视频卡顿、屏幕跳跃的问题
  • 原文地址:https://www.cnblogs.com/rickzhai/p/6504355.html
Copyright © 2020-2023  润新知