自定义Annotation

本文详细介绍了Java注解的定义及使用方法,包括注解的创建规则、目标指定、生命周期设置等核心概念,并提供了类、构造方法、方法及属性级别的注解示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.编写注解类的规则

       (1)Annotation类型定义为@interface

                 所有的Annotation会自动集成java.lang.Annotation这一接口,并且不能再去继承别的类或者接口

       (2)Annotation成员只有public或者默认这两个修饰符

       (3)参数成员只能用8种原生数据类型和String、Enum、Class、Annotation等数据类型,以及这些类型的数组

       (4)要获取注解信息,只能通过反射


2.指定注解类的目标、生命周期、是否子类继承父类注解

       (1)指定注解的目标

                 @Target表示注解的目标,可以选取的值有以下几种

ElemenetType.CONSTRUCTOR     构造器声明
ElemenetType.FIELD           属性声明(包括 enum 实例) 
ElemenetType.LOCAL_VARIABLE  局部变量声明 
ElemenetType.METHOD          方法声明 
ElemenetType.PACKAGE         包声明 
ElemenetType.PARAMETER       参数声明 
ElemenetType.TYPE            类,接口(包括注解类型)或enum声明

       (2)指定注解的生命周期

                 @Retention表示注解的生命周期,可选的值有以下几种

RetentionPolicy.SOURCE        注解将被编译器丢弃 
RetentionPolicy.CLASS         注解在class文件中可用,但会被VM丢弃 
RetentionPolicy.RUNTIME       VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息

       (3)@Document表示将这个注解包含在javadoc中

       (4)@Inherited表示允许子类继承父类中的注解

2.注解使用

       (1)类注解

@Retention (RetentionPolicy.RUNTIME)
@Target (ElementType.TYPE)               //表示类注解

public @interface MyClassAnnotation 
{
	String uri();
	String desc();
}

       (2)默认构造方法注解
@Retention (RetentionPolicy.RUNTIME)
@Target (ElementType.CONSTRUCTOR)        //表示构造方法注解

public @interface MyConstructorAnnotation 
{
	String uri();
	String desc();
}

       (3)方法注解

@Retention (RetentionPolicy.RUNTIME)
@Target (ElementType.METHOD)

public @interface MyMethodAnnotation 
{
	String uri();
	String desc();
}

       (4)属性注解

@Retention (RetentionPolicy.RUNTIME)
@Target (ElementType.FIELD)

public @interface MyFieldAnnotation 
{
	String uri();
	String desc();
}

       (5)测试

@MyClassAnnotation (uri="mysample",desc="the class annotation")
public class Zhujie 
{
	@MyFieldAnnotation (uri="mysample",desc="the field annotation")
	private String id;
	
	@MyConstructorAnnotation (uri="mysample",desc="the constructor annotation")
	public Zhujie() 
	{
	}
	
	@MyMethodAnnotation (uri="mysample",desc="the method annotation")
	public String getId()
	{
		return id;
	}
}

	@org.junit.Test
	public void test() throws SecurityException, NoSuchMethodException, NoSuchFieldException 
	{
		Class<Zhujie> clazz = Zhujie.class;
		MyClassAnnotation classAnnotation = clazz.getAnnotation(MyClassAnnotation.class);
		System.out.println("class's uri = "+classAnnotation.uri()+"; desc = "+classAnnotation.desc());
		
		Constructor<Zhujie> constructor = clazz.getConstructor();
		MyConstructorAnnotation constructorAnnotation = constructor.getAnnotation(MyConstructorAnnotation.class);
		System.out.println("constructor's uri = "+constructorAnnotation.uri()+"; desc = "+constructorAnnotation.desc());
		
		Method method = clazz.getMethod("getId");
		MyMethodAnnotation methodAnnotation = method.getAnnotation(MyMethodAnnotation.class);
		System.out.println("method's uri = "+methodAnnotation.uri()+"; desc = "+methodAnnotation.desc());
		
		Field field = clazz.getDeclaredField("id");
		MyFieldAnnotation fieldAnnotation = field.getAnnotation(MyFieldAnnotation.class);
		System.out.println("field's uri = "+fieldAnnotation.uri()+"; desc = "+fieldAnnotation.desc());
	}
               结果为

class's uri = mysample; desc = the class annotation
constructor's uri = mysample; desc = the constructor annotation
method's uri = mysample; desc = the method annotation
field's uri = mysample; desc = the field annotation

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值