Java自定义注解的使用

什么是注解?html

#======================================================================================================================
  Annotation(注解)是一个接口,程序能够经过反射来获取指定程序元素的Annotation对象,而后经过Annotation对象来获取注解里面的元数据。

 Annotation能够用于建立文档,跟踪代码中的依赖性,执行编译时的检查。Annotation就像修饰符同样被使用,可用于包,类型,构造方法,方法,成员变量,
 参数,本地变量的申明中。这些信息会被存储在Annotation的“name=value”结构对中
#
======================================================================================================================

什么是metadata元数据?java

#==========================================================================================================================
  元数据,即“描述数据的数据”的意思,元数据一词从“metadata”翻译过来。
 
 Annotation的行为很是相似于public,final这样的修饰符。

 Annotation类型定义了Annotation的名字,类型,成员默认值,一个Annotation能够说是一个特殊的Java接口,它的成员变量是受限制的,当咱们经过Java反射
 API访问注解的时候,返回值将是一个实现了该Annotation类型接口的对象,经过这个对象咱们能方便的访问到其余的Annotation成员。            
#
============================================================================================================================

注解的分类?ide

#==========================================================================================================================

 JDK内置系统的注解:@Override,@Deprecated

 元注解:@Target,@Retention,@Document,@Inhrited,


 自定义注解:(元注解的做用就是负责注解其余注解) #
==========================================================================================================================

注解是如何使用? spa

#==========================================================================================================================

 // 用于描述注解的使用范围  @Target(ElementType.Constructor)
 @Target(ElementType.Field)
 @Target(ElementType.Local_Variable)
 @Target(ElementType.Method)
 @Target(ElementType.Package)
 @Target(ElementType.Paramater)
 @Target(ElementType.Type)

// 用于描述注解的生命周期
 @Rentention(RetentionPolicy.source)
 @Rentention(RetentionPolicy.class)
 @Rentention(RetentionPolicy.runtime)
 
 @Document 和 @Inhrited 不经常使用,暂时不作介绍;
#
==========================================================================================================================

自定义注解(注解与反射机制.net

#==========================================================================================================================

 @Target(ElementType.Parameter)
 @Retention(RetentionPolicy.Runtime) 
 public @interface CacheKey{
    String value();
 }
 好比咱们在代码中定义上述注解,直观的咱们能看到注解使用在参数上,使用周期是运用在运行时使用,注解的参数名是value。阐述自定义注解:
 1. 使用@interface自定义注解的时候,自动继承了java.lang.annotation.Annotation接口,由编译程序自动完成其余细节
 2. 自定义注解,不能继承其余的注解或者接口。
 3. @interface用来申明一个注解,其中的每个方法其实是申明了一个配置参数,方法的名称就是参数的名称。
 4. 返回值类型仅仅包括:基本数据类型 + class + Enum + String
 5. 参数只能用public或default修饰,若是只有一个参数,最好把参数名称设置为value()。

 注解元素的默认值:注解元素必须有肯定的值,要么在定义注解的默认值中指定,要么在使用注解时指定,非基本类型的注解元素的值不可为null。所以,
 使用空字符串或0做为默认值是一种经常使用的作法。这个约束使得处理器很难表现一个元素的存在或缺失的状态,由于每一个注解的声明中,全部元素都存在,
 而且都具备相应的值,为了绕开这个约束,咱们只能定义一些特殊的值,例如空字符串或者负数,一次表示某个元素不存在,在定义注解时,这已经成为一个习惯用法
#==========================================================================================================================

关于注解的一些其余说明翻译

#==========================================================================================================================

public interface Annotation{

  boolean equals(Object obj);
  int hasCode();
  String toString();
  Class<? extends Annotation> annotationType();
} Java中的注解是一种继承自接口的java.lang.annotation.Annotation的接口。
#
==========================================================================================================================

注解在程序运行时是如何做处理的code

  java在java.lang.reflect下新增了AnnotatedElement接口,java处理注解的原理以下:htm

@Getter
@Setter
public class Person{

    @Name("狂刀")
    private String name;

    @Gender(gender=Gender.GenderType)
    private String gender;

    @Profile(id=1001, height=180, nativePlace="CN")
    private String profile;            
}

public class CustomUtils{
    
    public static void getInfo(Class<?> clazz){

        Sring name; String gender; String profile;
        Field[] fields = clazz.getDeclaredFields();
        for(Field field : fields){
          if(field.isAnnotationPresent(Name.class)){
        Name arg0 = field.getAnnotation(Name.class);
          name = name + arg0.value();
     }   if(field.isAnnotationPresent(Gender.class)){
         Gender arg0 = field.getAnnotation(Gender.class);
         gender = gender + arg0.gender().toString();     
      }
       if(field.isAnnotationPresent(Profile.class)){
         Profile arg0 = field.getAnnotation(Profile.class);
         profile = profile + "id = " + arg0.id + ", height = " + arg0.height + ",nativePlace = " + arg0.nativePlace;
       } } } }

自定义注解 && Spring AOP的使用对象

#==========================================================================================================================

       一般使用场景:自定义注解 ==》Spring AOP中获取注解,并处理

@Around("publicMethods() && @annotation(timedAnnotation)")
public Object instrumentTimed(ProceedingJoinPoint pjp, TimingTimed timedAnnotation) throws Throwable {
  String name = name(pjp.getTarget().getClass(), StringUtils.hasLength(timedAnnotation.name()) blog

                      ? timedAnnotation.name() : pjp.getSignature().getName(), "timingTimer");
  Metric metric = registry.getMetric(name);
  if (metric == null) {
    RedAlertMetric redAlertMetric = new RedAlertMetric(new TimingTimer(), name,
                  timedAnnotation.measurement(), timedAnnotation.interval());
    metric = registry.register(name, redAlertMetric);
  }
  if (metric != null && metric instanceof RedAlertMetric) {
      Metric timer = ((RedAlertMetric) metric).getMetric();
    if (timer != null && timer instanceof TimingTimer) {
      TimingTimer.Context tc = ((TimingTimer) timer).time();
      try {
        return pjp.proceed();
      } finally {
        tc.stop();
      }
    } else {
      return pjp.proceed();
    }
  } else {
  return pjp.proceed();
  }
}



#==========================================================================================================================

 

 

 

附,参考文章:

[1] http://blog.csdn.net/u013045971/article/details/53433874 

[2] 自定义注解的使用Demo

相关文章
相关标签/搜索