在java中怎么自定义注解

举例:

  • @Target(ElementType.METHOD):这是一个元注解,用于指定 PermissionLimit 这个注解可以应用的目标元素类型。在这里明确表示 PermissionLimit 注解只能应用于方法上,即只能在方法声明处使用该注解来添加权限相关的限制信息。例如,如果有一个 UserController 类中的 getUserInfo 方法,就可以在这个方法上添加 @PermissionLimit 注解来进行权限控制。
  • @Retention(RetentionPolicy.RUNTIME):同样是元注解,它决定了注解信息在什么阶段保留。RetentionPolicy.RUNTIME 表示这个注解的信息在程序运行时仍然可以被获取到。这对于需要在运行时根据注解信息进行权限检查等操作是非常必要的,因为只有在运行时能获取到注解信息,才能依据这些信息来判断方法是否满足相应的权限要求。

定义一个名为 MyAnnotation 的简单注解,用于标记某个方法是否需要进行性能监控: 

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

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    boolean monitorPerformance() default false;
}

 在上述代码中:

  • @Target(ElementType.METHOD):这是一个元注解,用于指定 MyAnnotation 注解可以应用的目标元素类型。这里表示 MyAnnotation 注解只能应用于方法上,即只能在方法声明处使用该注解。
  • @Retention(RetentionPolicy.RUNTIME):同样是元注解,它决定了注解信息在什么阶段保留。RetentionPolicy.RUNTIME 表示这个注解的信息在程序运行时仍然可以被获取到,以便后续根据注解信息进行相关操作。
  • boolean monitorPerformance() default false;:定义了 MyAnnotation 注解的一个属性 monitorPerformance,它是一个布尔类型的属性,并且默认值为 false。这个属性用于指定是否对标注了该注解的方法进行性能监控。当 monitorPerformance 设置为 true 时,表示需要对该方法进行性能监控;当 monitorPerformance 设置为 false(默认情况)时,表示不需要进行性能监控。

使用注解的示例:

public class MyClass {

    @MyAnnotation(monitorPerformance = true)
    public void myMethod() {
        // 这里是方法的具体实现内容
        System.out.println("执行 myMethod 方法");
    }
}

 在 MyClass 的 myMethod 方法上使用了 MyAnnotation 注解,并将 monitorPerformance 属性设置为 true,表示希望对这个方法进行性能监控。

要根据注解信息进行实际的操作(如在上述示例中根据是否需要性能监控来执行相应的操作),通常需要使用反射机制来获取并处理注解信息。 如何通过反射获取 MyClass 中 myMethod 方法上的 MyAnnotation 注解信息并根据其进行相应操作:

import java.lang.reflect.Method;

public class AnnotationProcessor {

    public static void main(String[] args) {
        try {
            Class<?> clazz = MyClass.class;
            Method method = clazz.getMethod("myMethod");

            MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
            if (annotation!= null) {
                if (annotation.monitorPerformance()) {
                    System.out.println("需要对 myMethod 方法进行性能监控");
                    // 这里可以添加实际的性能监控相关代码,比如记录方法执行时间等
                } else {
                    System.out.println("不需要对 myMethod 方法进行性能监控");
                }
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
}
  • 首先通过 Class<?> clazz = MyClass.class 获取了 MyClass 的类对象。
  • 然后通过 Method method = clazz.getMethod("myMethod") 获取了 myMethod 方法的 Method 对象。
  • 接着通过 MyAnnotation annotation = method.getAnnotation(MyAnnotation.class) 获取了 myMethod 方法上的 MyAnnotation 注解对象。
  • 最后根据注解对象的 monitorPerformance 属性的值来判断是否需要对 myMethod 方法进行性能监控,并输出相应的提示信息。如果需要进行性能监控,在实际应用中还可以在这里添加具体的性能监控相关代码,比如使用 System.currentTimeMillis() 来记录方法执行前后的时间,从而计算出方法的执行时间等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小懒懒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值