一直都看框架级的代码中都是各种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. TaskCompletionSource<TResult>

    参考:https://blogs.msdn.microsoft.com/pfxteam/2009/06/02/the-nature-of-taskcompletionsourcetresult/

  2. Java并发之CountDownLatch

    CountDownLatch是Java concurrent包下的一个同步工具.它可以让一个(或多个)线程等待,直到其他线程中的某些操作完成. 本质上是一个信号量,我们把它比作一个有N个插销的大门,它 ...

  3. Xms Xmx PermSize MaxPermSize 区别

    Eclipse崩溃,错误提示: MyEclipse has detected that less than 5% of the 64MB of Perm Gen (Non-heap memory) s ...

  4. [Storm] No data flows into bolt

    最近在HDP2.1的HBase环境中安装了一个Storm测试机器(单节点,JDK8),遇到了几个问题,记录下来. 尝试步骤 1. 使用和HBase一样HDP版本,直接安装Storm yum insta ...

  5. MySql简易配置

    选择standard configuration ,然后next Service Name :服务名字 Launch the MySQL Server automatically:是否开机启动mysq ...

  6. Java Web ——http协议请求报文

    package com.demo.util; import java.io.IOException; import java.io.InputStream; import java.net.*; /* ...

  7. Java上等价类划分测试的实现

    利用JavaFx实现对有效等价类和无效等价类的划分: 代码: import javafx.application.Application;import javafx.event.ActionEvent ...

  8. JavaScript对象创建,继承

    创建对象 在JS中创建对象有很多方式,第一种: var obj = new Object(); 第二种方式: var obj1 = {};//对象直面量 第三种方式:工厂模式 function Per ...

  9. 手机站使图片高度统一jq代码

    <script> function showImg(){ $(".honor_i_c img").each(function(index, element) { var ...

  10. Springmvc的跳转方式

    跳转到其他controller: 返回类型是String: return "forward:/log/home.action";  映射路径 跳转到本类Controller的某一个 ...