Fork me on GitHub
返回顶部
跳到底部

注解

从JDK5开始,Java增加了对元数据的支持,也就是注解(Annotation),注解就是在代码里的特殊标记,这些标记可以在编译、类加载、运行时被读取,并执行相应的处理。通过注解,程序开发人员可以在不改变原有逻辑的情况下,在源代码中嵌入一些补充信息。

5个基本注解

  • @Override
  • @Depricated:该注解的方法说明已过时,不建议在使用。
  • @SafeVarargs:
  • @FunctionalInterface:Java1.8新增的注解,表明该接口是函数式接口,可以用Lambda表达式。
  • @SuppressWarnings:

上面@SafeVarargs注解是最难理解的了,用的也少,下次遇到,再做详细说明。

自定义注解

简要说明

注解也是一种特殊的Java类,其定义方式如下:

public @interface A{
    String name();
}

即以接口的形式定义注解,以抽象方法定义注解的属性,方法返回值的类型就注解是属性值的类型。

然后我们就可以这样使用自己定义的注解:

@A(name="sqmax")
class B{

    @A(name="sqmax")
    String a;

    @A(name="sqmax")
    void doSth() {

    }
}

仿照注解源码,些自己的注解

下面是我们经常使用的@Override和注解的源代码:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}

我们也可以自己定义注解。
比如我们可以自定义一个数据库配置类注解。

/**
 * Created by SqMax on 2018/5/15.
 */
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@interface JDBCConfig {
    String ip();
    int port() default 3306;
    String database();
    String encoding();
    String userName();
    String passWord();
}

然后我们可以使用这个注解,以注解的方式获取数据库连接信息:

@JDBCConfig(ip="127.0.0.1",database = "dbgirl",encoding = "utf-8", userName = "root",passWord = "123456")
public class DBUtil {
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException{
        JDBCConfig config=DBUtil.class.getAnnotation(JDBCConfig.class);
        String ip=config.ip();
        int port=config.port();
        String database=config.database();
        String encoding=config.encoding();
        String userName=config.userName();
        String passWord=config.passWord();

        String url=String.format("jdbc:mysql://%s:%d/%s?characterEncoding=%s",ip,port,database,encoding);
        System.out.println(url);
        return DriverManager.getConnection(url,userName,passWord);
    }

    public static void main(String[] args) throws SQLException{
        Connection connection=getConnection();
        System.out.println(connection.isClosed());
    }
}

运行结果如下:

jdbc:mysql://127.0.0.1:3306/dbgirl?characterEncoding=utf-8
false

可以看到已成功连接到数据库。

元注解

重新看一下上面我们自定义的@JDBCConfig注解:

@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@interface JDBCConfig {
    String ip();
    int port() default 3306;
    String database();
    String encoding();
    String userName();
    String passWord();
}

它的上面还有一些注解:@Target,@Retention,@Documented。
这些就是元注解。元注解就是可以用来注解自定义注解的注解。
主要有这么几种:

  • @Target
  • @Retention
  • @Inherited
  • @Document
  • @Repeatable(java1.8新增)

下面一一讲解:

@Target

@Target 表示这个注解能放在什么位置上,是只能放在类上?还是既可以放在方法上,又可以放在属性上。
从Target的源代码可以看到,java在定义Target注解时用又用@Target注解了。
源码如下:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
    ElementType[] value();
}

该注解有一个value属性,值是一个ElementType数组。其中ElementType是一个枚举类,源码如下:

public enum ElementType {
    /** Class, interface (including annotation type), or enum declaration */
    TYPE,

    /** Field declaration (includes enum constants) */
    FIELD,

    /** Method declaration */
    METHOD,

    /** Parameter declaration */
    PARAMETER,

    /** Constructor declaration */
    CONSTRUCTOR,

    /** Local variable declaration */
    LOCAL_VARIABLE,

    /** Annotation type declaration */
    ANNOTATION_TYPE,

    /** Package declaration */
    PACKAGE
}

这些枚举实例分别表示@Target注解的注解可以用在哪些地方,注释已经写的很清楚。
再看我们自定义的@JDBCConfig注解,可以看到该注解上的@Target,参数为METHOD、TYPE,表明@JDBCConfig注解只能用于方法、类或接口上。

@Retention

表示注解存在的生命周期。还是看其源码:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
    RetentionPolicy value();
}

可以看到该注解有一个value参数,参数的值是RetentionPolicy,它也是一个枚举类,源码如下:

public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     */
    SOURCE,

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     */
    CLASS,

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     *
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}

value的值为SOURCE,说明该注解只保留在源代码上,编译后该注解就会被去除。
为CLASS表明注解在java文件编程成.class文件后,依然存在,但是运行起来后就没了。
为RUNTIME表明注解在运行起来之后依然存在,程序可以通过反射获取这些信息,自定义注解@JDBCConfig就是这样。

@Inherited

@Inherited表示该注解具有继承性。下面自定义注解@Inheritable被元注解@Inherited修饰,当用该注解修饰Base类时,那么其子类InheritableTest也会得到这个注解,下面输出结果为true。

@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Inheritable {

}
@Inheritable
class Base{

}
public class InheritableTest extends Base{
    public static void main(String[] args) {
        //输出true
        System.out.println(InheritableTest.class
                .isAnnotationPresent(Inheritable.class));
    }
}

上面输出结果为true。虽然InheritableTest上面没有注解,但因为他继承自Base,Base又被一个可被继承的注解@Inheritable注释,所以InheritableTest也能获取到这个注解。

@Document

@Document用于指定被该元Annotation修饰的Annotation类将被javadoc工具提取成文档,如果定义Annotation类时使用@Document修饰,则所有使用该Annotation修饰的程序元素的API文档中将会包含该Annotation说明。
我觉得这个不常用,就不再说明,以后遇到在补充。

@Repeatable(java1.8新增)

在java8之前,同一个程序元素前最多只能使用一个相同类型的Annotation;如果需要在同一个元素前使用多个相同类型的Annotation,则必须使用Annotation“容器”。例如Structs2开发中,有时需要在Action类上使用@Result注解。在java8以前只能写成如下形式。

@Results({@Result(name="failure",location="fail.jsp"),@Result(name="success",location="success.jsp")}
public Action FooAction{..... }

而不能写成如下形式:

@Result(name="failure",location="fail.jsp")
@Result(name="success",location="success.jsp")
public Action FooAction{..... }

现在我们自己写一个可重复的注解。
首先自定义的注解要用@Repeatable修饰@MultiAnot,该注解的值是一个“容器”注解@MultiAnots,这个容器注解的值就是自定义的可重复注解。如下:

@Repeatable(value = MultiAnots.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface MultiAnot{
    String name() default "SunQ";
    int age();
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface MultiAnots {
    MultiAnot[] value();
}
//@MultiAnots({@MultiAnot(age=24),@MultiAnot(name = "wang",age=22)})
@MultiAnot(age=24)
@MultiAnot(name = "zhang",age=23)
public class MultiAnotTest {

    public static void main(String[] args) {
        Class<MultiAnotTest> clas=MultiAnotTest.class;

        MultiAnot[] anots=clas.getAnnotationsByType(MultiAnot.class);
        for (MultiAnot multiAnot:anots){
            System.out.println(multiAnot.name()+"--->"+multiAnot.age());
        }

        MultiAnot anot=clas.getDeclaredAnnotation(MultiAnot.class);
        System.out.println(anot);//输出null

        MultiAnots container = clas.getDeclaredAnnotation(MultiAnots.class);
        System.out.println(container);
    }
}

上面程序输出为:

SunQ--->24
zhang--->23
null
@top.sqmax.chapter14.MultiAnots(value=[@top.sqmax.chapter14.MultiAnot(name=SunQ, age=24), @top.sqmax.chapter14.MultiAnot(name=zhang, age=23)])

从上面输出可以看到getAnnotationsByType可以获取重复注解,而getDeclaredAnnotation不能;还有虽然上面没有用到@MultiAnots注解,getDeclaredAnnotation仍然可以获取到@MultiAnots注解,它会把两个@MultiAnot注解当作一个数组作为@MultiAnots注解的值。

省略属性名

当注解方法名为value时,使用注解时,value属性名可以省略。
上面的很多注解的使用都省略了value这个属性名,注意属性名为其他时,不能省略。
如:

@Repeatable(value = MultiAnots.class)
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = ElementType.TYPE)
@interface MultiAnot{
    String name() default "SunQ";
    int age();
}

可以写成:

@Repeatable(MultiAnots.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface MultiAnot{
    String name() default "SunQ";
    int age();
}
posted @ 2018-05-15 22:13  sqmax  阅读(141)  评论(0编辑  收藏  举报