Java学习路线-33:反射与Annotation
【摘要】 第27 章 : 反射与Annotation
120 反射取得Annotation信息
JDK > 1.5
不同的Annotation 有他的存在范围
public enum RetentionPolicy { SOURCE, CLASS, RUNTIME
}
123456
import java.lang.annotation.Annotation;
...
第27 章 : 反射与Annotation
120 反射取得Annotation信息
JDK > 1.5
不同的Annotation 有他的存在范围
public enum RetentionPolicy { SOURCE, CLASS, RUNTIME
}
- 1
- 2
- 3
- 4
- 5
- 6
import java.lang.annotation.Annotation;
@Deprecated
class Person{
}
class Demo{ public static void main(String[] args) { Annotation[] annotations = Person.class.getAnnotations(); for (Annotation a: annotations){ System.out.println(a); // @java.lang.Deprecated() } }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
121 自定义Annotation
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
// 定义运行时策略
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{ public String name(); public int age() default 23; // 设置默认值
}
class Message { @MyAnnotation(name="Tom") public void send(String msg){ System.out.println(msg); }
}
class Demo{ public static void main(String[] args) throws Exception { Method method = Message.class.getDeclaredMethod("send", String.class); MyAnnotation annotation = method.getAnnotation(MyAnnotation.class); String name = annotation.name(); // Tom int age = annotation.age(); // 23 String msg = String.format("[%s] %s ", name, age); method.invoke(Message.class.getDeclaredConstructor().newInstance(), msg); // [Tom] 23 }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
122 工厂设计模式与Annotation整合
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
// 消息接口
interface IMessage { public void send(String msg);
}
// 消息实现类
class MessageImpl implements IMessage { public void send(String msg) { System.out.println("消息发送"); }
}
// 动态代理类
class MessageProxy implements InvocationHandler { private Object target; public Object bind(Object target) { this.target = target; return Proxy.newProxyInstance( target.getClass().getClassLoader(), target.getClass().getInterfaces(), this ); } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object obj = null; if (this.connect()) { obj = method.invoke(this.target, args); } this.close(); return obj; } public boolean connect() { System.out.println("打开连接"); return true; } public void close() { System.out.println("关闭连接"); }
}
// 工厂类
class Factory { private Factory() { } public static <T> T getInstance(Class<T> clazz) { try { return (T) new MessageProxy().bind(clazz.getDeclaredConstructor().newInstance()); } catch (Exception e) { e.printStackTrace(); return null; } }
}
@Retention(RetentionPolicy.RUNTIME)
@interface UseMessage { public Class<?> clazz();
}
// 利用注解实现类的使用
@UseMessage(clazz = MessageImpl.class)
class MessageService { private IMessage message; public MessageService() { UseMessage use = MessageService.class.getAnnotation(UseMessage.class); this.message = (IMessage) Factory.getInstance(use.clazz()); } public void send(String msg) { this.message.send(msg); }
}
class Demo { public static void main(String[] args) { MessageService message = new MessageService(); message.send("发送消息"); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。
原文链接:pengshiyu.blog.csdn.net/article/details/103789433
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)