`

java注解Annotation

 
阅读更多
java的注解其实很常见,如@Override标记重载,@SuppressWarnings("unused")用过Eclipse的应该都知道。其实注解的本身并不做任何的操作,就像接口一样。注解的定义也特有意思[public @interface 注解名]是不是看起来很像接口。
现在的很多框架都用到了注解,如spring、jpa个人认为注解会让让程序看起来生涩难懂,但也正应为这样,也体现了编程中隐藏实现过程的这一思想。
虽然注解本身不做什么,但注解所配套的解析器会帮你完成一些事情,如属性的填充,其实原理很简单就用到了反射机制。下面是一个实现SqlMapClient实例填充到Dao层中的实例中的实现。
public static Map<String, Object> beanMap = new HashMap<String, Object>();
	
	public static void parse()throws Exception{
		String packageName = "com.sjw.lx.dao";
		String packagePath = packageName.replace(".", "/");
		String path = MyAnnotationParse.class.getClassLoader().getResource(packagePath).getPath();
		File f = new File(path);
		for(String s : f.list()){
			String className = packageName.concat("."+s.replace(".class", ""));
			Object obj = Class.forName(className).newInstance();
			Method[] methods = Class.forName(className).getMethods();
			for(Method m : methods){
				if(m.getAnnotation(FillSqlMap.class)!=null&&m.getName().startsWith("set")){
					m.invoke(obj, SqlMapSingle.getSqlMap());
					beanMap.put(className, obj);
				}
			}
		}
	}

这是一个自定义扫描包中的类,将打有自定义注解的类通过反射创建类的实例并填充SqlMapClient的属性,放入到Map结构中提供服务层的调用。

java注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置的功能。
注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用。包含在 java.lang.annotation 包中。

1、元注解
元注解是指注解的注解。包括  @Retention @Target @Document @Inherited四种。

1.1、@Retention: 定义注解的保留策略
@Retention(RetentionPolicy.SOURCE)   //注解仅存在于源码中,在class字节码文件中不包含
@Retention(RetentionPolicy.CLASS)     // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,
@Retention(RetentionPolicy.RUNTIME)  // 注解会在class字节码文件中存在,在运行时可以通过反射获取到

1.2、@Target:定义注解的作用目标
其定义的源码为:
@Documented  
@Retention(RetentionPolicy.RUNTIME)  
@Target(ElementType.ANNOTATION_TYPE)  
public @interface Target {  
    ElementType[] value();  
}  
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
    ElementType[] value();
}

@Target(ElementType.TYPE)   //接口、类、枚举、注解
@Target(ElementType.FIELD) //字段、枚举的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法参数
@Target(ElementType.CONSTRUCTOR)  //构造函数
@Target(ElementType.LOCAL_VARIABLE)//局部变量
@Target(ElementType.ANNOTATION_TYPE)//注解
@Target(ElementType.PACKAGE) ///包    
由以上的源码可以知道,他的elementType 可以有多个,一个注解可以为类的,方法的,字段的等等

1.3、@Document:说明该注解将被包含在javadoc中

1.4、@Inherited:说明子类可以继承父类中的该注解

2、java 注解的自定义
      下面是自定义注解的一个例子
@Documented  
@Target({ElementType.TYPE,ElementType.METHOD})  
@Retention(RetentionPolicy.RUNTIME)  
public @interface Yts {  
   public enum YtsType{util,entity,service,model}  
     
   public YtsType classType() default YtsType.util;  
}  
   
@Documented
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Yts {
   public enum YtsType{util,entity,service,model}
   
   public YtsType classType() default YtsType.util;
}
@Documented  
@Retention(RetentionPolicy.RUNTIME)  
@Target(ElementType.METHOD)  
@Inherited  
public @interface HelloWorld {  
    public String name()default "";  
}  
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Inherited
public @interface HelloWorld {
    public String name()default "";
}
@Retention(RetentionPolicy.RUNTIME)

定义的这个注解是注解会在class字节码文件中存在,在运行时可以通过反射获取到。

@Target({ElementType.TYPE,ElementType.METHOD})

因此这个注解可以是类注解,也可以是方法的注解

这样一个注解就自定义好了,当然注解里面的成员可以为基本的数据类型,也可以为数据,Object等等

3 注解是定义好了,那么怎么来得到,解析注解呢?java的反射机制可以帮助,得到注解,代码如下:
public class ParseAnnotation {  
  
     public void parseMethod(Class clazz) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException, InstantiationException{  
  Object obj = clazz.getConstructor(new Class[]{}).newInstance(new Object[]{});  
    for(Method method : clazz.getDeclaredMethods()){  
        HelloWorld say = method.getAnnotation(HelloWorld.class);  
        String name = "";  
        if(say != null){  
           name = say.name();  
           method.invoke(obj, name);  
        }  
       Yts yts = (Yts)method.getAnnotation(Yts.class);  
       if(yts != null){  
          if(YtsType.util.equals(yts.classType())){  
          System.out.println("this is a util method");  
        }else{  
            System.out.println("this is a other method");  
            }  
        }  
      }  
    }  
    @SuppressWarnings("unchecked")  
    public void parseType(Class clazz) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{  
        Yts yts = (Yts) clazz.getAnnotation(Yts.class);  
        if(yts != null){  
            if(YtsType.util.equals(yts.classType())){  
                System.out.println("this is a util class");  
            }else{  
                System.out.println("this is a other class");  
            }  
        }  
    }  
      
}  
public class ParseAnnotation {

	 public void parseMethod(Class clazz) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException, InstantiationException{
  Object obj = clazz.getConstructor(new Class[]{}).newInstance(new Object[]{});
    for(Method method : clazz.getDeclaredMethods()){
        HelloWorld say = method.getAnnotation(HelloWorld.class);
        String name = "";
        if(say != null){
           name = say.name();
           method.invoke(obj, name);
        }
       Yts yts = (Yts)method.getAnnotation(Yts.class);
       if(yts != null){
          if(YtsType.util.equals(yts.classType())){
          System.out.println("this is a util method");
        }else{
            System.out.println("this is a other method");
            }
        }
      }
    }
	@SuppressWarnings("unchecked")
	public void parseType(Class clazz) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		Yts yts = (Yts) clazz.getAnnotation(Yts.class);
		if(yts != null){
			if(YtsType.util.equals(yts.classType())){
				System.out.println("this is a util class");
			}else{
				System.out.println("this is a other class");
			}
		}
	}
	
}

前一个方法是解析得到方法注解,后一个方法是得到类注解以下是测试方法类
@Yts(classType =YtsType.util)  
public class SayHell {  
  
    @HelloWorld(name = " 小明 ")  
    @Yts  
    public void sayHello(String name){  
        if(name == null || name.equals("")){  
            System.out.println("hello world!");  
        }else{  
            System.out.println(name + "say hello world!");  
        }  
    }  
}  
@Yts(classType =YtsType.util)
public class SayHell {

	@HelloWorld(name = " 小明 ")
	@Yts
	public void sayHello(String name){
		if(name == null || name.equals("")){
			System.out.println("hello world!");
		}else{
			System.out.println(name + "say hello world!");
		}
	}
}

public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException, InstantiationException {  
        ParseAnnotation parse = new ParseAnnotation();  
        parse.parseMethod(SayHell.class);  
        parse.parseType(SayHell.class);  
    }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics