首页 > 其他分享 >SpringBoot @Target、@Retention、@Documented注解简介

SpringBoot @Target、@Retention、@Documented注解简介

时间:2023-01-19 15:56:25浏览次数:31  
标签:SpringBoot Target Inherited 注解 Documented ElementType METHOD Retention


jdk1.5起开始提供了4个元注解:@Target、@Retention、@Documented、@Inherited。何谓元注解?就是注解的注解。 在程序开发中,有时候我们需要自定义一个注解,这个自定义注解类就需要被元注解修饰,以定义该类的一些基本特征。 例如,我们创建一个LogAnnotation的自定义注解类:

@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface LogAnnotation {
	String module() default "";
}

@interface意思是声明一个注解,方法名对应参数名,返回值类型对应参数类型。

@Target

@Target注解用于定义注解的使用位置,如果没有该项,表示注解可以用于任何地方。@Target的格式为:

// 单参数
@Target({ ElementType.METHOD })
// 多参数
@Target(value = {ElementType.METHOD,ElementType.TYPE})

@Target的ElementType取值有以下类型:

  • TYPE:类,接口或者枚举
  • FIELD:域,包含枚举常量
  • METHOD:方法
  • PARAMETER:参数
  • CONSTRUCTOR:构造方法
  • LOCAL_VARIABLE:局部变量
  • ANNOTATION_TYPE:注解类型
  • PACKAGE:包

@Retention

@Retention注解用于指明修饰的注解的生存周期,即会保留到哪个阶段。格式为:

@Retention(RetentionPolicy.RUNTIME)

RetentionPolicy的取值包含以下三种:

  • SOURCE:源码级别保留,编译后即丢弃。
  • CLASS:编译级别保留,编译后的class文件中存在,在jvm运行时丢弃,这是默认值。
  • RUNTIME:运行级别保留,编译后的class文件中存在,在jvm运行时保留,可以被反射调用。

@Documented

指明修饰的注解,可以被例如javadoc此类的工具文档化,只负责标记,没有成员取值。

@Inherited

@Inherited注解用于标注一个父类的注解是否可以被子类继承,如果一个注解需要被其子类所继承,则在声明时直接使用@Inherited注解即可。如果没有写此注解,则无法被子类继承。

转自 https://www.cnblogs.com/myhomepages/p/16516768.html

标签:SpringBoot,Target,Inherited,注解,Documented,ElementType,METHOD,Retention
From: https://www.cnblogs.com/Kaelthas/p/17061659.html

相关文章

  • SpringBoot日志框架分析
    本文简介第一部分,介绍spring-jcl适配各种日志框架的方式第二部分,介绍slf4j适配各种日志框架的方式第三部分,介绍下logback框架的使用及原理 一、spring-jcl分析说......
  • springboot整合es
    pom.xml增加依赖(注意版本要和springboot匹配)<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-st......
  • springboot 热更 2023.3
    热更使用devtools或者alt+shit+f9ideaFile|Settings|Preferences|Build,Execution,Deployment|Compiler:BuildprojectautomaticallyFile|Setting......
  • springboot 常用项目结构
    servicex//项目名|-admin-ui//管理服务前端代码(一般将UI和SERVICE放到一个工程中,便于管理)|-servicex-auth//模块1......
  • SpringBoot(三)
    Swagger、分布式架构部分​​11、Swagger​​​​11.1、Swagger简介​​​​11.2、SpringBoot集成Swagger​​​​11.3、配置Swagger​​​​11.4、Swagger配置扫描接口​​......
  • SpringBoot(二)
    模板引擎、数据源、安全框架部分​​5、SpringBootWeb开发​​​​5.1、静态资源​​​​5.2、首页​​​​5.3、Thymeleaf模板引擎​​​​5.4、装配扩展SpringMVC​​​......
  • SpringBoot+MyBatils-puls+db2
    记录自己搭建db2数据库,链接数据库遇到的问题。 搭建db2我是在阿里云使用docker搭建的db2数据库,搭建数据库参考下面这个链接https://www.cnblogs.com/Yongzhouunknown/p......
  • SpringBoot源码学习3——SpringBoot启动流程
    系列文章目录和关于我一丶前言在《SpringBoot源码学习1——SpringBoot自动装配源码解析+Spring如何处理配置类的》中我们学习了SpringBoot自动装配如何实现的,在《Sprin......
  • 230118_50_SpringBoot入门
    yaml配置文件中,支持占位符配置person:name:bill${random.int}age:4happy:truebirth:2023/01/15maps:{k1:v1,k2:v2}hello:hellolists:-cat-dog-fish......
  • springboot-start 核心
    1.定义自动配置类 配置类上@Import设置自动导入bean类的选择器  2.spring.factories文件注册自动配置类   3.实现Import选择器 里面加载bean 4.如果......