一直都看框架级的代码中都是各种annotation,一起来看看到底怎么弄的

例子1:直接定义一个annotation,并使用之:

package com.base.annotation.example;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; /**
* Created by guangyi on 15/12/8.
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTarget {
String value();
} package com.base.annotation.example; import java.lang.reflect.Method; /**
* Created by guangyi on 15/12/8.
*/
public class MyTargetTest {
@MyTarget("test")
public void doSomething() {
System.out.println("hello world");
}
public static void main(String[] args) throws NoSuchMethodException{
Method method = MyTargetTest.class.getMethod("doSomething", null);
if(method.isAnnotationPresent(MyTarget.class)) {
System.out.println(method.getAnnotation(MyTarget.class)); // 打印@com.base.annotation.example.MyTarget(value=test)
}
}
}

例子2:复杂类型的,各种注解类型

package com.base.annotation.example.example2;

/**
* Created by guangyi on 15/12/8.
*/ public class EnumTest {
public static enum TrafficLamp {
RED, GREEN, YELLOW
}
} package com.base.annotation.example.example2; import com.base.annotation.example.MyTarget; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; /**
* Created by guangyi on 15/12/8.
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String hello() default "hello";
String world();
int[] array() default {1, 2, 3, 4};
EnumTest.TrafficLamp lamp();
MyTarget myAnnotation() default @MyTarget("ddd");
Class style() default String.class;
} package com.base.annotation.example.example2; import com.base.annotation.example.MyTarget; import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; /**
* Created by guangyi on 15/12/8.
*/
@MyAnnotation(hello = "beijing", world = "shanghai", array = {5, 6, 7, 8}, lamp = EnumTest.TrafficLamp.GREEN, style = int.class)
public class MyTest {
@MyAnnotation(myAnnotation = @MyTarget("baby"), world = "shanghai", array = {10, 11, 12}, lamp = EnumTest.TrafficLamp.YELLOW)
@Deprecated
@SuppressWarnings("")
public void output() {
System.out.println("output something!");
} public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException{
MyTest myTest = new MyTest();
Class<MyTest> c = MyTest.class;
Method method = c.getMethod("output", new Class[]{});
if(MyTest.class.isAnnotationPresent(MyAnnotation.class)) {
System.out.println("have annotation");
}
if(method.isAnnotationPresent(MyAnnotation.class)) {
method.invoke(myTest, null);
MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);
String hello = myAnnotation.hello();
String world = myAnnotation.world();
System.out.println(hello + ", " + world);
System.out.println(myAnnotation.array().length);
System.out.println(myAnnotation.myAnnotation().value());
System.out.println(myAnnotation.style());
System.out.println(myAnnotation.lamp());
}
Annotation[] annotations = method.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation.annotationType().getName());
}
}
}

示例3:看原始注解的定义,可以看到也是使用注解,另外,使用value作为注解属性名,则可以省略注解名字不写,例如:@Rentention(RententionPolicy.RUNTIME):

package java.lang;

import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*; @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
String[] value();
} package java.lang; import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*; @Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
public @interface Deprecated {
} package java.lang.annotation; @Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
RetentionPolicy value();
} package java.lang.annotation; @Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
ElementType[] value();
}

参考:http://blog.csdn.net/liuwenbo0920/article/details/7290586/

[JAVA] 注解学习@interface的更多相关文章

  1. Java注解学习

    一.注解定义 JVM5.0定义了4个标准的元注解: @Target, @Retention, @Documented @Inherited 1. @Target 作用:用于描述注解的使用范围 取值El ...

  2. java 注解 学习

    周末闲来无事,想要研究一下注解方面的知识,曾经看过几次,都忘记了,这次学习下,而且写篇文章记录下, 1.元注解  元注解是指注解的注解.包含 @Retention @Target @Document ...

  3. Java注解学习笔记

    我们平常写Java代码,对其中的注解并不是很陌生,比如说写继承关系的时候经常用到@Override来修饰方法.但是@Override是用来做什么的,为什么写继承方法的时候要加上它,不加行不行.如果对J ...

  4. java注解学习(1)注解的作用和三个常用java内置注解

    今天,记录一下自己学习的关于注解方面的知识. Annotation是从JDK5.0开始引入的新技术 Annotation的作用: -不是程序本身,可以对程序做出解释(这一点和注释没什么区别) -可以被 ...

  5. java注解学习笔记总结

    注解的理解 ① jdk 5.0 新增的功能 ② Annotation 其实就是代码里的特殊标记, 这些标记可以在编译, 类加载, 运行时被读取, 并执行相应的处理.通过使用 Annotation,程序 ...

  6. java注解使用总结

    2005年,sun公司推出了jdk1.5,同时推出的注解功能吸引了很多人的目光,使用注解编写代码,能够减轻java程序员繁琐配置的痛苦. 使用注解可以编写出更加易于维护,bug更少的代码. 注解是什么 ...

  7. 分享:自定义JAVA注解

    元注解 元注解指用来定义注解的注解,例如:@Retention @Target Inherited @Documented等等.最为重要和经常使用的是@Retention @Target. @Rete ...

  8. 深入学习JAVA注解-Annotation(学习过程)

    JAVA注解-Annotation学习 本文目的:项目开发过程中遇到自定义注解,想要弄清楚其原理,但是自己的基础知识不足以支撑自己去探索此问题,所以先记录问题,然后补充基础知识,然后解决其问题.记录此 ...

  9. Java注解系统学习与实战

    背景 为什么要再次梳理一下java注解,显而易见,因为重要啊.也是为研究各大类开源框架做铺垫,只有弄清楚Java注解相关原理,才能看懂大部分框架底层的设计. 缘起 注解也叫做元数据,是JDK1.5版本 ...

随机推荐

  1. hdu2005第几天?

    Problem Description 给定一个日期,输出这个日期是该年的第几天.   Input 输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input , ...

  2. [老文章搬家] 关于 Huffman 编码

    按:去年接手一个项目,涉及到一个一个叫做Mxpeg的非主流视频编码格式,编解码器是厂商以源代码形式提供的,但是可能代码写的不算健壮,以至于我们tcp直连设备很正常,但是经过一个UDP数据分发服务器之后 ...

  3. Timequest GUI

    Tasks界面 使用Tasks界面可以访问常用命令,例如生成网表建立报告等. 两个常用命令位于Tasks界面中:打开工程和编写SDC文件.其他命令在下面的文件夹中: Netlist Setup Rep ...

  4. 为什么Java不适合游戏开发

    Strawberry Cow Bear: why java sucks for game developmenthttp://strawberrycowbear.blogspot.jp/2011/02 ...

  5. IOS中div contenteditable=true无法输入

    在IOS中<div contenteditable="true"></div>中点击时可以弹出键盘但是无法输入.加一个样式-webkit-user-sele ...

  6. 使用Grunt构建自动化开发环境

    1.准备工作 1)首页确保电脑上网,以及能够访问https://registry.npmjs.org/,因需从此网站中下载安装相应的插件; 2)电脑安装Node.js,Grunt及Grunt插件都是基 ...

  7. 让ecshop模板支持php运算

    让ecshop模板支持php运算在 cls_template.php 底部加入函数: /** * 处理if标签 * * @access public * @param string $tag_args ...

  8. ios app的版本号

    ios其实有3个版本号 version 就是ios的版本号 (只能分3段,并且都是数字) build 是ios构建内部版本时的版本号 (可以分4段) 而提交到appstore时, 还是要创建一个sku ...

  9. CSS3学习基本记录

    CSS3 边框 border-radius: 圆角 border-radius: 15px 50px 70px 100px; 左上 右上 右下 左下 box-shadow:阴影 box-shadow: ...

  10. 超详细Web前端开发规范文档

    http://www.w3cfuns.com/notes/26488/c2ae788c77f835357025026a148b9863.html