@interface是用来自定义JAVA Annotation的语法,
@interface是用来自定义注释类型的

注释类型的定义跟定义一个接口相似,我们需要在 interface这个关键字前面加上一个@符号,即@interface。

注释中的每一个方法定义了这个注释类型的一个元素,注释中方法的声明中一定不能包含参数,也不能抛出异常;方法的返回值被限制为简单类型、String、Class、emnus、注释,和这些类型的数组。方法可以有一个缺省值。

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

java用  @interface Annotation{ } 定义一个注解 @Annotation,一个注解是一个类。
@Override,@Deprecated,@SuppressWarnings为常见的3个注解。
注解相当于一种标记,在程序中加上了注解就等于为程序加上了某种标记,以后,
JAVAC编译器,开发工具和其他程序可以用反射来了解你的类以及各种元素上有无任何标记,看你有什么标记,就去干相应的事。

注解@Override用在方法上,当我们想重写一个方法时,在方法上加@Override,当我们方法
的名字出错时,编译器就会报错,如图:

注解@Deprecated,用来表示某个类的属性或方法已经过时,不想别人再用时,在属性和方法
上用@Deprecated修饰,如图:

注解@SuppressWarnings用来压制程序中出来的警告,比如在没有用泛型或是方法已经过时的时候,
 如图:

注解@Retention可以用来修饰注解,是注解的注解,称为元注解。
Retention注解有一个属性value,是RetentionPolicy类型的,Enum RetentionPolicy是一个枚举类型,
这个枚举决定了Retention注解应该如何去保持,也可理解为Rentention 搭配 RententionPolicy使用。RetentionPolicy有3个值:CLASS  RUNTIME   SOURCE
用@Retention(RetentionPolicy.CLASS)修饰的注解,表示注解的信息被保留在class文件(字节码文件)中当程序编译时,但不会被虚拟机读取在运行的时候;
用@Retention(RetentionPolicy.SOURCE )修饰的注解,表示注解的信息会被编译器抛弃,不会留在class文件中,注解的信息只会留在源文件中;
用@Retention(RetentionPolicy.RUNTIME )修饰的注解,表示注解的信息被保留在class文件(字节码文件)中当程序编译时,会被虚拟机保留在运行时,
所以他们可以用反射的方式读取。RetentionPolicy.RUNTIME 可以让你从JVM中读取Annotation注解的信息,以便在分析程序的时候使用.

package com.self;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;@Retention(RetentionPolicy.RUNTIME)
public @interface MyTarget
{ }
定义个一注解@MyTarget,用RetentionPolicy.RUNTIME修饰;
package com.self;
import java.lang.reflect.Method;
public class MyTargetTest
{@MyTargetpublic void doSomething(){System.out.println("hello world");}public static void main(String[] args) throws Exception{Method method = MyTargetTest.class.getMethod("doSomething",null);if(method.isAnnotationPresent(MyTarget.class))//如果doSomething方法上存在注解@MyTarget,则为true{System.out.println(method.getAnnotation(MyTarget.class));}}
}
上面程序打印:@com.self.MyTarget(),如果RetentionPolicy值不为RUNTIME,则不打印。@Retention(RetentionPolicy.SOURCE )
public @interface Override@Retention(RetentionPolicy.SOURCE )
public @interface SuppressWarnings@Retention(RetentionPolicy.RUNTIME )
public @interface Deprecated
由上可以看出,只有注解@Deprecated在运行时可以被JVM读取到注解中可以定义属性,看例子:
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation
{String hello() default "gege";String world();int[] array() default { 2, 4, 5, 6 };EnumTest.TrafficLamp lamp() ;TestAnnotation lannotation() default @TestAnnotation(value = "ddd");Class style() default String.class;
}
上面程序中,定义一个注解@MyAnnotation,定义了6个属性,他们的名字为:
hello,world,array,lamp,lannotation,style.
属性hello类型为String,默认值为gege
属性world类型为String,没有默认值
属性array类型为数组,默认值为2,4,5,6
属性lamp类型为一个枚举,没有默认值
属性lannotation类型为注解,默认值为@TestAnnotation,注解里的属性是注解
属性style类型为Class,默认值为String类型的Class类型看下面例子:定义了一个MyTest类,用注解@MyAnnotation修饰,注解@MyAnnotation定义的属性都赋了值
@MyAnnotation(hello = "beijing", world="shanghai",array={},lamp=TrafficLamp.RED,style=int.class)
public class MyTest
{@MyAnnotation(lannotation=@TestAnnotation(value="baby"), world = "shanghai",array={1,2,3},lamp=TrafficLamp.YELLOW)@Deprecated@SuppressWarnings("")public void output(){System.out.println("output something!");}
}接着通过反射读取注解的信息:
public class MyReflection
{public static void main(String[] args) throws Exception{MyTest myTest = new MyTest();Class<MyTest> c = MyTest.class;Method method = c.getMethod("output", new Class[] {});//如果MyTest类名上有注解@MyAnnotation修饰,则为trueif(MyTest.class.isAnnotationPresent(MyAnnotation.class)){System.out.println("have annotation");}if (method.isAnnotationPresent(MyAnnotation.class)){method.invoke(myTest, null); //调用output方法//获取方法上注解@MyAnnotation的信息MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);String hello = myAnnotation.hello();String world = myAnnotation.world();System.out.println(hello + ", " + world);//打印属性hello和world的值System.out.println(myAnnotation.array().length);//打印属性array数组的长度System.out.println(myAnnotation.lannotation().value()); //打印属性lannotation的值System.out.println(myAnnotation.style());}//得到output方法上的所有注解,当然是被RetentionPolicy.RUNTIME修饰的Annotation[] annotations = method.getAnnotations();for (Annotation annotation : annotations){System.out.println(annotation.annotationType().getName());}}
}
上面程序打印:
have annotation
output something!
gege, shanghai
3
baby
class java.lang.String
com.heima.annotation.MyAnnotation
java.lang.Deprecated如果注解中有一个属性名字叫value,则在应用时可以省略属性名字不写。
可见,@Retention(RetentionPolicy.RUNTIME )注解中,RetentionPolicy.RUNTIME是注解属性值,属性名字是value,
属性的返回类型是RetentionPolicy,如下:
public @interface MyTarget
{String value();
}
可以这样用:@MyTarget("aaa")public void doSomething(){System.out.println("hello world");}注解@Target也是用来修饰注解的元注解,它有一个属性ElementType也是枚举类型,
值为:ANNOTATION_TYPE CONSTRUCTOR  FIELD LOCAL_VARIABLE METHOD PACKAGE PARAMETER TYPE
如@Target(ElementType.METHOD) 修饰的注解表示该注解只能用来修饰在方法上。
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTarget
{String value() default "hahaha";
}
如把@MyTarget修饰在类上,则程序报错,如:
@MyTarget
public class MyTargetTest
注解大都用在开发框架中吧,好了有关注解就学习那么多了,谢谢。

  

spring boot: java @interface注解相关推荐

  1. Spring Boot中常见注解诠释

    一:@Mapper和@MapperScan 1.@Mapper @Mapper 将接口交给Spring进行管理,为这个接口生成一个实现类,让别的类进行引用.不再写mapper映射文件. @Mapper ...

  2. spring boot 自定义@EnableXXX注解

    前言 spring boot 自带了很多@EnableXXX这样的注解,通过这些注解我们可以很方便地启用某些功能,比如@EnableAutoConfiguration用来开启自动装配的功能.内部实现主 ...

  3. openshift k8s_带有DIY的Openshift上的Spring Boot / Java 8 / Tomcat 8

    openshift k8s DIY盒带是一种实验性盒带,提供了一种在OpenShift上测试不受支持的语言的方法. 它提供了最小限度的自由形式的支架,将墨盒的所有细节留给了应用程序开发人员 . 这篇博 ...

  4. 带有DIY的Openshift上的Spring Boot / Java 8 / Tomcat 8

    DIY墨盒是一种实验性墨盒,它提供了一种在OpenShift上测试不受支持的语言的方法. 它提供了最小限度的自由形式的支架,将墨盒的所有细节留给了应用程序开发人员 . 这篇博客文章说明了结合了Post ...

  5. 注解参数获取不到_scm-springboot基于spring boot的统一注解缓存

    scm-springboot 基于spring boot的统一注解缓存,支持mencached.redis.ehcache的缓存无缝切换.支持单个缓存设置过期时间,灵活的key设置规则,采用fastj ...

  6. Spring boot +java.awt.HeadlessException: null异常处理

    Spring boot +java.awt.HeadlessException: null异常处理 参考文章: (1)Spring boot +java.awt.HeadlessException: ...

  7. Spring Boot java.sql.SQLSyntaxErrorException: Table ‘mydb.table_name‘ doesn‘t exist

    我是强哥,一个在互联网苟且的男人 Spring Boot java.sql.SQLSyntaxErrorException: Table 'mydb.table_name' doesn't exist ...

  8. spring boot 的常用注解使用 总结

    附:Spring Boot 官方文档学习(一)入门及使用 见https://www.cnblogs.com/larryzeal/p/5799195.html @RestController和@Requ ...

  9. spring boot java app_利用spring boot创建java app

    利用spring boot创建java app 背景 在使用spring框架开发的过程中,随着功能以及业务逻辑的日益复杂,应用伴随着大量的XML配置和复杂的bean依赖关系,特别是在使用mvc的时候各 ...

最新文章

  1. php分享二十八:mysql运行中的问题排查
  2. 通过sql-labs进行sql注入学习(11-22)
  3. java例子:九九乘法表
  4. 马化腾定义腾讯是普通公司,这波重新定义“普通”可还行......
  5. pandas的自带数据集_pandas.DataFrame.sample随机抽样
  6. 直接拿来用,10个PHP代码片段
  7. 年增代码 12.9 亿行,每天完成需求近 4000 个,鹅厂程序员秘密大爆料!
  8. iOS开发最新之CocoaPods环境配置教程
  9. 山东理工大学oj打字速度测试
  10. set_include_path()
  11. 公司终于又一次屏蔽了QQ
  12. 如何通过name获取单选框和复选框选中状态的value值?
  13. 阿里云服务器是国内的还是国外的?
  14. layui-table表格根据条件更换表格背景颜色,高亮显示
  15. 普通工程师和高级工程师的差别在哪里?如何快速突破?
  16. 动画一:过渡(超详细!)
  17. 韦东山:驱动和APP,根本不应该上升到互相鄙视的地步
  18. java 串口 dtr rts_串口(RS232 RS485等)通讯中RTS/CTS,DTR/DSR的含义详解
  19. 想成为一名合格的前端工程师,需要掌握哪些技能?
  20. WIN10笔记本偶然会出现插入USB设备的时候报错:无法识别的usb设备,前一个设备不正常......

热门文章

  1. 不用下载安装,你的机器人可以直接在浏览器里跳舞丨Jupyter-ROS
  2. 思必驰AI芯片发布:内置完整语音交互方案,支持离线模式,All in One
  3. AutoX披露无人车云代驾系统:夜晚远程操控车队也easy
  4. IOS开发中遇到的问题
  5. Swift实践:使用CoreData存储多种数据类的通讯录
  6. iptables的详细介绍及配置方法
  7. 极简版ASP.NET Core学习路径及教程
  8. Java Web文件下载
  9. Linux系统上利用nmcli命令创建网络组
  10. 为Laravel的artisan指令增加bash脚本