服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - JAVA annotation入门基础

JAVA annotation入门基础

2019-10-11 11:46java教程网 JAVA教程

以下是JAVA annotation入门基础,新手朋友们可以过来参考下。希望对你有所帮助

一. 最常见的annotation
•@Override:用在方法之上,用来告诉别人这一个方法是改写父类的
•@Deprecated:建议别人不要使用旧的API的时候用的,编译的时候会用产生警告信息,可以设定在程序里的所有的元素上. 
•@SuppressWarnings:暂时把一些警告信息消息关闭
•@Entity:表示该类是可持久化的类

二. 设计一个自己的Annotation
先看代码再讲话
1. 只有一个参数的Annotation实现

复制代码代码如下:

package chb.test.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation1 {
        String value();
}


2. 有两个参数的Annotation实现

复制代码代码如下:

package chb.test.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation2 {
        String description();
        boolean isAnnotation();
}


3. Annotation实验类

复制代码代码如下:

package chb.test.annotation;
@MyAnnotation1("this is annotation1")
public class AnnotationDemo {
        @MyAnnotation2(description="this is annotation2",isAnnotation=true)
        public void sayHello(){
                System.out.println("hello world!");
        }
}


4.Annotation测试说明类

复制代码代码如下:

package chb.test.annotation;
import java.lang.reflect.Method;
import org.junit.Test;
public class TestAnnotation {
        @Test
        public void test() throws ClassNotFoundException, SecurityException, NoSuchMethodException{
                Class<?> cls = Class.forName("chb.test.annotation.AnnotationDemo");
                boolean flag = cls.isAnnotationPresent(MyAnnotation1.class);
                if(flag){
                        System.out.println("判断类是annotation");
                        MyAnnotation1 annotation1 = cls.getAnnotation(MyAnnotation1.class);
                        System.out.println(annotation1.value());
                }

                Method method = cls.getMethod("sayHello");
                flag = method.isAnnotationPresent(MyAnnotation2.class) ;
                if(flag){
                        System.out.println("判断方法也是annotation");
                        MyAnnotation2 annotation2 = method.getAnnotation(MyAnnotation2.class);
                        System.out.println(annotation2.description()+"/t"+annotation2.isAnnotation());
                }
        }

}


实验结果,控制台打出如下信息:

判断类是annotation
this is annotation1
判断方法也是annotation
this is annotation2     true

三.简介及说明
1. MyAnnotation1中的@Target(ElementType.TYPE)

@Target里面的ElementType是用来指定Annotation类型可以用在哪些元素上的.例如:
TYPE(类型)、FIELD(属性)、METHOD(方法)、PARAMETER(参数)、CONSTRUCTOR(构造函数)、LOCAL_VARIABLE(局部变量),、PACKAGE(包),其中的TYPE(类型)是指可以用在Class,Interface,Enum和Annotation类型上。

 

2. MyAnnotation1中的@Retention(RetentionPolicy.RUNTIME)

RetentionPolicy 共有三种策略,分别为:
•SOURCE:这个Annotation类型的信息只会保留在程序源码里,源码如果经过了编译之后,Annotation的数据就会消失,并不会保留在编译好的.class文件里面

•CLASS:这个Annotation类型的信息保留在程序源码里,同时也会保留在编译好的.class文件里面,在执行的时候,并不会把这些信息加载到JVM中。注:默认策略为CLASS类型

•RUNTIME:表示在源码、编译好的.class文件中保留信息,在执行的时候会把这一些信息加载到JVM中去的

3. MyAnnotation1中的@Documented
目的就是将这一Annotation的信息显示在JAVA API文档上,如果没有增加@Documented的话,JAVA API文档上不会显示相关annotation信息

4. MyAnnotation1中的@interface
关键字,表示该类为Annotation定义

5. MyAnnotation1中的 String value();
表示有一个成员参数,名字为value,访问权为默认(default)修饰符,注意以下两点:
•访问权只能用public和默认(default)修饰
•参数成员只能用基本类型byte,short,char,int,long,float,double,boolean八种基本数据类型和String,Enum,Class,annotations等数据类型,以及这一些类型的数组

6.AnnotationDemo中的@MyAnnotation1("this is annotation1")
因为MyAnnotation1只有一个参数,因此可以直接在括号中写上value值。注:如果Annotation只有一个参数,则建议最好将该参数名称定义为value

7.TestAnnotation中的cls.isAnnotationPresent(MyAnnotation1.class)
判断该类是否使用了MyAnnotation1的注释

8. TestAnnotation中的MyAnnotation1 annotation1 = cls.getAnnotation(MyAnnotation1.class)
返回该类针对MyAnnotation1的注释

9. TestAnnotation中的method.isAnnotationPresent(MyAnnotation2.class) 
判断该方法是否使用了MyAnnotation2的注释

延伸 · 阅读

精彩推荐
  • JAVA教程log4j的使用详细解析

    log4j的使用详细解析

    最近在整理公司产品的日志输出规范,涉及log4j的使用介绍,就简单整理了一下。需要的朋友可以过来参考参考 ...

    java之家4112019-10-11
  • JAVA教程浅析Java Mail无法解析带分号的收件人列表的问题

    浅析Java Mail无法解析带分号的收件人列表的问题

    JAVA MAIL严格按照RFC 822规范进行操作,没有对分号做处理。大多数邮件服务器都是严格遵循RFC 822规范的 ...

    java教程网4212019-10-11
  • JAVA教程Scala入门之List使用详解

    Scala入门之List使用详解

    这篇文章主要介绍了Scala入门之List使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小...

    小破孩2922019-07-08
  • JAVA教程如何使用Java生成具有安全哈希的QR码

    如何使用Java生成具有安全哈希的QR码

    这篇文章主要介绍了如何使用Java生成具有安全哈希的QR码,这是关于如何在Java中使用salt生成QR代码和安全散列字符串的分步教程。,需要的朋友可以参考下...

    Java_苏先生5012019-06-29
  • JAVA教程Retrofit+RxJava实现带进度条的文件下载

    Retrofit+RxJava实现带进度条的文件下载

    这篇文章主要为大家详细介绍了Retrofit+RxJava实现带进度条的文件下载,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    yw_5_241472019-06-25
  • JAVA教程zookeeper服务优化的一些建议

    zookeeper服务优化的一些建议

    今天小编就为大家分享一篇关于zookeeper服务优化的一些建议,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来...

    Scub3452019-06-20
  • JAVA教程面试中遇到的java逃逸分析问题

    面试中遇到的java逃逸分析问题

    这篇文章主要介绍了面试中遇到的java逃逸分析问题,逃逸分析(Escape Analysis)简单来讲就是,Java Hotspot 虚拟机可以分析新创建对象的使用范围,并决定是否...

    Java技术栈2982019-06-28
  • JAVA教程Java截取中英文混合字符串的方法

    Java截取中英文混合字符串的方法

    这篇文章主要为大家详细介绍了Java截取中英文混合字符串的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    余-雷3332019-06-30