四十一,java中Annotation详解

1.Annotation简介

Annotation实际上表示一种注释的语法,java中最先的程序是提倡代码与配置相分离,而最新的理论又是将全部的配置直接写入到程序中去,那么若是想要完成这样的功能,则须要使用Annotation. html

JDK1.5以后的新特性:枚举,自动装箱拆箱,foreach,可变参数,静态导入,泛型. java

2.系统内建的Annotation

  • @Override ide

  • @Deprecated 学习

  • @SupressWarnings spa

2.1 @Override

@Override表示正确的覆写操做. code

示例: orm

父类代码 htm

public class Person {
    public String say(){
        return "人在说话。" ;
    }
}


子类代码 :
public class Studnt extends Person {
    @Override
    public String say(){
        return "学生在说话" ;
     }
}

@Override能够保证正确的覆写,若是一个方法声明了覆写,而实际上覆写有问题就会提示出错误. 对象


2.2 @Deprecated

@Deprecated表示不建议使用的操做. 继承

示例:

public class Info {
    @Deprecated
    public String getInfo(){
        return "hello" ;
    }
}
getInfo这个方法就是不建议使用的操做 ,在代码中会用中划线表示警告 ,可是不影响实际使用 .


2.3 @SupressWarnings

@SupressWarnings表示压制警告.

示例:

public class TestInfo {

	@SuppressWarnings("deprecation")
	public static void main(String[] args) {
		new Info().getInfo() ;
	}

}
注意的是 SupressWarnings能够压制多个警告 .

示例:

import java.io.Serializable;

@SuppressWarnings({ "serial", "deprecation" })
public class Person extends Info implements Serializable {

}


使用过程当中,能够明确表示出是为 SuppressWarnings中的value属性赋值.

import java.io.Serializable;

@SuppressWarnings(value = { "serial", "deprecation" })
public class Person extends Info implements Serializable {

}




3.自定义Annotation

定义Annotation的语法:

public @Annotation

示例:

public @interface MyAnnotation {

    public String key() ;
    public String value() ;
}
如今若是要使用此 Annotation,不在同一个包中须要导入 ,导入以后使用 @Annotation的访问语法 .

示例:

@MyAnnotation(key = "ARES", value = "19")
public class Info {
}

:能够为Annotation定义默认的属性.

public @interface MyAnnotation {
	public String key() default "ARES";

	public String value() default "19";
}


Annotation能够经过枚举类型制定范围 .

示例:

Color:

public enum Color {
	RED, GREEN, BLUE;
}
MyAnnotation:
public @interface MyAnnotation {
	public String key() default "ARES";
	public String value() default "19";
	public Color color() default Color.RED ;
}




4.RententionRententionPolicy

java.lang.annotation中定义了全部与之相关的操做,Rentention自己是一个 Annotation,其中的取值是经过RententionPolicy这个枚举类型制定的.RententionPolicy规定了一下三种范围:

  • 源码中起做用:public static final RetentionPolicy SOURCE

  • 编译后的class中起做用:public static final RetentionPolicy CLASS

  • 运行的时候起做用:public static final RetentionPolicy RUNTIME

若是一个Annotation要起做用,则必须使用RUNTIME范围.


5.反射与Annotation

一个Annotation要想起做用,则必须依靠反射机制,经过反射机制能够取得一个方法在Annotation上声明的所有内容.

取得所有的Annotation.

示例:

import java.lang.annotation.*;
@Retention(value=RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
	public String key() default "ARES";
	public String value();
}

Info:

package com.ares.demo;
public class Info {
	@Override
	@Deprecated
	@SuppressWarnings(value = "")
	@MyAnnotation(key = "ARES", value = "19")
	public String toString() {
		return "hello";
	}
}
以上三个 Annotation,只有 @Deprecated RUNTIME范围的 .因此运行时 ,只有运行时只有 @Deprecated 能够取到 .

示例:

package com.ares.demo;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class ClassAnnotationDemo {

	public static void main(String[] args) throws Exception {
		Class<?> cls = Class.forName("com.ares.demo.Info");
		Method toStringMethod = cls.getMethod("toString");
		Annotation ans[] = toStringMethod.getAnnotations();// 取得所有的Annotation
		for (int i = 0; i < ans.length; i++) {
			System.out.println(ans[i]) ;
		}
	}

}




6.自定义RUNTIMEAnnotation

示例:

package com.ares.demo12;

import java.lang.annotation.*;
@Documented
@Target(value = { ElementType.METHOD, ElementType.TYPE })
@Retention(value = RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
	public String key() default "ARES";

	public String value() default "ARES";
}

经过反射取得Annotation:

package com.ares.demo;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class ClassAnnotationDemo {

	public static void main(String[] args) throws Exception {
		Class<?> cls = Class.forName("com.ares.demo.Info");
		Method toStringMethod = cls.getMethod("toString");
		Annotation ans[] = toStringMethod.getAnnotations();// 取得所有的Annotation
		for (int i = 0; i < ans.length; i++) {
			if (toStringMethod.isAnnotationPresent(MyAnnotation.class)) {
				MyAnnotation my = null; // 声明Annotation的对象
				my = toStringMethod.getAnnotation(MyAnnotation.class) ;
				String key = my.key() ;
				String value = my.value() ;
				System.out.println(key + " --> " + value) ;
			}
		}
	} 
}

实际开发中不用过多关注这些底层的实现,程序中为其提供支持.



7.Annotation深刻

①自定义Annotation能够在程序的任意位置使用.

示例:

package com.ares.demo;
@MyAnnotation
public class Info {
	private String name ;
	@MyAnnotation
	public String toString() {
		return "hello" ;
	}
}

:实际上咱们能够为Annotation制定使用范围.

②设置Annotation的使用范围.(实际有8种范围,能够查看手册)

制定范围示例:

package com.ares.demo;

import java.lang.annotation.*;

@Target(value = { ElementType.METHOD, ElementType.TYPE })
@Retention(value = RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
	public String key() default "ARES";

	public String value() default "ARES";
}

Documented注释

注释格式:

package com.ares.demo;

import java.lang.annotation.*;
@Documented
@Target(value = { ElementType.METHOD, ElementType.TYPE })
@Retention(value = RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
	public String key() default "ARES";

	public String value() default "ARES";
}

在使用类中加入文档注释:

package com.ares.demo;
@MyAnnotation
public class Info {
	private String name ;
	/**
	 * 本方法是覆写Object类中的toString()方法
	 */
	@MyAnnotation
	public String toString() {
		return "hello" ;
	}
}

用文档注释的最大好处是能够导出doc文档.相似官方文档的html格式文档.



8.Inherited

表示此Annotation可否继续被子类继承下去,若是没有写上此注释,表示此Annotation根本就是没法被继承的.

示例:

package com.ares.demo;

import java.lang.annotation.*;
@Inherited
@Documented
@Target(value = { ElementType.METHOD, ElementType.TYPE })
@Retention(value = RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
	public String key() default "ARES";

	public String value() default "ARES";
}
此时表示能够被子类继承 .







20150529


JAVA学习笔记系列

--------------------------------------------

                    联系方式

--------------------------------------------

        Weibo: ARESXIONG

        E-Mail: aresxdy@gmail.com

------------------------------------------------
相关文章
相关标签/搜索