首页 > 其他分享 >@Documented 的作用

@Documented 的作用

时间:2022-10-06 10:02:11浏览次数:38  
标签:String default ContextConfiguration locations AliasFor 注解 Documented 作用

1. @Documented 的含义

@Document 是 java 在生成文档,是否显示注解的开关。

2. 加和不加有什么区别?

测试注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RestMapping {
String[] value() default {};

String[] path() default {};
}

测试类 TerminalController

@RestMapping("/terminal")
public class TerminalController {

使用命令

// 生成 java doc 的命令:
javadoc -d doc TerminalController.java`
// -d <directory> 输出文件的目标目录

a. 不加的效果

@Documented 的作用_xml

b. 加上的效果


@Documented 的作用_显式_02

可以看到在 public class TerminalController 上面多了一个我们写的注解。这就是 @Documented 的用处。

3. 题外话:注解合并

java 注解原本只是一种不会被编译器忽略的注释。本身对代码逻辑没有任何影响(可以用来判断是否存在,能读取内容信息),而使用效果完全由使用工具决定。

注解被很多规范使用,作为标记或者约定,如 jsr303 参数校验。

a. 注解合并

在 springboot 中注解发挥了很大的作用,而这些作用仅在springboot中有用,就好比@AliasFor。

注解本身并不能被注解继承,而 springboot 中却看到大量的合并注解就好比 @RestController = @Controller + @ResponseBody。这就是 @AliasFor 的功劳。需要注意的是,离开 Spring 就无法使用了。

spring 的注解都是由下面这个类读取的,所以 spring 的注解可以进行注解合并(有且仅限 spring 注解:Spring Annotations)

public interface MergedAnnotations

b. @AliasFor

@AliasFor 有四个作用:

注释中的显式别名:
在@ContextConfiguration中, value和locations是彼此的显式别名。

public @interface ContextConfiguration {
@AliasFor("locations")
String[] value() default {};

@AliasFor("value")
String[] locations() default {};

// ...
}

元注释中属性的显式别名:
在@XmlTestConfig中, xmlFiles是@ContextConfiguration中locations的显式别名。换句话说, xmlFiles覆盖了@ContextConfiguration中的locations属性。

 @ContextConfiguration
public @interface XmlTestConfig {

@AliasFor(annotation = ContextConfiguration.class, attribute = "locations")
String[] xmlFiles();
}

注释中的隐式别名:
在@MyTestConfig中, value 、 groovyScripts和xmlFiles都是@ContextConfiguration中locations属性的显式元注释属性覆盖。因此,这三个属性也是彼此的隐式别名。

 @ContextConfiguration
public @interface MyTestConfig {
@AliasFor(annotation = ContextConfiguration.class, attribute = "locations")
String[] value() default {};

@AliasFor(annotation = ContextConfiguration.class, attribute = "locations")
String[] groovyScripts() default {};

@AliasFor(annotation = ContextConfiguration.class, attribute = "locations")
String[] xmlFiles() default {};
}

注释中的传递隐式别名:
在@GroovyOrXmlTestConfig中, groovy是对@MyTestConfig中groovyScripts属性的显式覆盖;而xml是对@ContextConfiguration中的locations属性的显式覆盖。此外, groovy和xml是彼此的可传递隐式别名,因为它们都有效地覆盖了@ContextConfiguration中的locations属性。

@MyTestConfig
public @interface GroovyOrXmlTestConfig {
@AliasFor(annotation = MyTestConfig.class, attribute = "groovyScripts")
String[] groovy() default {};

@AliasFor(annotation = ContextConfiguration.class, attribute = "locations")
String[] xml() default {};
}

标签:String,default,ContextConfiguration,locations,AliasFor,注解,Documented,作用
From: https://blog.51cto.com/u_15733182/5733432

相关文章

  • 从这两道题重新理解,JS的this、作用域、闭包、对象
    日常开发中,我们经常用到this。例如用Jquery绑定事件时,this指向触发事件的DOM元素;编写Vue、React组件时,this指向组件本身。对于新手来说,常会用一种意会的感觉去判断this的指......
  • 深入理解JS作用域链与执行上下文
    变量提升:变量提升(hoisting)。我可恨的var关键字:你读完下面内容就会明白标题的含义,先来一段超级简单的代码:<scripttype="text/javascript">varstr='Hello......
  • 面试官:说说JS作用域和作用域链,我是这样回答的
    作用域(scope)1.什么是作用域概念:作用域是在程序运行时代码中的某些特定部分中变量、函数和对象的可访问性。从使用方面来解释,作用域就是变量的使用范围,也就是在代码的......
  • 从变量声明到作用域到闭包
    Es6新增加了两种声明变量的方式let,const从客观上为这门语言更精确地声明作用域和语义提供了更好的支持。以下是三种声明方式的区别与对比 作用域   初始化值......
  • 变量作用域
    说明:  例子:  形参也会被视为当前函数的一个局部变量如intf1(intn)其中的n视为f1函数的局部变量在函数外部定义的变量称之为全局变量,作用域为整个程序当形......
  • 一些js 插件的作用
    前言:从一些开源网站上下载下来的后台管理系统模板一般会有很多的js,其js的功能是什么呢?这里随手查询了一下,记录下来正文:1.zDialog.js各种弹窗插件详细案例:​​​http:......
  • 第一季:7Spring Bean的作用域之间有什么区别【Java面试题】
    第一季:7SpringBean的作用域之间有什么区别【Java面试题】​​前言​​​​推荐​​​​第一季:7SpringBean的作用域之间有什么区别​​​​题目​​​​2.6bean的作用域★......
  • 痞子衡嵌入式:浅谈i.MXRT10xx系列MCU外接24MHz晶振的作用
    大家好,我是痞子衡,是正经搞技术的痞子。今天痞子衡给大家介绍的是i.MXRT10xx系列MCU外接24MHz晶振的作用。痞子衡之前写过一篇关于时钟引脚的文章《i.MXRT1xxx系......
  • zeroMemory 其作用是用0来填充一块内存区域
    ZeroMemory是一个计算机函数,由美国微软公司的软件开发包SDK中的一个宏。其作用是用0来填充一块内存区域。声明voidZeroMemory(PVOIDDestination,SIZE_TLength);参数......
  • 002.Spring IOC的原理——包括:Spring IOC简介——Spring IOC的装配流程——Spring Be
    1.5SpringIOC的原理1.5.1SpringIOC简介Spring通过一个配置文件描述Bean与Bean之间的依赖关系,利用Java的反射功能实例化Bean并建立Bean之间的关系。Spring的IOC容器......