使用过ssh框架的人一定也使用过注解,尤其是在spring框架中,注解可谓是spring容器和AOP编程的重要环节。注解就是用于修饰类、全局变量、方法、参数或局部变量的接口,java中规定,注解的使用不允许影响其修饰类的存在,也就是说如果将一个类的注解全部删除,这个类也能够编译通过。java中,注解的应用主要有四个方面:类注解,全局变量注解,方法注解,参数注解。其他的还有诸如包注解和局部变量注解这里暂不作讨论。首先我们定义一个类注解如下:
package annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface TypeAnnotation { String say() default "this is a type annotation"; }
在java中有四个元注解,分别是@RetentIon、@Target、@Inherited和@Document,@Retention指定的是注解的生命周期,其value参数的值有三个:RUNTIME、SOURCE、CLASS。RUNTIME表示当前修饰的注解是在虚拟机运行时实时加载的,也就是虚拟机中要用到该注解一次就加载一次;CLASS表示该注解在类的源文件和编译后的class文件中都存在,而在类加载器加载到虚拟机中时就会消失,SOURCE表示该注解只在源文件中存在,当编译成class文件后就不存在了。@Target指定的是当前注解可修饰的类型,可value参数可取如下几个值:Type、FIELD、METHOD、PARAMETER、PACKAGE、CONSTRUCTOR、LOCAL_VARIABLE、ANNOTATION_TYPE等。其中最主要的应用有TYPE、FIELD、METHOD、PARAMETER,其分别表示类注解,全局变量注解,方法注解,参数注解,其余几个则表示包注解,构造器注解,局部变量注解,用于注解的注解等。
注解的声明同接口的声明类似,不过要在interface关键字前加@修饰以标明其为一个注解,在使用注解是括号中的变量名称也即注解声明时方法名称,声明方法时可以默认指定该方法的默认值,即在使用注解时若不指定该“变量”则该“变量”使用该方法的默认值,这里需要说明的是,若方法名称为value,那么在使用注解时,注解中该“变量名”可以不写,如上述Retention注解中的值(这种写法要求使用该注解时只指定这一个方“变量”的值)。
如下示例我们分别演示了四个常用注解的声明方式:
package annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface FieldAnnotation { String say() default "this is a field annotation"; }
package annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MethodAnnotation { String say() default "this is a method annotation"; }
package annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.PARAMETER) public @interface ParamAnnotation { String say() default "this is a parameter annotation"; }
由于注解的使用已经非常广泛,下面我们通过一个类来简单演示注解的使用:
package source; import annotation.FieldAnnotation; import annotation.MethodAnnotation; import annotation.ParamAnnotation; import annotation.TypeAnnotation; @TypeAnnotation(say = "this is modified type annotation") public class Apple { @FieldAnnotation(say = "this is modified field annotation") private String color; @MethodAnnotation(say = "this is modified method annotation") public void setColor(@ParamAnnotation String color) { this.color = color; } public String getColor() { return color; } }
由于java规定,注解的使用不能对其使用类的存在产生影响,因而注解对于使用类只能起到一定的辅助作用,一个非常好的应用实例为javaweb中的权限控制,我们可以预定义一些权限码,然后在需要权限校验的地方使用自定义的注解;不过,因为java支持反射,因而通过反射注解就可以与使用类非常好的配合在一起,从而实现某些功能,一个非常好的例子是spring中的实体类对象属性的注入。下面我们通过一个简单的例子来说明如何通过反射获取注解值,并且利用反射使其与使用类产生关系:
package test; import annotation.FieldAnnotation; import annotation.MethodAnnotation; import annotation.ParamAnnotation; import annotation.TypeAnnotation; import source.Apple; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Parameter; import java.lang.reflect.Type; import org.junit.Test; public class App { @Test public void testApp() throws Exception { Apple apple = Apple.class.newInstance(); // 获取被注释的类 if (Apple.class.isAnnotationPresent(TypeAnnotation.class)) { System.out.println(Apple.class.getAnnotation(TypeAnnotation.class).say()); } // 获取被注释的变量 Field[] fields = Apple.class.getDeclaredFields(); for (Field f : fields) { if (f.isAnnotationPresent(FieldAnnotation.class)) { System.out.println(f.getAnnotation(FieldAnnotation.class).say()); } } // 获取被注释的方法 Method[] methods = Apple.class.getDeclaredMethods(); for (Method m : methods) { if (m.isAnnotationPresent(MethodAnnotation.class)) { System.out.println(m.getAnnotation(MethodAnnotation.class).say()); } // 获取当前方法被注释的参数 Parameter[] parameters = m.getParameters(); for (Parameter p : parameters) { if (p.isAnnotationPresent(ParamAnnotation.class)) { System.out.println(p.getAnnotation(ParamAnnotation.class).say()); } } // 通过反射调用方法 if (m.getName().startsWith("set")) { m.invoke(apple, "Red"); } } System.out.println("通过反射调用对象的方法后对象变量的值:" + apple.getColor()); } }
运行结果如下:
this is modified type annotation this is modified field annotation this is modified method annotation this is a parameter annotation 通过反射调用对象的方法后对象变量的值:Red
上述示例简单的阐述了反射和注解之间紧密的联系。在实际应用中, 注解的使用非常广泛,通过注解可以大大简化我们的开发,并且实现某些特定的效果,本文对注解的创建,使用以及深层调用方式进行了简单的介绍,希望大家能够喜欢!
相关推荐
java注解代码java注解代码java注解代码java注解代码java注解代码java注解代码java注解代码java注解代码java注解代码java注解代码java注解代码java注解代码java注解代码java注解代码java注解代码java注解代码java注解...
使用Java自定义注解来拼接SQL查询语句,方便使用。
java 自定义注解验证 可自己添加所需要的注解,本案例中只写了三个自定义注解类 本案例只花了半天时间,如有不好之处请多提提意见。谢谢!
简单demo,导入即可运行。
包括Java自定义注解以及包括自定义注解的应用实例。以及注解相关知识的注解
本文档简述了有关javabean中id的注解解释,注解本来就很繁琐,但是学好的话用起来还是比较方便的
自己写的一些关于java注解使用的例子,感觉里面写的挺全了,希望对大家的学习有帮助.有不足之处的话还请多多指教.
使用注解控制java事务, 类似spring处理,代码干净简洁
Java Annotation注解技术
Java注解实现
Java1.5引入了注解,当前许多java框架中大量使用注解,如Hibernate、Jersey、Spring。注解作为程序的元数据嵌入到程序当中。注解可以被一些解析工具或者是编译工具进行解析。我们也可以声明注解在编译过程或执行时...
Java注解的使用例子,使用Ant编译,执行run.bat运行.
里边主要是 junit-4.12.jar hamcrest-core-1.3.jar l两个jar包,是使用junit4,单元测试需要导入的包
java 元注解+拦截器实现自定义注解 @CmwAutoWired:自定义依赖注入 注意:注入的接口和实现类需要在同一包名下,注解的是类则无限制 @FieldAnnotation:自定义属性注解 @MethodAnnotation:自定义方法注解 @...
java动态代理对象实现日志输出,另外再配上annotation实现注解输出。
java中(注解符)处理Clob(blob)类型
java注解和反射的个人学习笔记
NULL 博文链接:https://smallbee.iteye.com/blog/1611962
Spring java注解,元注解和自定义注解 Spring java注解,元注解和自定义注解 Spring java注解,元注解和自定义注解 Spring java注解,元注解和自定义注解 Spring java注解,元注解和自定义注解
java 通过反射获取类上注解,方法上注解,注解里的值及方法参数,项目为maven项目。导入时记得选择maven项目