Annotation注解 (一)

2024-08-30 03:58
文章标签 注解 annotation

本文主要是介绍Annotation注解 (一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Annotation



自定义的Annotation注解:

package com.annotation.java;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)  //限定 myAnnotation只能用在method方法上。
public @interface myAnnotation {String value2() default "zhangsan";String value3();int in4();
}

@Retention(RetentionPolicy.RUNTIME)
Retation注释可以修饰注释,RetentionPolicy是枚举类型。

Quote From JDK:@Retention Indicates how long annotations with the annotated type are to
 * be retained. @Retention意思是被它修饰的注释保持多久,有三种枚举策略RetentionPolicy可选:CLASS  SOURCE  RUNTIME

这里的RetentionPolicy定义为 RUNTIME类型,表明myAnnotation是记录在Class文件中,并可被JVM在运行期获取,以反射的方式被读取。

@Target(ElementType.METHOD)  //限定 myAnnotation只能用在method方法上。
Target注释表明哪种类型(函数METHOD、构造器CONSTRUCTOR、类/接口/枚举 TYPE)可以被这个Target修饰的注释修饰。

这里ElementType枚举设定为Method ,表明myAnnotation只能修饰函数method。


下面自定义一个类中的一个方法被 myAnnotation修饰。

package com.annotation.java;import java.util.ArrayList;import java.util.List;//@myAnnotation(value3="class",in4=10)public class UsingMyAnnotation {@SuppressWarnings("unchecked") @Deprecated@myAnnotation(value3 = "method_print", in4 = 90)public void print(String str) {List list = new ArrayList();System.out.println("method: " + str);}
}
可以看出,在print函数上有三个注释修饰,第三个为自定义的@myAnnotation(value3 = "method_print", in4 = 90) 并给其赋值。


下面将利用反射机制来获取该方法print上的Annotation信息。

package com.annotation.java;import java.lang.annotation.Annotation;
import java.lang.reflect.Method;public class CheckMyAnnotation {public static void main(String[] args) throws Exception {Class<?> classtype = UsingMyAnnotation.class;//利用反射来获取UsingMyAnnotation类对应的运行中的Class对象UsingMyAnnotation objMyAnnotation = (UsingMyAnnotation) classtype 反射来new一个实例.newInstance();Method methodOfAnnotation = classtype.getMethod("print",new Class[] { String.class });  利用反射来获取print方法对应的Method对象 if (methodOfAnnotation.isAnnotationPresent(myAnnotation.class)) { //isAnnotationPresent是Method父类AccessibleObject的函数,Feild、Construtor也是methodOfAnnotation.invoke(objMyAnnotation,new Object[] { "success!" });myAnnotation my = methodOfAnnotation.getAnnotation(myAnnotation.class);//获取该方法上的myAnnotation注解System.out.println(my.value3() + my.in4());}Annotation[] mys = methodOfAnnotation.getAnnotations();//获取该方法上的的所有RetentionPolicy为RUNTIME的注解for (Annotation annotation : mys) {System.out.println(annotation.annotationType().getName());}}
}


打印结果:



注意:打印出java.lang.Deprecated 而没有打印出SuppressWarnings注解,是因为 前者的 是RetentionPolicy.RUNTIME 而后者是:RetentionPolicy.SOURCE

这篇关于Annotation注解 (一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1119727

相关文章

Spring中@Lazy注解的使用技巧与实例解析

《Spring中@Lazy注解的使用技巧与实例解析》@Lazy注解在Spring框架中用于延迟Bean的初始化,优化应用启动性能,它不仅适用于@Bean和@Component,还可以用于注入点,通过将... 目录一、@Lazy注解的作用(一)延迟Bean的初始化(二)与@Autowired结合使用二、实例解

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

Spring排序机制之接口与注解的使用方法

《Spring排序机制之接口与注解的使用方法》本文介绍了Spring中多种排序机制,包括Ordered接口、PriorityOrdered接口、@Order注解和@Priority注解,提供了详细示例... 目录一、Spring 排序的需求场景二、Spring 中的排序机制1、Ordered 接口2、Pri

Idea实现接口的方法上无法添加@Override注解的解决方案

《Idea实现接口的方法上无法添加@Override注解的解决方案》文章介绍了在IDEA中实现接口方法时无法添加@Override注解的问题及其解决方法,主要步骤包括更改项目结构中的Languagel... 目录Idea实现接China编程口的方法上无法添加@javascriptOverride注解错误原因解决方

Java中基于注解的代码生成工具MapStruct映射使用详解

《Java中基于注解的代码生成工具MapStruct映射使用详解》MapStruct作为一个基于注解的代码生成工具,为我们提供了一种更加优雅、高效的解决方案,本文主要为大家介绍了它的具体使用,感兴趣... 目录介绍优缺点优点缺点核心注解及详细使用语法说明@Mapper@Mapping@Mappings@Co

Java中注解与元数据示例详解

《Java中注解与元数据示例详解》Java注解和元数据是编程中重要的概念,用于描述程序元素的属性和用途,:本文主要介绍Java中注解与元数据的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参... 目录一、引言二、元数据的概念2.1 定义2.2 作用三、Java 注解的基础3.1 注解的定义3.2 内

SpringBoot使用注解集成Redis缓存的示例代码

《SpringBoot使用注解集成Redis缓存的示例代码》:本文主要介绍在SpringBoot中使用注解集成Redis缓存的步骤,包括添加依赖、创建相关配置类、需要缓存数据的类(Tes... 目录一、创建 Caching 配置类二、创建需要缓存数据的类三、测试方法Spring Boot 熟悉后,集成一个外

使用@Slf4j注解,log.info()无法使用问题

《使用@Slf4j注解,log.info()无法使用问题》在使用Lombok的@Slf4j注解打印日志时遇到问题,通过降低Lombok版本(从1.18.x降至1.16.10)解决了问题... 目录@Slf4androidj注解,log.info()无法使用问题最后解决总结@Slf4j注解,log.info(

spring—使用注解配置Bean

从Spring2.5开始,出现了注解装配JavaBean的新方式。注解可以减少代码的开发量,spring提供了丰富的注解功能,现在项目中注解的方式使用的也越来越多了。   ** 开启注解扫描          Spring容器默认是禁用注解配置的。打开注解扫描的方式主要有两种: <context:component-scan>组件扫描和<context:annotation

Spring Boot 注解探秘:HTTP 请求的魅力之旅

在SpringBoot应用开发中,处理Http请求是一项基础且重要的任务。Spring Boot通过提供一系列丰富的注解极大地简化了这一过程,使得定义请求处理器和路由变得更加直观与便捷。这些注解不仅帮助开发者清晰地定义不同类型的HTTP请求如何被处理,同时也提升了代码的可读性和维护性。 一、@RequestMapping @RequestMapping用于将特定的HTTP请求映射到特定的方法上