package com.annotation.test;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PACKAGE;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author Administrator
* 自定义用户注解
*/
//注解定义
@Documented
//注解生命周期
@Retention(RetentionPolicy.RUNTIME)
//注解的作用范围 设定作用构造器 字段 方法 包等
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
public @interface UserAnnotation {
//定义注解 的基本的
String name();
//定义属性值
String password();
}
//
package com.annotation.test;
@UserAnnotation(name="zhangsan", password = "123456")
public class TestMyAnnotation {
public static void main(String[] args) {
//使用反射进行参数注解
UserAnnotation annotation=TestMyAnnotation.class.getAnnotation(UserAnnotation.class);
//测试注解
System.out.println(annotation.name()+"__"+annotation.password());
}
}
//运行结果
zhangsan__123456