再读完Java自定义注解(一)我们可以通过代码来实现我们自己的自定义注解

如何自定义注解

EventBean类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.code.note.annotation;

import lombok.Data;

@Data
public class EventBean {

@EventName("coding now...")
private String name;

@EventType(eventType = EventType.Type.MEETING)
private String type;

@User(id = 1, name = "testName", email = "15090552277@163.com")
private String user;
}

第二步,添加参数、默认值:

1
2
3
4
5
public @interface Report {
int type() default 0;
String level() default "info";
String value() default "";
}

第三步,用元注解配置注解:

1
2
3
4
5
6
7
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Report {
int type() default 0;
String level() default "info";
String value() default "";
}

其中,必须设置@Target@Retention@Retention一般设置为RUNTIME,因为我们自定义的注解通常要求在运行期读取。一般情况下,不必写@Inherited@Repeatable