一直都看框架级的代码中都是各种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. opencv_判断两张图片是否相同

    QQ:231469242 pip install opencv 如果找不到版本,去非官方下载opencv第三方包http://www.lfd.uci.edu/~gohlke/pythonlibs/ 下 ...

  2. thinkphp vendor

    vendor('weixin.request'); 对应文件:Core\Library\Vendor\weixin\request.php import('weixin.request#class') ...

  3. window server 2008配置FTP服务器550 Access is denied. 问题解决办法

    如果你是按照网上的通用方法,在搭建FTP服务器的时候临时建了一个用户的话,那么这个用户是普通用户,你即便勾选了“读”“写”权限,那么再打开防火墙的情况下,你还是不能上传文件,只能下载.只需要登录到服务 ...

  4. CJCMS系列--持久层对MangoDB的支持

    持久层添加对MangoDB数据库的支持 using System; using System.Collections.Generic; using System.Linq; using System. ...

  5. 关于BCGControlbar16.1版本的安装与使用

    csdn上有BCGControlbar16.1版本的下载,地址:http://download.csdn.net/detail/wangxiangdong_sl/4821726带key,个人亲测VS2 ...

  6. 【bzoj2073】[POI2004]PRZ

    题目描述 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍在桥上的人都不能超过一定的限制. 所以这只队伍过桥时只能分批 ...

  7. (转)System.Drawing.Color的颜色对照表

    经常使用System.Drawing.Color, 本篇介绍一下颜色与名称及RGB值的对应关系. 1. 颜色与名称的对照表(点击下图放大看): 2. 颜色与RGB值对照表: Color.AliceBl ...

  8. 重写官方TodoList,对于初学react+redux的人来说,很有好处

    虽然官网的TodoList的例子写的很详细,但是都是一步到位,就是给你一个action,好家伙,全部都写好了,给你一个reducer,所有功能也是都写好了,但是我们这些小白怎么可能一下就消化那么多,那 ...

  9. 将本地项目提交到coding上托管

    1:   注册coding并新建项目test2:在终端 cd 到要提交的项目  使用git init创建.git文件夹3:使用git pull  <项目地址>https的那个4:git a ...

  10. C#夯实基础之多线程二:主线程、前台线程与后台线程

    我们在<C#夯实基础之多线程一:初识多线程>一文中第二部分中指出,既然windows最终发展出了多线程模型,按理说,我们直接使用一个.NetFramework的线程类就可以直接撸代码了,但 ...