萤火小屋

优律的知识库

  • 首页
  • 归档
  • 分类
  • 标签
  • 留言
  • 关于

  • 搜索
消息队列 RabbitMQ Redis 双指针 力扣 动态代理 Git YAML SpringBoot SpringMVC 回溯算法 分治算法 归并排序 快排 手撕 事务 MySQL索引 MySQL 小技巧 Spring Framework Spring 动态规划 Linux Android 贪心算法 操作系统 进程调度模拟 IPv6 数据库 计算机组成原理 计算机基础 栈 Java 静态路由 路由器 交换机 数字通信 网络工程 计算机网络 Web http 大学学习技巧 程序设计 算法

Java的注解

发表于 2021-08-15 | 分类于 Java SE | 0 | 阅读次数 207

概念介绍

Annotation(注解)是Java提供的一种对元程序中元素关联信息和元数据(metadata)的途径和方法。Annatation(注解)是一个接口,程序可以通过反射来获取指定程序中元素的Annotation对象,然后通过该Annotation对象来获取注解中的元数据信息。

四种标准元注解

元注解的作用是负责注解其他注解。Java5.0定义了4个标准的meta-annotation类型,它们被用来提供对其它annotation类型作说明。

@Target修饰的对象范围

@Target说明了Annotation所修饰的对象范围:Annotation可被用于packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch 参数)。在Annotation类型的声明中使用了target可更加明晰其修饰的目标。

@Retention定义被保留的时间长短

Retention 定义了该 Annotation 被保留的时间长短:表示需要在什么级别保存注解信息,用于描述注解的生命周期(即:被描述的注解在什么范围内有效),取值(RetentionPoicy)由:

  • SOURCE:在源文件中有效(即源文件保留)
  • CLASS:在class文件中有效(即class保留)
  • RUNTIME:在运行时有效(即运行时保留)

@Documented 描述-javadoc

@Documented用于描述其它类型的annotation应该被作为被标注的程序成员的公共 API,因此可以被例如javadoc此类的工具文档化。

@Inherited阐述了某个被标注的类型是被继承的

@Inherited元注解是一个标记注解,@Inherited阐述了某个被标注的类型是被继承的。如果一个使用了@Inherited 修饰的annotation类型被用于一个class,则这个annotation将被用于该class的子类。

注解处理器

如果没有用来读取注解的方法和工作,那么注解也就不会比注释更有用处了。使用注解的过程中,很重要的一部分就是创建于使用注解处理器。JavaSE5扩展了反射机制的API,以帮助程序员快速的构造自定义注解处理器。下面实现一个注解处理器。

  1. 定义一个注解UserConfigure.java
package cc.fireflyhut.test.annotation;

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

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface UserConfigure {
	public String username() default "XinBa";
	public String password() default "123456";
	public String email() default "[email protected]";	
}
  1. 定义一个使用注解的类User.java
package cc.fireflyhut.test;

import cc.fireflyhut.test.annotation.UserConfigure;

public class User {
	@UserConfigure(username = "Youlo", password = "654321", email = "[email protected]")
	private String userInfo;

	public String getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(String userInfo) {
		this.userInfo = userInfo;
	}
}

  1. 定义一个注解处理器
package cc.fireflyhut.test.handle;

import java.lang.reflect.Field;
import cc.fireflyhut.test.annotation.UserConfigure;

public class UserInfoAnnotationHandle {
	public static void useTheAnnotation(Class<?> clazz) {
		Field[] fields = clazz.getDeclaredFields();  // 使用反射获取注解
		for (Field field : fields) {
			if (field.isAnnotationPresent(UserConfigure.class)) {
				UserConfigure userConfigure = field.getAnnotation(UserConfigure.class);
				System.out.println("用户名:" + userConfigure.username() + 
						"  密码:" + userConfigure.password() + 
						"  邮箱:" + userConfigure.email());
			}
		}
	}
}
  1. 定义一个有主方法的类作为入口类
package cc.fireflyhut.test;

import cc.fireflyhut.test.handle.UserInfoAnnotationHandle;

public class Main {
	public static void main(String[] args) {
		UserInfoAnnotationHandle.useTheAnnotation(User.class);
	}
}

运行主方法,在控制台打印

image.png

# Java
Java的异常类
Spring学习笔记-SpringMVC
  • 文章目录
  • 站点概览
优律

优律

优律的知识库

83 日志
20 分类
44 标签
E-mail Twitter Instagram
Links
  • CZLisyx - 浮生志
  • Vedfolnir
0%
© 2019 — 2023 萤火小屋——优律的博客网站
网站已勉强运行 
Halo博客系统技术支持