自定义注解并进行使用

自定义注解学习与使用

案例代码

1. 自定义注解@LogExecutionTime
  • 当前注解只用于标识方法
package com.bytedance.annotation.annotation1;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author lms
 * @version 1.0
 * @date 2023/11/3 9:42
 */
// 定义注解的应用目标(方法)
@Target(ElementType.METHOD)
// 定义注解的保留策略(运行时有效,因为我们在运行时通过反射读取它)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogExecutionTime {

}
2. 定义切面(切入点 + 通知)
package com.bytedance.annotation.annotation1;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * @author lms
 * @version 1.0
 * @date 2023/11/3 9:43
 * 定义一个切面处理@LogExecutionTime注解 注释的方法
 * 切面 = 切入点 + 通知
 * 拦截被注解标识的方法,然后对这些方法进行增强,比如:日记,事务,安全等
 */
@Aspect
@Component
public class LoggingAspect {

    /**
     * 环绕通知: 处理所有被 @LogExecutionTime 注释的注解
     * 定义切入点和通知,应用于带有@LogExecutionTime注解的方法
     * @param joinPoint 表示正在执行的连接点
     * @return
     */
    @Around("@annotation(LogExecutionTime)")
    public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        // 执行目标方法
        Object proceed = joinPoint.proceed();
        long end = System.currentTimeMillis();
        long offset = end - start;
        // joinPoint.getSignature(): 获取目标方法的签名,通常用于记录日志或其他目的
        System.out.println(joinPoint.getSignature() + " executed in " + offset + " ms");
        return proceed;
    }
}
3. 启动切面设置
package com.bytedance.annotation.annotation1;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 * @author lms
 * @version 1.0
 * @date 2023/11/3 9:50
 * 配置切面
 */
@Configuration
@EnableAspectJAutoProxy
public class AspectConfig {

}
4. 具体业务实现
package com.bytedance.annotation.annotation1;

import org.springframework.stereotype.Service;

/**
 * @author lms
 * @version 1.0
 * @date 2023/11/3 9:51
 */
@Service
public class DemoServiceImpl {
    @LogExecutionTime
    public int calculate(int n) {
        int sum = 0;
        for (int i = 0; i < n; i++) {
            sum += i;
        }
        return sum;
    }
}
package com.bytedance.annotation.annotation1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lms
 * @version 1.0
 * @date 2023/11/3 9:51
 */
@RestController
public class DemoController {
    @Autowired
    private DemoServiceImpl demoService;

    @GetMapping("/{num}")
    public int cal(@PathVariable int num) {
        int calculate = demoService.calculate(num);
        return calculate;
    }
}
5. 调用方法,控制台打印输出
2023-11-03 14:56:59.780  INFO 10844 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 0 ms
int com.bytedance.annotation.annotation1.DemoServiceImpl.calculate(int) executed in 8 ms
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值