Spring框架注解开发(IDEA)——关于Aop功能测试

从简单来说Aop【动态管理】:指在运行期间将某段代码切入到指定方法的指定位置运行的编程

下面带大家来测试一下Aop的功能测试

1 导入SpringAop的依赖 

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>5.2.13.RELEASE</version>
</dependency>

1.1创建业务逻辑类

package com.Aop;
//业务逻辑类
public class MathCalculator {

    public int div(int i,int j){
        System.out.println("div方法被调用");
        return i/j;
    }
}

1.2定义日志切面类 定义@Aspect定义为切面

package com.Aop;

import org.aspectj.lang.annotation.*;

@Aspect
public class LogAspect {

    @Pointcut("execution(public int com.Aop.MathCalculator.*(..))")
    public void pointcut(){

    }


    @Before("pointcut()")
    public void logStart(){
        System.out.println("除法运行前");
    }
    @After("pointcut()")
    public void logEnd(){
        System.out.println("除法运行后");
    }
    @AfterReturning("pointcut()")
    public void logReturn(){
        System.out.println("除法正常返回");
    }
    @AfterThrowing("pointcut()")
    public void logException(){
        System.out.println("除法异常返回");
    }

}

@Before("pointcut()"):目标方法运行前执行

@After("pointcut()"):目标方法运行后执行

@AfterReturning("pointcut()"):目标方法正常返回后执行

@AfterThrowing("pointcut()"): 目标方法异常后执行

1.3 定义Config类,并添加@EnableAspectAutoProxy ,开启注解Aop模式

.

package com.Config;

import com.Aop.LogAspect;
import com.Aop.MathCalculator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

//切入到指定位置 AOP【动态代理】
@Configuration
@EnableAspectJAutoProxy
public class AopConfig {

    @Bean
    public MathCalculator calculator(){
        return new MathCalculator();
    }
    @Bean
    public LogAspect aspect(){
        return new LogAspect();
    }

}

1.4 创建测试类

package com;

import com.Aop.MathCalculator;
import com.Config.AopConfig;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class AopTest {

    @Test
    public void test() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AopConfig.class);

        MathCalculator mathCalculator = applicationContext.getBean(MathCalculator.class);
        mathCalculator.div(2,1);

    }
}

1.5 运行后

        


方法正常运行后得到的结果 

如果你想要得到拿到值,我们可以在方法中添加JoinPoint来获取方法名

package com.Aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

@Aspect
public class LogAspect {

    @Pointcut("execution(public int com.Aop.MathCalculator.*(..))")
    public void pointcut(){

    }


    @Before("pointcut()")
    public void logStart(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        System.out.println(joinPoint.getSignature().getName()+"除法运行前");
    }
    @After("pointcut()")
    public void logEnd(JoinPoint joinPoint){
        System.out.println(joinPoint.getSignature().getName()+"除法运行后");
    }
    @AfterReturning(value = "pointcut()",returning="result")
    public void logReturn(JoinPoint joinPoint,Object result){
        System.out.println(joinPoint.getSignature().getName()+"除法正常返回..."+result);
    }
    @AfterThrowing(value = "pointcut()",throwing="exception")
    public void logException(JoinPoint joinPoint,Exception exception){
        System.out.println(joinPoint.getSignature().getName()+"除法异常返回"+exception);
    }

}

在返回值中添加参数result,可以获得返回的值

运行后得到方法名和除法后的值 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值