自定义注解annotation及用法

1.注解的定义

@Documented:一个简单的Annotations标记注解,表示是否将注解信息添加在java文档中。

@Retention(RetentionPolicy.RUNTIME):

RetentionPolicy.SOURCE – 在编译阶段丢弃。这些注解在编译结束之后就不再有任何意义,所以它们不会写入字节码。@Override, @SuppressWarnings都属于这类注解。
RetentionPolicy.CLASS – 在类加载的时候丢弃。在字节码文件的处理中有用。注解默认使用这种方式。
RetentionPolicy.RUNTIME– 始终不会丢弃,运行期也保留该注解,因此可以使用反射机制读取该注解的信息。我们自定义的注解通常使用这种方式。

@Target(ElementType.FIELD):单个参数
@Target({ElementType.FIELD,ElementType.METHOD}):多个参数用大括号
 ElementType.TYPE:用于描述类、接口或enum声明
 ElementType.FIELD:用于描述实例变量
 ElementType.METHOD
 ElementType.PARAMETER
 ElementType.CONSTRUCTOR
 ElementType.LOCAL_VARIABLE
 ElementType.ANNOTATION_TYPE 另一个注释
 ElementType.PACKAGE 用于记录java文件的package信息

@Inherited:
 Inherited作用是,使用此注解声明出来的自定义注解,在使用此自定义注解时,如果注解在类上面时,子类会自动继承此注解,
 否则的话,子类不会继承此注解。这里一定要记住,使用Inherited声明出来的注解,只有在类上使用时才会有效,对方法,属性等其他无效。
 

package com.order.orderSystem.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**一个简单的Annotations标记注解,表示是否将注解信息添加在java文档中。*/
@Documented
/**
 * RetentionPolicy.SOURCE – 在编译阶段丢弃。这些注解在编译结束之后就不再有任何意义,所以它们不会写入字节码。@Override, @SuppressWarnings都属于这类注解。
 * RetentionPolicy.CLASS – 在类加载的时候丢弃。在字节码文件的处理中有用。注解默认使用这种方式。
 * RetentionPolicy.RUNTIME– 始终不会丢弃,运行期也保留该注解,因此可以使用反射机制读取该注解的信息。我们自定义的注解通常使用这种方式。
 */
@Retention(RetentionPolicy.RUNTIME)
/**
 * ElementType.TYPE:用于描述类、接口或enum声明
 * ElementType.FIELD:用于描述实例变量
 * ElementType.METHOD
 * ElementType.PARAMETER
 * ElementType.CONSTRUCTOR
 * ElementType.LOCAL_VARIABLE
 * ElementType.ANNOTATION_TYPE 另一个注释
 * ElementType.PACKAGE 用于记录java文件的package信息
 */
@Target({ElementType.FIELD,ElementType.METHOD})
/**
 * Inherited作用是,使用此注解声明出来的自定义注解,在使用此自定义注解时,如果注解在类上面时,子类会自动继承此注解,
 * 否则的话,子类不会继承此注解。这里一定要记住,使用Inherited声明出来的注解,只有在类上使用时才会有效,对方法,属性等其他无效。
 */
@Inherited
public @interface FieldAnnotation {
	
	public enum PASSWORD {LOW, MEDIUM, HIGH}
	
	PASSWORD value() default PASSWORD.LOW;
	
	String type();
	
	
	
	
}

2.注解的应用

注解方法有default则可以不传值,多个值方法@FieldAnnotation(value=PASSWORD.MEDIUM,type="password")

package com.order.orderSystem.bean;

import com.order.orderSystem.annotation.FieldAnnotation;
import com.order.orderSystem.annotation.FieldAnnotation.PASSWORD;

public class UserInfo {

	@FieldAnnotation(value=PASSWORD.MEDIUM,type="password")
	private String password;
	
	@FieldAnnotation(type="用户信息")
	private String getUser(String str){
		return str;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}

测试用例,用反射获取注解内容

package com.order.test;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.junit.Test;

import com.order.orderSystem.annotation.FieldAnnotation;
import com.order.orderSystem.bean.UserInfo;

public class AnnotationTest {

	@Test
	public void fieldTest() throws Exception, SecurityException{
		Class<UserInfo> clazz = UserInfo.class;
		Field field = clazz.getDeclaredField("password");
		FieldAnnotation fieldAnnotation = field.getAnnotation(FieldAnnotation.class);
		System.out.println("field注解:value:"+fieldAnnotation.value()+",type:"+fieldAnnotation.type());
		
		Method method = clazz.getDeclaredMethod("getUser", String.class);
		fieldAnnotation = method.getAnnotation(FieldAnnotation.class);
		System.out.println("method注解:value:"+fieldAnnotation.value()+",type:"+fieldAnnotation.type());
	}
}

猜你喜欢

转载自blog.csdn.net/mengda_lei/article/details/82054408