spring boot: java @interface注解
@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
{
@MyTarget
public 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修饰,则为true
if(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注解的更多相关文章
- (32)Spring Boot使用@SpringBootApplication注解,从零开始学Spring Boot
[来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论] 如果看了我之前的文章,这个节你就可以忽略了,这个是针对一些刚入门的选手存在的困惑进行写的一篇文章. 很多Spring Boot开发者总是使用 ...
- Spring Boot 二十个注解
Spring Boot 二十个注解 占据无力拥有的东西是一种悲哀. Cold on the outside passionate on the insede. 背景:Spring Boot 注解的强大 ...
- spring boot @ConditionalOnxxx相关注解总结
Spring boot @ConditionalOnxxx相关注解总结 下面来介绍如何使用@Condition public class TestCondition implements Condit ...
- Spring boot 使用的注解有哪些?
Spring boot 使用的注解有哪些? 注解 作用 @SpringBootApplication 等价于 @Configuration + @EnableAutoConfiguration + @ ...
- Spring boot ---- java.lang.NoClassDefFoundError: javax/servlet/ServletContext
Spring boot ---- java.lang.NoClassDefFoundError: javax/servlet/ServletContext 场景描述 项目中用到spring boo ...
- spring boot 中@Autowired注解无法自动注入的错误
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/huihuilovei/article/de ...
- Spring Boot中@Scheduled注解的使用方法
Spring Boot中@Scheduled注解的使用方法 一.定时任务注解为@Scheduled,使用方式举例如下 //定义一个按时间执行的定时任务,在每天16:00执行一次. @Scheduled ...
- Spring Boot 的核心注解是哪个?它主要由哪几个注解组成的?
启动类上面的注解是@SpringBootApplication,它也是 Spring Boot 的核心注解,主要组合包含了以下 3 个注解: @SpringBootConfiguration:组合了 ...
- 详解spring boot mybatis全注解化
本文重点介绍spring boot mybatis 注解化的实例代码 1.pom.xml //引入mybatis <dependency> <groupId>org.mybat ...
随机推荐
- Power Systems 虚拟化简介
本文向您详细地介绍了 Power System 虚拟化相关的技术和亮点,让您对这些最新的虚拟化技术有一个全面的了解.本文来自 IBM Systems Magazine for AIX 中文版. 自从引 ...
- IBM WebSphere cannot start in RAD 9.1
Have solved the problem. Solutions follows Step 1: double click on "WebSphere Application Serve ...
- 什么是 C++ 11 原始字符串?
std::string path = "C:\\VulkanSDK";//需要转义 std::string path = R"(C:\VulkanSDK)";/ ...
- 扩展运算符和rest运算符
扩展运算符 扩展运算符用三个点号表示,功能是把数组或类数组对象展开成一系列用逗号隔开的值 一.拆分数组 扩展运算符可以直接把数组拆分成用逗号隔开的值 <template> <sect ...
- Java输入输出重定向代码
try { BufferedInputStream in = new BufferedInputStream(new FileInputStream("input.txt")) ...
- [Idea]安装avtiviti插件以及 插件中文乱码
安装插件 打开IDEA,按ctrl+alt+S,打开Pluging 乱码问题 idea 安转activiti插件后,编辑流程图发现保存后中文乱码,并且idea的字符集(Settings—>Edi ...
- 印象笔记Mac端快捷键
- github代码托管
下载github客户端软件 1) 官网下载help.github.com 2) 百度搜索,一般用于windows7以前的系统 安装github软件 按照软件提示安装即可.不过,博主倾向使用命令行工 ...
- SQL SERVER 2005 Express版, 精简版 下载
Microsoft SQL Server 2005 Express Edition(数据库) https://www.microsoft.com/zh-CN/download/details.as ...
- Spring:笔记整理(1)——HelloWorld
Spring:笔记整理(1)——HelloWorld 导入JAR包: 核心Jar包 Jar包解释 Spring-core 这个jar 文件包含Spring 框架基本的核心工具类.Spring 其它组件 ...