ava注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置的功能。
注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用。包含在 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:定义注解的作用目标
@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:说明子类可以继承父类中的该注解
自定义annotation使用三步曲
1.define annotation interface
2.define parser annotation class
3.use annotation and it's parser
例子如下:
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) @Inherited public @interface HelloWorld { public String name()default ""; }
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); } } } }
public class SayHello { @HelloWorld(name = " 小明 ") 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(SayHello.class); } }
相关推荐
hibernate 注解 annotation 教程
annotation 注解 annotation的资料 JAVA注解 个人整理很有实用价值
java注解annotation
自定义注解annotation及用法
本文档对利用struts2 注解 annotation 实现文件下载作了简单介绍,并有代码为例,希望对学习struts2注解annotation的人有帮助,特别是需要动态传参方面。
主要介绍了java 注解annotation的使用以及反射如何获取注解的相关资料,需要的朋友可以参考下
Dwr3+Spring3 全注解 annotation 方式实现
Java注解Annotation用起来很方便,也越来越流行,由于其简单、简练且易于使用等特点,很多开发工具都提供了注解功能,不好的地方就是代码入侵比较严重,所以使用的时候要有一定的选择性。 这篇文章将利用注解,来做...
注解Annotation实现原理与自定义注解例子 每当你创建描述符性质的类或者接口时,一旦其中包含重复性的工 作,就可以考虑使用注解来简化与自动化该过程。 Java提供了四种元注解,专门负责新注解的创建工作
Java基础复习笔记12Java自定义注解Annotation的使用
JPA、EJB、Spring零配置等等怎么使用相信各位读者都能掌握,这里主要是说如何自定义自己的注解,自己使用自定义的注解。
csh框架+cxf+spring+hibernate+mysql 注解 annotation xml json, 参考了菠萝大象的文章,感谢。
基于注解Annotation的最新版SSH(Struts2.3.7+Hibernate4.1.9+Spring3.2.0)整合开发,真正实现零配置。 最新版本Struts、Spring、Hibernate框架整合: struts-2.3.7 spring-framework-3.2.0.RELEASE hibernate-...
这里是使用hibernate注解开发用到的annotation的jar包
项目中用到的 ssh2 oracle 全annotation 的lib包 亲测可通过
学习java自定义注解的小例子,处理运行时注解方法,可以结合博客学习,博客地址: http://blog.csdn.net/liuyonglei1314/article/details/59494503
对于Annotation,是Java5的新特性,JDK5引入了Metedata(元数据)很容易的就能够调用Annotations.Annotations提供一些本来不属于程序的数据,比如:一段代码的作者或者告诉编译器禁止一些特殊的错误。
详见http://blog.csdn.net/huaxun66/article/details/77507085
Android 注解Annotation 相关文章: Android AOP注解Annotation详解(一) Android AOP之注解处理解释器详解(二) Android AOP 注解详解及简单使用实例(三) Android AOP 等在Android上应用越来越广泛,例如框架...
新手学Java注解的最好资料,15分钟迅速开发