对于java注解的认识

最近一直在看aop,apt,javassist之类的东西。看的自己是一脸懵逼,只能说,android世界太大,自己太渺小,知识稍微懂了那么一些,还有很多遗落在那边,还没有理解完全。待我哪天开个天眼,再拿出来讲讲。

这次,主要讲讲这其中的注解。通过注解,aop可以很方便的切入一段代码。


元注解

云注解的作用就是负责注解其他注解。java5.0的时候,定义了4个标准的meta-annotation类型,它们用来提供对其他注解的类型作说明。

1.@Target
2.@Retention
3.@Documented
4.@Inherited

@Target:   规定Annotation所修饰的对象范围。有以下几种
1.ElementType.CONSTRUCTOR ;  构造器声明
2.ElementType.FIELD:成员变量、对象、属性(包括enum实例)
3.ElementType.LOCAL_VARIABLE; 局部变量声明
4.ElementType.METHOD ; 方法声明
5.ElementType.PACKAGE; 包声明
6.ElementType.PARAMETER;参数声明
7.ElementType.TYPE; 类、接口(包括注解类型)或enum声明
示例:
@Target(ElementType.TYPE)
public   @interface Table{
public String value() default "";
}
@Target还支持填写数组,同时支持多种范围
@Target({ElementType.TYPE,ElementType.FIELD})
public @interface Test{}


@Retention:对Annotation的生命周期限制:某些Annotation仅出现在源代码中,而被编译器丢弃;而另一些却被编译在class文件中;编译在class文件中的Annotation可能会被虚拟机忽略,而另一些在class被装载时将被读取。
1.RetentionPolicy.SOUREC: 在源文件中有效
2.RetentionPolicy.CLASS; 在class文件中有效
3.RetentionPolicy.RUNTIME;在运行时有效

示例:
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Column{
public String name() default ""

}

@Documented: 用于描述其它类型的annotation应该被作为被标注的程序成员的公共API,因此可以被例如javadoc此类的工具文档化。Documented是一个标记注解,没有成员
示例
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Column{
public String name() default "";
}

@Inherited: 是一个标记注解,@Inherited阐述了某个被标注的类型是被继承的。如果一个使用了@Inherited修饰的annotation类型被用于一个class,则这个annotation将用于该class的子类。
示例:
@Inherited
public @interface Season{

public enum  Sea{Spring,Summer,Autumn,Winter};
Sea sea() default Sea.Spring;
}


当然了,我们也是可以自定义注解的。

在使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口。
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Description {
	public String value(); //只有一个参数的时候,必须为value
}
类的支持注解如下:
@Description("这是类上的注解")
public class Person {
	
	public String getName(){
		return "";
	}
}
方法的支持注解如下:
public class Coder extends Person{


	@Override
	@Description("这是方法上的注解")
	public String getName() {
		// TODO Auto-generated method stub
		return "Mark";
	}
}
@Inherited表示可以继承的
然后下面的这段代码就是注解的解析了,通过反射来拿到哪些注解
public static void main(String[] args) {
		// TODO Auto-generated method stub
		//1.使用类加载器加载类
		try {
			Class c=Class.forName("com.annotation.Coder");
			//2.找到类加载器上面的注解
			boolean isExist=c.isAnnotationPresent(Description.class);
			if(isExist){
				//3 拿到注解实例
				Description d=(Description) c.getAnnotation(Description.class);
				System.out.println(d.value());
			}
			//4.找到方法上的注解
			Method[] ms=c.getMethods();
			for(Method m:ms){
				boolean isMExist =m.isAnnotationPresent(Description.class);
				if(isMExist){
					Description d=m.getAnnotation(Description.class);
					System.out.println(d.value());
				}
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
ok~ 这就是完整的注解了,如有不懂,欢迎留言。
当然了,最大的同志交友网站,可以follow下我,时刻关注。https://github.com/zhairui   







  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值