注解其实就是一个接口,程序(注解控制器)通过反射来获取程序中的注解对象,然后通过该注解对象获得它的方法信息。

4种标准元注解

@Target 修饰对象的范围

@Retention定义被保存的时间 SOURCE(源文件保留)、CLASS(class保留)、RUNTIME(运行时保留)

@Documented 用于描述其他类型的注解被作为标注的程序成员的公共API,可以被javadoc文档化

@Inherited 阐述了某个被标注的类型是被继承的,即它的父类被注解时,它的子类也会被注解

注解处理器

用来读取注解的,没有它注解就等同于注释

注解编写

1.自定义注解编写

 1 package com.zhao.annotation;
 2 
 3 import java.lang.annotation.Documented;
 4 import java.lang.annotation.ElementType;
 5 import java.lang.annotation.Retention;
 6 import java.lang.annotation.RetentionPolicy;
 7 import java.lang.annotation.Target;
 8 
 9 /**
10  * 自定义一个注解
11  * @author 光头强
12  *
13  */
14 
15 
16 @Target(ElementType.FIELD)
17 @Retention(RetentionPolicy.RUNTIME)
18 @Documented
19 public @interface annotationDemo {
20     public int id() default -1;
21     public String name() default "我";
22     public String address() default "港湾";
23 
24 
25 }
View Code

相关文章:

  • 2021-09-23
  • 2021-06-06
  • 2022-12-23
  • 2022-12-23
  • 2021-11-15
  • 2021-11-17
猜你喜欢
  • 2022-01-06
  • 2021-11-12
  • 2021-06-21
  • 2021-06-05
  • 2021-11-24
  • 2021-08-14
  • 2022-02-05
相关资源
相似解决方案