注解(Annotation) 为我们在代码中天界信息提供了一种形式化的方法,是我们可以在稍后
某个时刻方便地使用这些数据(通过 解析注解 来使用这些数据)。
注解的语法比较简单,除了@符号的使用以外,它基本上与java的固有语法一致,java内置了三种
注解,定义在java.lang包中。
@Override 表示当前方法是覆盖父类的方法。
@Deprecated 表示当前元素是不赞成使用的。
@SuppressWarnings 表示关闭一些不当的编译器警告信息。
下面是一个定义注解的实例
- package Test_annotation;
-
- import java.lang.annotation.Documented;
- import java.lang.annotation.Inherited;
- import java.lang.annotation.Retention;
- import java.lang.annotation.Target;
- import java.lang.annotation.ElementType;
- import java.lang.annotation.RetentionPolicy;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- @Target(ElementType.METHOD)
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- @Inherited
-
-
-
-
-
- public @interface Test {
- public int id();
- public String description() default "no description";
- }
下面是一个使用注解 和 解析注解的实例
- package Test_annotation;
-
- import java.lang.reflect.Method;
-
- public class Test_1 {
-
-
-
- @Test(id = 1, description = "hello method_1")
- public void method_1() {
- }
-
- @Test(id = 2)
- public void method_2() {
- }
-
- @Test(id = 3, description = "last method")
- public void method_3() {
- }
-
-
-
-
- public static void main(String[] args) {
- Method[] methods = Test_1.class.getDeclaredMethods();
- for (Method method : methods) {
-
-
-
- boolean hasAnnotation = method.isAnnotationPresent(Test.class);
- if (hasAnnotation) {
-
-
-
- Test annotation = method.getAnnotation(Test.class);
- System.out.println("Test( method = " + method.getName()
- + " , id = " + annotation.id() + " , description = "
- + annotation.description() + " )");
- }
- }
- }
-
- }
输出结果如下:
Test( method = method_1 , id = 1 , description = hello method_1 )
Test( method = method_2 , id = 2 , description = no description )
Test( method = method_3 , id = 3 , description = last method )
分享到:
相关推荐
hibernate 注解 annotation 教程
annotation 注解 annotation的资料 JAVA注解 个人整理很有实用价值
java注解annotation
自定义注解annotation及用法
本文档对利用struts2 注解 annotation 实现文件下载作了简单介绍,并有代码为例,希望对学习struts2注解annotation的人有帮助,特别是需要动态传参方面。
主要介绍了java 注解annotation的使用以及反射如何获取注解的相关资料,需要的朋友可以参考下
Dwr3+Spring3 全注解 annotation 方式实现
注解Annotation实现原理与自定义注解例子 每当你创建描述符性质的类或者接口时,一旦其中包含重复性的工 作,就可以考虑使用注解来简化与自动化该过程。 Java提供了四种元注解,专门负责新注解的创建工作
Java注解Annotation用起来很方便,也越来越流行,由于其简单、简练且易于使用等特点,很多开发工具都提供了注解功能,不好的地方就是代码入侵比较严重,所以使用的时候要有一定的选择性。 这篇文章将利用注解,来做...
Java基础复习笔记12Java自定义注解Annotation的使用
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-...
JPA、EJB、Spring零配置等等怎么使用相信各位读者都能掌握,这里主要是说如何自定义自己的注解,自己使用自定义的注解。
项目中用到的 ssh2 oracle 全annotation 的lib包 亲测可通过
学习java自定义注解的小例子,处理运行时注解方法,可以结合博客学习,博客地址: http://blog.csdn.net/liuyonglei1314/article/details/59494503
这里是使用hibernate注解开发用到的annotation的jar包
Android 注解Annotation 相关文章: Android AOP注解Annotation详解(一) Android AOP之注解处理解释器详解(二) Android AOP 注解详解及简单使用实例(三) Android AOP 等在Android上应用越来越广泛,例如框架...
对于Annotation,是Java5的新特性,JDK5引入了Metedata(元数据)很容易的就能够调用Annotations.Annotations提供一些本来不属于程序的数据,比如:一段代码的作者或者告诉编译器禁止一些特殊的错误。
详见http://blog.csdn.net/huaxun66/article/details/77507085
新手学Java注解的最好资料,15分钟迅速开发