JDK自带注解
@Override
@Deprecated
@SuppressWarnings
常见第三方注解
Spring:
@Autowired
@Service
@Repository
Mybatis:
@InsertPProvider
@UPdateProvider
@Options
元注解:给注解进行注解
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
自定义注解
eg:
@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { String value() default ""; }
eg:
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.SOURCE) @Documented public @interface MyAnnotation2 { String name(); int age() default -1; }
引用自定义注解:
@MyAnnotation2(name = "张三", age = 18) public class TestMyAnnotation { @MyAnnotation() @SuppressWarnings({"all"}) public static void fun() { } }
-
使用@interface关键字定义注解
-
成员以无参无异常方式声明(只有参数,参数后面会带个括号,但这并不意味着是方法,只是特殊的表示)
-
可以用default为成员指定一个默认值。
注意:
- 成员类型是受限的,合法的类型包括原始类型(int,double等)及String,class,Annotation,Enumeration。
- 如果注解只有一个成员,则成员名必须取名为value(),在使用时可以忽略成员名和赋值号(=).
- 注解类可以没有成员,没有成员的注解称为标识注解。