Java注解是一个很重要的知识点,把握好Java注解有利于学习Java开发框架底层实现。@mikechen
Java注解定义
Java注解又称Java标注,是在 JDK5 时引入的新个性,注解(也被称为元数据)。
Java注解它提供了一种平安的相似正文的机制,用来将任何的信息或元数据(metadata)与程序元素(类、办法、成员变量等)进行关联。
Java注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和应用,起到阐明、配置的性能。
Java注解利用
1.生成文档这是最常见的,也是java 最早提供的注解;
2.在编译时进行格局查看,如@Override放在办法前,如果你这个办法并不是笼罩了超类办法,则编译时就能查看出;
3.跟踪代码依赖性,实现代替配置文件性能,比拟常见的是spring 2.5 开始的基于注解配置,作用就是缩小配置;
4.在反射的 Class, Method, Field 等函数中,有许多于 Annotation 相干的接口,能够在反射中解析并应用 Annotation。
Java注解分类
1、Java自带的规范注解
包含@Override、@Deprecated、@SuppressWarnings等,应用这些注解后编译器就会进行查看。
2、元注解
元注解是用于定义注解的注解,包含@Retention、@Target、@Inherited、@Documented、@Repeatable 等。\
元注解也是Java自带的规范注解,只不过用于润饰注解,比拟非凡。
3、自定义注解
用户能够依据本人的需要定义注解。
Java规范注解
JDK 中内置了以下注解:
1.@Override
如果试图应用 @Override 标记一个实际上并没有覆写父类的办法时,java 编译器会告警。
class Parent { public void test() { }}class Child extends Parent { /** * 放开上面的正文,编译时会告警 */ /* @Override public void test() { }*/}
2.Deprecated
@Deprecated 用于表明被润饰的类或类成员、类办法曾经废除、过期,不倡议应用。@Deprecatedclass TestClass { // do something}
3.@SuppressWarnings
@SuppressWarnings 用于敞开对类、办法、成员编译时产生的特定正告。
1)克制单类型的正告
@SuppressWarnings("unchecked") public void addItems(String item){ @SuppressWarnings("rawtypes") List items = new ArrayList(); items.add(item); }
2)克制多类型的正告
@SuppressWarnings(value={"unchecked", "rawtypes"}) public void addItems(String item){ List items = new ArrayList(); items.add(item); }
3)克制所有类型的正告
@SuppressWarnings("all") public void addItems(String item){ List items = new ArrayList(); items.add(item); }
@SuppressWarnings 注解的常见参数值的简略阐明:
4.@FunctionalInterface
@FunctionalInterface 用于批示被润饰的接口是函数式接口,在 JDK8 引入。
@FunctionalInterfacepublic interface UserService { void getUser(Long userId); // 默认办法,能够用多个默认办法 public default void setUser() { } // 静态方法 public static void saveUser() { } // 笼罩Object中的equals办法 public boolean equals(Object obj);}
函数式接口(Functional Interface)就是一个有且仅有一个形象办法,然而能够有多个非形象办法的接口。
Java元注解
元注解是java API提供的,是用于润饰注解的注解,通常用在注解的定义上:
1.@Retention
@ Retention用来定义该注解在哪一个级别可用,在源代码中(SOURCE)、类文件中(CLASS)或者运行时(RUNTIME)。
@Retention 源码:
@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.ANNOTATION_TYPE)public @interface Retention { RetentionPolicy value();}public enum RetentionPolicy { //此注解类型的信息只会记录在源文件中,编译时将被编译器抛弃,也就是说 //不会保留在编译好的类信息中 SOURCE, //编译器将注解记录在类文件中,但不会加载到JVM中。如果一个注解申明没指定范畴,则零碎 //默认值就是Class CLASS, //注解信息会保留在源文件、类文件中,在执行的时也加载到Java的JVM中,因而能够反射性的读取。 RUNTIME}
RetentionPolicy 是一个枚举类型,它定义了被 @Retention 润饰的注解所反对的保留级别:\
@Target(ElementType.METHOD)@Retention(RetentionPolicy.SOURCE) //注解信息只能在源文件中呈现public @interface Override {}@Documented@Retention(RetentionPolicy.RUNTIME) //注解信息在执行时呈现@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})public @interface Deprecated {}@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})@Retention(RetentionPolicy.SOURCE) //注解信息在源文件中呈现public @interface SuppressWarnings { String[] value();}
2.@Documented
@Documented:生成文档信息的时候保留注解,对类作辅助阐明
@Documented 示例
@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Column { public String name() default "fieldName"; public String setFuncName() default "setField"; public String getFuncName() default "getField"; public boolean defaultDBValue() default false;}
3.@Target
@Target:用于形容注解的应用范畴(即:被形容的注解能够用在什么中央)
@Target源码:
@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.ANNOTATION_TYPE)public @interface Target { ElementType[] value();}
ElementType 是一个枚举类型,它定义了被 @Target 润饰的注解能够利用的范畴:\
4.@Inherited
@Inherited:阐明子类能够继承父类中的该注解
示意主动继承注解类型。 如果注解类型申明中存在 @Inherited 元注解,则注解所润饰类的所有子类都将会继承此注解。
@Inheritedpublic @interface Greeting { public enum FontColor{ BULE,RED,GREEN}; String name(); FontColor fontColor() default FontColor.GREEN;}
5.@Repeatable
@Repeatable 示意注解能够重复使用。
当咱们须要重复使用某个注解时,心愿利用雷同的注解来体现所有的模式时,咱们能够借助@Repeatable注解。\
以 Spring @Scheduled 为例:
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Schedules { Scheduled[] value();}@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Repeatable(Schedules.class)public @interface Scheduled { // ...}
自定义注解
当咱们了解了内置注解, 元注解和获取注解的反射接口后,咱们便能够开始自定义注解了。
创立自定义注解和创立一个接口类似,然而注解的interface关键字须要以@符号结尾,咱们能够为注解申明办法。
自定义注解格局:
// 元注解public @interface 注解名称{ // 属性列表}
咱们先来看看注解的例子:
1.创立自定义注解
/** * 自定义注解例子 * * @author mikechen */@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)@Inheritedpublic @interface HelloAnnotation { String value();}
2.应用自定义注解
/** * 应用自定义注解 * * @author mikechen */public class HelloAnnotationClient { @HelloAnnotation(value="Simple custom Annotation example") public void sayHello(){ System.out.println("Inside sayHello method.."); }}
3.测试自定义注解
/** * 自定义注解测试 * * @author mikechen */public class HelloAnnotationTest { public static void main(String[] args) throws Exception { HelloAnnotationClient helloAnnotationClient=new HelloAnnotationClient(); Method method=helloAnnotationClient.getClass().getMethod("sayHello"); if(method.isAnnotationPresent(HelloAnnotation.class)){ HelloAnnotation helloAnnotation=method.getAnnotation(HelloAnnotation.class); //Get value of custom annotation System.out.println("Value : "+helloAnnotation.value()); //Invoke sayHello method method.invoke(helloAnnotationClient); } }}
以上
作者简介
陈睿|mikechen,10年+大厂架构教训,《BAT架构技术500期》系列文章作者,分享十余年BAT架构教训以及面试心得!
浏览mikechen的互联网架构更多技术文章合集
Java并发|JVM|MySQL|Spring|Redis|分布式|高并发|架构师
关注「mikechen 的互联网架构」公众号,回复 【架构】 支付我原创的《300 期 + BAT 架构技术系列与 1000 + 大厂面试题答案》