springboot创建注解类
时间: 2023-10-14 10:19:14 浏览: 35
要创建一个注解类,可以使用Java中的元注解来注解该类,示例代码如下:
```
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() default "";
}
```
在这个示例中,我们创建了一个名为MyAnnotation的注解类,使用了@Target和@Retention两个元注解来指定注解的使用范围和生命周期。@Target注解指定了该注解只能用来注解类(@Target(ElementType.TYPE)),@Retention注解指定了该注解会在运行时保留(@Retention(RetentionPolicy.RUNTIME))。
在注解类中,可以添加一些属性,使用方式类似于方法定义,例如上述示例中的value()方法。这些属性可以在使用注解时进行赋值,例如:
```
@MyAnnotation(value = "test")
public class MyClass {
// class body
}
```
这样,我们就可以在MyClass类上使用MyAnnotation注解,并且为其value属性赋值为"test"。