27、AOP-AOP功能测试

这篇博客主要介绍了如何进行Spring AOP的功能测试,包括@EnableAspectJAutoProxy注解的使用以开启注解驱动的AOP,@Aspect用于声明切面,@Before和@After注解定义前置和后置通知,以及JoinPoint作为切入点表达式的使用。
摘要由CSDN通过智能技术生成

 

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>4.3.12.RELEASE</version>
</dependency>
package com.lei.study_09_19.bean;

import com.lei.study_09_19.anno.MyAnno;
import org.aspectj.lang.annotation.Aspect;

/**
 *
 *
 * @author LeiLei
 * @date 2019/9/19
 */
public class Calc {

    public int div(int a,int b) {
        System.out.println(a/b);
        return a/b;
    }

}

 

package com.lei.study_09_19.aop;

import com.lei.study_09_19.anno.MyAnno;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;
import java.util.Collections;

/**
 * 
 * 
 * @author LeiLei
 * @date 2019/9/19
 */
@Aspect
public class CalcAop {

    @Pointcut("execution(public int com.lei.study_09_19.bean.Calc.*(..))")
    public void pointCut() {
    }

    @Before("pointCut()")
    public void calcBefore() {
        System.out.println("我在calc之前干了事情!!!");
    }

    @After("pointCut()")
    public void calcAfter() {
        System.out.println("我在calc之后干了事情!!!");
    }

    @AfterReturning(value = "pointCut()",returning = "res")
    public void calcAfter(JoinPoint joinPoint,int res) {
        System.out.println("你的参数我都知道:"+ Arrays.toString(joinPoint.getArgs()));
        System.out.println("我知道了你的返回值:"+res);
    }

    @AfterThrowing(value = "pointCut()",throwing = "exception")
    public void calcAfter(JoinPoint joinPoint,Exception exception) {
        System.out.println("你的方法名我都知道:"+joinPoint.getSignature().getName());
        System.out.println("你的异常我都知道:"+ exception);
    }

}

 

package com.lei.study_09_19.config;

import com.lei.study_09_19.aop.AnnoAop;
import com.lei.study_09_19.aop.CalcAop;
import com.lei.study_09_19.bean.Calc;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.annotation.*;
import org.springframework.util.StringValueResolver;

/**
 *
 *
 * @author LeiLei
 * @date 2019/9/16
 */
@EnableAspectJAutoProxy
@Configuration
public class BeanConfig {
    
    @Bean
    public Calc calc()  {
        return new Calc();
    }

    @Bean
    public CalcAop calcAop()  {
        return new CalcAop();
    }


} 

测试结果: 

 我在calc之前干了事情!!!
4
我在calc之后干了事情!!!
你的参数我都知道:[8, 2]
我知道了你的返回值:4

package com.lei.study_09_19.aop;

import com.lei.study_09_19.anno.MyAnno;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * 
 * 
 * @author LeiLei
 * @date 2019/9/19
 */
@Aspect
public class AnnoAop {

    @Pointcut("@annotation(com.lei.study_09_19.anno.MyAnno)")
    public void pointCut() {
    }

    /**
     *      String toString();         //连接点所在位置的相关信息
     *     String toShortString();     //连接点所在位置的简短相关信息
     *     String toLongString();     //连接点所在位置的全部相关信息
     *     Object getThis();         //返回AOP代理对象
     *     Object getTarget();       //返回目标对象
     *     Object[] getArgs();       //返回被通知方法参数列表
     *     Signature getSignature();  //返回当前连接点签名
     *     SourceLocation getSourceLocation();//返回连接点方法所在类文件中的位置
     *     String getKind();        //连接点类型
     *     StaticPart getStaticPart(); //返回连接点静态部分
     * @param joinPoint
     */
    @Before("pointCut()")
    public void calcBefore(JoinPoint joinPoint) throws Exception {
//        String aa = joinPoint.getKind();//method-execution
//        Object aa = joinPoint.getTarget();//com.lei.study_09_19.bean.Calc@10163d6
//        String aa = joinPoint.getSignature().getDeclaringTypeName();//com.lei.study_09_19.bean.Calc
        Class clzz = joinPoint.getSignature().getDeclaringType();//class com.lei.study_09_19.bean.Calc
        String aa = joinPoint.getSignature().getName();//bbb
        Method m = clzz.getMethod(aa);
        MyAnno anno = m.getAnnotation(MyAnno.class);
        String annoName = anno.name();

        System.out.println("我发现你了:"+joinPoint.getSignature().getName()+"|||"+annoName);
    }

}

总结:

1.@EnableAspectJAutoProxy开启注解

2.@Aspect声明切面,写在类上

3..@PonitCut写切入点

2.@Before、@After写前、后、环绕等通知 

3.JoinPoint只能放在第一个参数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值