注解 Annotation

基于注解的开发,使得代码简洁,可读性高,简化的配置的同时也提高了开发的效率,尤其是SpringBoot的兴起,随着起步依赖和自动配置的完善,更是将基于注解的开发推到了新的高度。

元注解 meta-annotation

Java 5 定义了四个标准的元注解类型,用以提供对其它注解的功能说明。

位于java.lang.annotation包下,分别为:

  1. @Target

  2. @Retention

  3. @Documented

  4. @Inherited

以@Profile注解为例:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(ProfileCondition.class)
public @interface Profile { /**
* The set of profiles for which the annotated component should be registered.
*/
String[] value(); }

@Target

说明注解所修饰的对象范围。

注解可用于:package、type(类、接口、枚举、注解)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(循环变量、catch参数)。

按照作用范围,个人给出的常用注解,按照作用范围排序,各类暂举一:

  @Configuration、@MapperScan、@RestController、@RequestMapping、@ResponseBody、@Autowired、@Resource、@Value、@PathVariable。。。

具体的取值范围为ElementType(enum)类型的数组,见源码:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
/**
* Returns an array of the kinds of elements an annotation type
* can be applied to.
* @return an array of the kinds of elements an annotation type
* can be applied to
*/
ElementType[] value();
}
  1. ElementType.Constructor : 描述构造器
  2. ElementType.Field : 描述域
  3. ElementType.LocalVariable : 描述局部变量
  4. ElementType.Method : 描述方法
  5. ElementType.Package : 描述包
  6. ElementType.Parameter : 描述参数
  7. ElementType.Type : 描述类、接口、enum、annotation

@Retention

定义注解的作用时期(源文件、字节码、运行期)。

用以 说明被描述的注解在什么范围内生效。

取值唯一,即具体的保存策略,RetentionPolicy(enum)之一。

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
/**
* Returns the retention policy.
* @return the retention policy
*/
RetentionPolicy value();
}

RetentionPolicy包括:

  1. SOURCE : 源文件有效
  2. CLASS : 字节码文件有效
  3. RUNTIME : 运行时有效

@Documented

标记注解,可被javadoc之类的工具文档化,即描述该类型的注解,应该作为被标注的程序成员的公共API,没有成员

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}

@Inherited

标记注解,描述的注解是可被继承的。即如果一个类被@Inherited标记的注解所修饰,则该注解同样作用于这个类的子类。

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {
}

Spring注解与Java元注解小结的更多相关文章

  1. 使用Java元注解和反射实现简单MVC框架

    Springmvc的核心是DispatcherServlet来进行各种请求的拦截,进而进行后续的各种转发处理.流程图如下: 说明:客户端发出一个http请求给web服务器,web服务器对http请求进 ...

  2. java注解中的元注解

    一:java注解中的元注解 四个元注解分别是:@Target,@Retention,@Documented,@Inherited , 再次强调下元注解是java API提供,是专门用来定义注解的注解, ...

  3. Java元注解@Retention规则

    @Retention是java当中的一个元注解,该元注解通常都是用于对软件的测试 1.适用方式:     @Retention(RetentionPolicy.RUNTIME)     @interf ...

  4. Java元注解—— @Retention @Target @Document @Inherited

    java中元注解有四个: @Retention @Target @Document @Inherited: @Retention:注解的保留位置 @Retention(RetentionPolicy. ...

  5. Java 元注解

    元注解@Target,@Retention,@Documented,@Inherited * * @Target 表示该注解用于什么地方,可能的 ElemenetType 参数包括: * Elemen ...

  6. Java元注解

    元注解是指注解的注解,包括@Retention @Target @Document @Inherited四种. 1.@Retention: 定义注解的保留策略@Retention(RetentionP ...

  7. java元注解(注解在注解上的注解)

    //ElementType.TYPE 给类.接口.枚举上使用 @Target(ElementType.TYPE)//给注解进行注解,表示该注解可以用在什么地方 //@Retention(Retenti ...

  8. java valid 注解使用-java validation注解详解

    注解 描述 @AssertFalse 带注解的元素必须为false,支持boolean/Boolean @AssertTrue 带注解的元素必须为true,支持boolean/Boolean @Dec ...

  9. java元注解 @Retention注解使用

    @Retention定义了该Annotation被保留的时间长短: 1.某些Annotation仅出现在源代码中,而被编译器丢弃: 2.另一些却被编译在class文件中,注解保留在class文件中,在 ...

随机推荐

  1. SpringBoot整合Swagger2搭建API在线文档

    Swagger,中文"拽"的意思,它是一个功能强大的在线API在线文档,目前它的版本为2.x,所以称为Swagger2.Swagger2提供了在线文档的查阅和测试功能.利用Swag ...

  2. 初步学习Xamarin的感受

    一直仰慕Xamarin的大名,最近抽空去浅学了一下. 最后有一种这东西不咋地,又有一种这东西还不错的感觉 先说下为什么不咋地? 如果在公司项目使用Xamarin.forms这个东西.按照国内APP设计 ...

  3. uva11300 分金币(中位数)

    来源:https://vjudge.net/problem/UVA-11300 题意: 有n个人围成一圈,每个人有一定数量的金币,每次只能挪动一个位置,求挪动的最少金币使他们平分金币 题解: 蓝书p6 ...

  4. ubuntu中更改apache默认目录的方法

    如上,在这两个文件中,我都改为/home/www 及/home/www/html

  5. jconsole & jvisualvm远程监视websphere服务器JVM的配置案

    jconsole是JDK里自带的一个工具,可以监测Java程序运行时所有对象的申请.释放等动作,将内存管理的所有信息进行统计.分析.可视化.我们可以根据这些信息判断程序是否有内存泄漏问题. 使用jco ...

  6. Mysql数据库中的日期相关操作

    1.获取当前时间的日期 select now();----------------------------------如:2008-12-29 16:25:46 select curdate();-- ...

  7. JSP 快速入门

    目录 生命周期 9大对象 常用指令 基本语法 表达式语言(EL) jstl介绍 常用的jstl标签 生命周期 我们虽然写的是jsp,代码中包含了html.css.js,以及Java代码,但是真正执行的 ...

  8. 字符串和ASCII之间的转换

    public class CharToAscii { public static void main(String[] args) { CharToAscii.AscToString(); CharT ...

  9. bootstrap模态框关闭后清除模态框的数据

    https://segmentfault.com/q/1010000008789123 bootstrap模态框第二次打开时如何清除之前的数据? 我用了bootstrap模态框的remote功能,在弹 ...

  10. 腾讯机试题 AcWing 603 打怪兽

    题目链接:https://www.acwing.com/problem/content/605/ 题目大意: 略 分析: 用dp[i][j]表示用j元钱能在前i只怪兽上所能贿赂到的最大武力值. 有一种 ...