简介
注解是一种能被添加到java源代码中的元数据,方法、类、参数和包都可以用注解来修饰。注解可以看作是一种特殊的标记,可以用在方法、类、参数和包上,程序在编译或者运行时可以检测到这些标记而进行一些特殊的处理
元注解介绍
元注解的作用就是负责注解其他注解,Java5.0定义了4个标准的meta-annotation类型,它们被用来提供对其它 annotation类型作说明
- @Target
- @Retention
- @Documented
- @Inherited
对应的参数详情说明见末尾
修饰符:访问修饰符必须为public,不写默认为pubic
关键字:关键字为@interface
注解名称: 注解名称为自定义注解的名称
注解内容:注解中内容,对注解的描述
以下内容采用枚举方式进行注解
1.定义枚举
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| public enum newChannelEnum { BVC("BVC","bvc_timeout","BVC","BVC_config"), ACC("ACC","acc_timeout","ACC","ACC_config"); private String channel; private String timeout; private String name; private String interfaceName;
newChannelEnum(String channel, String timeout, String name, String interfaceName) { this.channel = channel; this.timeout = timeout; this.name = name; this.interfaceName = interfaceName; }
public String getInterfaceName() { return interfaceName; }
public String getChannel() { return channel; }
public String getTimeout() { return timeout; }
public void setTimeout(String timeout) { this.timeout = timeout; }
public String getName() { return name; } }
|
2.定义注解
1 2 3 4 5 6
| @Target({ElementType.METHOD,ElementType.FIELD,ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface InterfaceTagConfig { newChannelEnum method(); }
|
3.定义注解工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| public class InterfaceTarHelper { private static final InterfaceDefault CLASS_LOAD_PATH = new InterfaceDefault(); private static final Map<String,Method> METHOD_MAP = new ConcurrentHashMap<String,Method>(); private Method method; static { Method[] methods = CLASS_LOAD_PATH.getClass().getMethods(); for (Method method : methods) { InterfaceTagConfig annotation = method.getAnnotation(InterfaceTagConfig.class); if(annotation != null){ METHOD_MAP.put(String.valueOf(annotation.method()),method); } } } public static InterfaceTarHelper getInstance(newChannelEnum methodEnum){ InterfaceTarHelper helper = new InterfaceTarHelper(); helper.method = METHOD_MAP.get(String.valueOf(methodEnum)); return helper; }
public Object getSearchFlightDetail(Object... args) { try { return method.invoke(CLASS_LOAD_PATH, args); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return null; } }
|
4.使用注解
1 2 3 4 5 6 7
| public class NewSearch { @InterfaceTarget(method = InterfaceTagConfig.BVC) public static void BVCValidation(InterfaceParam param){……} @InterfaceTarget(method = InterfaceTagConfig.ACC) public static void ACCValidation(InterfaceParam param){……} }
|
5.调用方法
1 2 3 4 5 6 7 8
| public class SearchUtils { public static void getData_new(String channel, InterfaceParam param) throws Exception { newChannelEnum enums = newChannelEnum.getEnums(channel); InterfaceHelper.getInstance(enums).getSearchFlightDetail(param); } }
|
6.附释
@Target
表明该注解可以应用的java元素类型
Target类型 |
描述 |
ElementType.TYPE |
应用于类、接口(包括注解类型)、枚举 |
ElementType.FIELD |
应用于属性(包括枚举中的常量) |
ElementType.METHOD |
应用于方法 |
ElementType.PARAMETER |
应用于方法的形参 |
ElementType.CONSTRUCTOR |
应用于构造函数 |
ElementType.LOCAL_VARIABLE |
应用于局部变量 |
ElementType.ANNOTATION_TYPE |
应用于注解类型 |
ElementType.PACKAGE |
应用于包 |
ElementType.TYPE_PARAMETER |
1.8版本新增,应用于类型变量 |
ElementType.TYPE_USE |
1.8版本新增,应用于任何使用类型的语句中(例如声明语句、泛型和强制转换语句中的类型) |
@Retention
表明该注解的生命周期
生命周期类型 |
描述 |
RetentionPolicy.SOURCE |
编译时被丢弃,不包含在类文件中 |
RetentionPolicy.CLASS |
JVM加载时被丢弃,包含在类文件中,默认值 |
RetentionPolicy.RUNTIME |
由JVM 加载,包含在类文件中,在运行时可以被获取到 |
@Document
表明该注解标记的元素可以被Javadoc 或类似的工具文档化
@Inherited
表明使用了@Inherited注解的注解,所标记的类的子类也会拥有这个注解