1.自定义注解
```java
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })//定义在类上必须是TYPE ,如果是属性上FIELD
@Documented
public @interface Cacheable {
public String prefix() default "";
public String key() default "";
public int expire() default 1800;
}
2.类上注解
```java
@Cacheable(prefix = "yang", key = "money")
public class User {
private String usernae;
}
3.测试
```java
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Map;
public class TestMain {
public static void main(String[] args) throws Exception {
User user=new User();
//获取注解上的值
Cacheable annotation = user.getClass().getAnnotation(Cacheable.class);
System.out.println(annotation.prefix()+"\t"+annotation.key()+"\t"+annotation.expire());
//设置注解上的值
InvocationHandler cacheable = Proxy.getInvocationHandler(annotation);//获取注解
Field field = cacheable.getClass().getDeclaredField("memberValues");
field.setAccessible(true);
Map<String,Object> map= (Map<String, Object>) field.get(cacheable);
map.put("prefix","jie");
map.put("key","yang");
map.put("expire",520);
System.out.println(annotation.prefix()+"\t"+annotation.key()+"\t"+annotation.expire());
}
}
如果是全局属性上,先获取属性,再获取属性上的注解