首页 > 其他分享 >6、注解Annotation

6、注解Annotation

时间:2023-08-07 17:46:23浏览次数:39  
标签:lang java name annotation 注解 ElementType Annotation

一、注解的本质:

1、注解,Annotation是JDK5.0引入的新技术。

2、注解的格式:@注释名,还可以添加参数(必要时)

3、注解不是程序本身,但可以对程序作出解释(就这一点,注释和注解的作用类似)

4、注解可以被其他程序读取(比如编译器等等)

5、注解可以给Java包、类型(类、接口、枚举)、构造器、方法、域、参数和局部变量进行注解,相当于给它们添加了一些额外的辅助信息。Java编译器可以根据指令来解释注解和放弃注解,或者将注解放到编译后的生成的class文件中,运行时可用。通过反射机制编程实现对这些元数据的访问。

注:注解的定义方式与接口相似,使用 @interface 关键字来定义注解。

 

二、元注解:

 java.lang.annotation

1、@Documented:

指定被标注的注解会包含在javadoc中。

2、@Retention:

指定注解的生命周期(源码、class文件、运行时),其参考值见类的定义:java.lang.annotation.RetentionPolicy

● RetentionPolicy.SOURCE :

在编译阶段丢弃。这些注解在编译结束之后就不再有任何意义,所以它们不会写入字节码。@Override, @SuppressWarnings都属于这类注解。

● RetentionPolicy.CLASS :

在类加载的时候丢弃。在字节码文件的处理中有用。注解默认使用这种方式。

● RetentionPolicy.RUNTIME :

始终不会丢弃,运行期也保留该注解,因此可以使用反射机制读取该注解的信息。我们自定义的注解通常使用这种方式。

 

3、@Target:

指定注解使用的目标范围(类、方法、字段等),其参考值见类的定义:java.lang.annotation.ElementType

● ElementType.CONSTRUCTOR :用于描述构造器。

● ElementType.FIELD :成员变量、对象、属性(包括enum实例)。

● ElementType.LOCAL_VARIABLE: 用于描述局部变量。

● ElementType.METHOD : 用于描述方法。

● ElementType.PACKAGE :用于描述包。

● ElementType.PARAMETER :用于描述参数。

● ElementType.ANNOTATION_TYPE:用于描述参数

● ElementType.TYPE :用于描述类、接口(包括注解类型) 或enum声明。

4、@Inherited:

指定子类可以继承父类的注解,只能是类上的注解,方法和字段的注解不能继承。即如果父类上的注解是@Inherited修饰的就能被子类继承。

 

三、自定义注解:

1、声明注解:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    String name() default "";
}

2、标记注解:

@Data
public class ResponseVO {

    @MyAnnotation(name = "UUID")
    private String uuid;

    @MyAnnotation(name = "姓名")
    private String name;

    @MyAnnotation(name = "状态")
    private String status;

}

3、应用:

public class AnnotationWork {
    /**
     * 解析字段详情
     *
     * @Return {"name":"姓名","uuid":"UUID","status":"状态"}
     * */
    public static void main(String[] args) {
        Map<String, String> resultParams = new HashMap<>();
        for (Field field : ResponseVO.class.getDeclaredFields()) {
            MyAnnotation annotation = field.getAnnotation(MyAnnotation.class);
            resultParams.put(field.getName(), annotation.name());
        }
        System.out.println(JSONObject.toJSONString(resultParams));
    }
}

 

标签:lang,java,name,annotation,注解,ElementType,Annotation
From: https://www.cnblogs.com/Iven-L/p/17612018.html

相关文章

  • 基于注解的(属性)依赖注入
    依赖注入(DI):说白了就是对于属性的赋值。基于xml的依赖注入有两种:第一种是基于setter方法的属性注入:<propertyname=""value(ref)=""></property>。第二种是基于构造方法的属性注入:<construct-argname=""value(ref)=""></property>上面两种无论是基于setter的bean属性注......
  • 8、Spring之基于注解的自动装配
    8.1、场景模拟8.1.1、UserDao接口及实现类packageorg.rain.spring.dao;/***@authorliaojy*@date2023/8/5-18:32*/publicinterfaceUserDao{voidsaveUser();}packageorg.rain.spring.dao.impl;importorg.rain.spring.dao.UserDao;importorg......
  • 注解(I)
    JDK元注解@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Componentpublic@interfaceExample{上面是什么意思?@Target指定注解可以用于哪些元素上可以用来限制注解的使用范围,从而提高注解的使用效率和安全性。ElementType.ANNOTATION_TYPE:注解类型......
  • mybatis-plus中的@Select注解里面写sql语句的in
    @Select("<script>"+"select\n"+"email\n"+"fromsys_user\n"+"whereidin\n"+"<foreachitem='item'index='index'collection='ids'open='(&......
  • mybatis-plus中的@Select注解里面写sql语句的in
    @Select("<script>"+    "select\n"+    "email\n"+    "fromsys_user\n"+    "whereidin\n"+    "  <foreachitem='item'index='index'colle......
  • mybatis-plus中的@Select注解里面写sql语句的in
    @Select("<script>"+    "select\n"+    "email\n"+    "fromsys_user\n"+    "whereidin\n"+    "  <foreachitem='item'index='index'colle......
  • day124 - 基于注解管理bean
    基于注解管理bean注解和XML配置文件一样,注解本身并不能执行,注解本身仅仅只是做一个标记,具体的功能是框架检测到注解标记的位置,然后针对这个位置按照注解标记的功能来执行具体操作。本质上:所有一切的操作都是Java代码来完成的,XML和注解只是告诉框架中的Java代码如何执行。S......
  • 一步一步实现若依框架--2.5匿名注解@Anonymous
     1实现绕过权限认证,可以直接访问某些接口。这些部分可以直接在SpringSecurity中的配置去写,也可以像这个主角这样给添加了注解的方法或类进行放行。原理:在springsecurity设置拦截前,获取到所有添加了该注解的请求,把这些请求添加到放开拦截的配置中。2实现a)新增注解(注解......
  • 对于Spring中的@Scheduled注解,cron表达式的格式与传统的cron表达式有所不同。
    @Scheduled(cron="00*/1**?")对于Spring中的@Scheduled注解,cron表达式的格式与传统的cron表达式有所不同。Spring的cron表达式包含6个字段,分别是秒分时日月星期。其中,秒是可选的。根据您提供的@Scheduled(cron="00*/1**?"),这表示任务会在每个小时的0分0秒执......
  • @ControllerAdvice注解使用及原理探究 | 京东物流技术团队
    最近在新项目的开发过程中,遇到了个问题,需要将一些异常的业务流程返回给前端,需要提供给前端不同的响应码,前端再在次基础上做提示语言的国际化适配。这些异常流程涉及业务层和控制层的各个地方,如果每个地方都写一些重复代码显得很冗余。然后查询解决方案时发现了@ControllerAdvice这......