在项目中如何自定义注解,很多地方都会用到,自定义注解一般来说有俩种方式。
如何自定义注解呢
要自定义注解,可以使用 Java 语言提供的 @interface 关键字来创建一个新的注解类型。下面是一个简单的自定义注解的示例:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
String value() default "";
int count() default 0;
}
上面的代码定义了一个名为 MyAnnotation 的注解类型,它具有两个成员变量 value 和 count,并分别指定了它们的默认值为空字符串和零。@Retention 注解用于指定该注解的保留策略,这里采用的是运行时保留。@Target 注解用于指定该注解可以用于哪些程序元素上,这里指定为方法级别。
使用自定义注解时,可以在需要标记的方法上使用 @MyAnnotation 注解,并在括号内指定对应的成员变量值。例如:
@MyAnnotation(value = "Hello, world!", count = 3)
public void myMethod() {
// 方法体
}
上述代码中,myMethod 方法被 @MyAnnotation 注解标记,并指定了 value 成员变量的值为 “Hello, world!”,count 成员变量的值为 3。
使用自定义注解时,可以根据需要自行定义注解的成员变量和保留策略,并在程序中使用 @annotation 表达式来捕获该注解类型,实现自定义的业务逻辑。
@annotation的使用例子
@annotation 可以用于捕获特定注解类型的方法或类,并在方法执行前后进行拦截和操作。下面是一个使用 @annotation 的例子:
@Aspect
@Component
public class MyAspect {
@Around("@annotation(MyAnnotation)")
public Object doSomething(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
MyAnnotation myAnnotation = methodSignature.getMethod().getAnnotation(MyAnnotation.class);
// 在方法执行前进行操作
String value = myAnnotation.value();
int count = myAnnotation.count();
System.out.println("MyAnnotation value: " + value);
System.out.println("MyAnnotation count: " + count);
// 执行被拦截的方法
Object result = joinPoint.proceed();
// 在方法执行后进行操作
System.out.println("Method result: " + result);
return result;
}
}
上面的代码定义了一个切面 MyAspect,它使用 @Around(“@annotation(MyAnnotation)”) 表达式来捕获带有 @MyAnnotation 注解的方法,并在方法执行前后进行操作。在方法执行前,通过 methodSignature.getMethod().getAnnotation(MyAnnotation.class) 方法获取了 @MyAnnotation 注解的实例,并从中读取了注解成员变量的值。在方法执行后,将方法的返回值输出到控制台。
使用自定义注解 @MyAnnotation 来标记方法,例如:
@MyAnnotation(value = "test", count = 5)
public String myMethod() {
return "result";
}
上述代码中,myMethod 方法被 @MyAnnotation 注解标记,并指定了 value 成员变量的值为 “test”,count 成员变量的值为 5。当 myMethod 方法被调用时,MyAspect 切面会对它进行拦截,并在控制台输出如下信息:
MyAnnotation value: test
MyAnnotation count: 5
Method result: result
这个例子演示了如何使用 @annotation 捕获自定义注解类型的方法,并在方法执行前后进行拦截和操作。
三、不用aop。
用aop的话使用@annotation注解,不用@annotation就需要通过反射来自定义注解逻辑,下面是一个不使用 AOP 的例子:
@MyAnnotation(value = "test", count = 5)
public class MyClass {
public void myMethod() {
// 方法体
}
}
myMain类
public class MyMain {
public static void main(String[] args) throws NoSuchMethodException {
MyClass myClass = new MyClass();
Method myMethod = myClass.getClass().getMethod("myMethod");
MyAnnotation myAnnotation = myMethod.getAnnotation(MyAnnotation.class);
// 进行自定义的业务逻辑操作
String value = myAnnotation.value();
int count = myAnnotation.count();
System.out.println("MyAnnotation value: " + value);
System.out.println("MyAnnotation count: " + count);
}
}
上面的代码定义了一个带有 @MyAnnotation 注解的 MyClass 类,并在其中定义了一个不带参数的 myMethod 方法。在 MyMain 类中,使用反射来获取 myMethod 方法,并通过 myMethod.getAnnotation(MyAnnotation.class) 方法获取到 @MyAnnotation 注解的实例。然后,根据注解成员变量的值进行自定义的业务逻辑操作。
当程序运行时,控制台输出如下信息:
MyAnnotation value: test
MyAnnotation count: 5
这个例子演示了如何在不使用 AOP 的情况下,使用反射来获取被注解的方法或类,并根据注解的成员变量进行自定义的业务逻辑操作。