Spring Aspect 用法略讲

『配置Aspect』
若要启用AspectJ风格的注解则必须额外的导入AspectJ的jar包,此外还需要在spring的配置文件中进行配置,配置方式有两种;

一.在配置文件的Schema中进行配置
第一步:在schema中添加xmlns:aop="http://www.springframework.org/schema.aop"
第二步:在schema中的xsi:schemaLocation中添加两行:http://www.springframework.org/schema/aop以及http://www.springframework.org/schema/aop/spring-aop.xls
第三步:在beans标签体中添加<aop:aspectj-autoproxy/>

二.在beans标签体中添加一个bean
第一步:添加<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator">
第二步:在beans标签体中添加<aop:aspectj-autoproxy/>

『Aspect使用讲解』
有两种方式,第一种是使用AspectJ风格的注解,第二种是使用XML配置文件

零.预备
execution(modifier returnType classType methodName params throwsType)——切入点指示符
用法讲解
六个参数均支持通配符"*",但methodName还额外支持"(..)"通配符用来匹配一个或者多个参数,六个参数中returnType,methodName,params这三者不能省略,其余均可省略
用法示例
execution(* * (..))——匹配任意返回类型,任意方法名,任意参数的方法,也就是一切方法都可以
execution(public * *Controller get* (*,String))——匹配任意类访问修饰符为public的,任意返回值类型的,属于以Controller结尾的类的,方法名以get开头的,参数一个任意类型一个String类型的方法
execution(* org.xt.controller.*Controller.* (..))——此用法不言而喻

一.使用AspectJ风格的注解,这类注解spring支持的有:
@Aspect——此注解用来将一个类定义成一个切面
用法示例
@Aspect
classModifier class className{}

@Before——此注解是用来标注增强方法,其作用是将增强方法切入连接点中,使其在连接点方法执行之前执行
用法示例
@Aspect
classModifier class className{
    @Before("execution(* org.xt.controller.*.* (..))")
    methodModifier methodReturnType methodName(){}
}

@AfterReturning——此注解是用来标注增强方法,其作用是将增强方法切入连接点中,使其在切入点点方法执行完成成功返回之后执行,它有两个属性分别为pointcut和returning
用法讲解
returning用来指定切入点的返回值对象
用法示例
@Aspect
classModifier class className{
    @AfterReturning(returning="desReturnValue",pointcut="execution(* org.xt.controller.*.* (..))")
    methodModifier methodReturnType methodName(Object desReturnValue){
        //打印返回值对象
        System.out.println(String.valueOf(desReturnValue));
    }
}

@AfterThrowing——此注解是用来标注增强方法,其作用是将增强方法切入连接点中,用来处理切入点方法未处理的异常,它有两个属性分别为pointcut和throwing
用法讲解
throwing用来指定切入点抛出的异常对象
用法示例
@Aspect
classModifier class className{
    @AfterThrowing(throwing="desThrowValue",pointcut="execution(* org.xt.controller.*.* (..))")
    methodModifier methodReturnType methodName(Throwable desThrowValue){
        //打印异常消息
        System.error.println(desThrowValue.getMessage());
    }
}

@After——此注解是用来标注增强方法,其作用是将增强方法切入连接点中,使其在切入点点方法执行之后执行,此标注并不要求切入点方法一定要成功执行完成
用法示例
@Aspect
classModifier class className{
    @After("execution(* org.xt.controller.*.* (..))")
    methodModifier methodReturnType methodName(){}
}

@Around——此注解的功能最是强大,因为其标注的增强方法可以直接干预切入点方法的执行
用法讲解
被此标注的增强方法必须有一个参数,参数类型为ProceedingJoinPoint
用法示例
@Aspect
classModifier class className{
    @Around("execution(* org.xt.controller.*.* (..))")
    methodModifier methodReturnType methodName(ProceedingJoinPoint point){}
}

补充:以上共有5种增强方法的标注,在增强方法中若要访问切入点的一些数据,比如切入点方法的参数,切入点方法所属的类对象,切入点方法的代理类对象以及切入点方法本身的一些数据则需要用到JoinPoint类
用法讲解
第一步:为增强方法定义一个JoinPoint类型的参数(@Around增强的方法就不必了,因为其本身就有一个ProceedingJoinPoint)
第二步:此时可以使用JoinPoint类型的参数对象来分别调用Object[] getArgs()来获取切入点方法的参数,Object getTarget()来获取切入点方法的类对象,Object getThis()来获取切入点方法的代理类对象,Signature getSignature()来获取切入点方法的本身信息
用法示例

@Aspect
classModifier class className{
    @Before("execution(protected * org.xt.controller.*.*(..))")
    methodModifier methodReturnType methodName(JoinPoint point){
        Object[] args=point.getArgs();
        for(Object arg:args){
            System.out.println(String.valueOf(arg));
        }
    }

    @AfterReturning(returning="",pointcut="execution(protected * org.xt.controller.*Controller.*(..))")
    methodModifier methodReturnType methodName(JoinPoint point){
        Object target=point.getTarget();
        //打印切入点方法的对象
        System.out.println("切入点方法的类对象为"+target.toString());
    }

    @AfterThrowing(throwing="",pointcut="execution(* org.xt.controller.*.* (*,String,Object))")
    methodModifier methodReturnType methodName(JoinPoint point){
        Object[] args=point.getArgs();
        for(Object arg:args){
            System.out.println(String.valueOf(arg));
        }
    }

    @After("execution(* org.xt.controller.*.* (..))")
    methodModifier methodReturnType methodName(JoinPoint point)){
        //打印切入点方法的方法名
        System.out.println("切入点方法的方法名为"+point.getSignature().getName());
    }

    @Around("execution(* org.xt.controller.*.* (..))")
    methodModifier methodReturnType methodName(ProceedingJoinPoint point){
        Object proxy=point.getThis();
        System.out.println("切入点方法的代理对象为"+proxy.toString());
        //执行切入点函数,可以改变切入点函数的参数
        Object returnValue=point.proceed(new String[]{});
    }
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。