springboot aop

本文展示了如何在SpringBoot项目中创建一个带有main方法的应用,实现CommandLineRunner接口,以及定义和使用AOP切面。通过Annotation1注解触发Before、AfterReturning和Around通知,实现方法执行前后的操作。同时,文章还包含了测试用例的编写,使用了@SpringBootTest和@Test注解进行单元测试。
摘要由CSDN通过智能技术生成

首先一个springboot项目需要一个带main方法的类,然后这个类需要注解,还需要继承

@SpringBootApplication
public class ApplicationMain implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationMain.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("run...");
    }
}

其次定义aspect要注意注解的使用

@Aspect
@Component
public class Aspect1 {

    @Pointcut("@annotation(com.dick.job.annotation.Annotation1)")
    public void pointcut1() {
    }

    @AfterReturning(returning = "result", value = "pointcut1()")
    public void aspectFunction1(JoinPoint joinPoint, Object result) throws Throwable {
        System.out.println("返回值:" + result);

        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        System.out.println(className + "  " + methodName);

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Annotation1 annotation1 = method.getAnnotation(Annotation1.class);
        System.out.println(annotation1.idName());

        for (Object arg : joinPoint.getArgs()) {
            System.out.println("args: " + arg);
        }
    }

    @Around("@annotation(com.dick.job.annotation.Annotation1)")
    public String aroundFunction(ProceedingJoinPoint joinPoint) throws Throwable {
        long beginTime = System.currentTimeMillis();
        //执行方法
        Object result = joinPoint.proceed();
        //执行时长(毫秒)
        long time = System.currentTimeMillis() - beginTime;

        System.out.println(time);

        return result.toString() + time;
    }

}

还有test的使用,

package com.dick.job;	//test/java目录下的包结构要和main/java目录下的包结构一样
@SpringBootTest
public class utilTest {

    @Autowired
    Class1 class1;

    @Test
    public void test1() {
        class1.testFunction1("dic", "pus", 3);
    }
}

上面的问题花了我一天,百度太难用了,浪费生命
给你看看测试函数

@Component
@Log4j2
public class Class1 {
    
    @Annotation1(idName = "testFunction1")
    public String testFunction1(String a, String b, int c) {
        log.trace("dick...");
        for (int i=0; i<c; i++) {
            System.out.println(a + "  " + b);
        }

        return "asldkasdfasdlkfjas;ldkjfalsdjf;lasjfdlkasjf";
    }

}

和annotation定义

package com.dick.job.annotation;

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface Annotation1 {
    String idName() default "id1";
}

结果
**
2021-10-29 16:12:01.550 [main] TRACE com.dick.job.utils.Class1 - dick…
dic pus
dic pus
dic pus
9
返回值:asldkasdfasdlkfjas;ldkjfalsdjf;lasjfdlkasjf9
com.dick.job.utils.Class1 testFunction1
testFunction1
args: dic
args: pus
args: 3
**

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值