一、核心代码
- 动态代理类
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 void close() {
System.out.println("【代理类】关闭连接");
}
public boolean connect() {
System.out.println("【代理类】连接成功!");
return true;
}
@Override
public Object invoke(Object proxy , Method method , Object[] args) throws Throwable {
Object returnObj = null;
try {
if (connect()) {
returnObj = method.invoke(this.target , args);
}
} finally {
close();
}
return returnObj;
}
}
- 动态工厂类
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 UserMessage {
public Class<?> clazz();
}
@UserMessage(clazz = NetMessage.class)
class MessageService{
private IMessage message;
public MessageService() throws NoSuchMethodException {
UserMessage anno = MessageService.class.getAnnotation(UserMessage.class);
this.message=(IMessage) Factory.getInstance(anno.clazz());
}
public void spend(String msg) {
this.message.spend(msg);
}
}
二、完整代码
package reflect.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class FactoryProxyAnnotation {
public static void main(String[] args) throws NoSuchMethodException {
MessageService messageService = new MessageService();
messageService.spend("xlu");
}
}
interface IMessage {
public void spend(String msg);
}
@Retention(RetentionPolicy.RUNTIME)
@interface UserMessage {
public Class<?> clazz();
}
@UserMessage(clazz = NetMessage.class)
class MessageService{
private IMessage message;
public MessageService() throws NoSuchMethodException {
UserMessage anno = MessageService.class.getAnnotation(UserMessage.class);
this.message=(IMessage) Factory.getInstance(anno.clazz());
}
public void spend(String msg) {
this.message.spend(msg);
}
}
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;
}
}
class Message implements IMessage {
public Message() {
}
@Override
public void spend(String msg) {
System.out.println("【普通消息】"+msg);
}
}
class NetMessage implements IMessage{
public NetMessage() {
}
@Override
public void spend(String msg) {
System.out.println("【网络消息】"+msg);
}
}
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 void close() {
System.out.println("【代理类】关闭连接");
}
public boolean connect() {
System.out.println("【代理类】连接成功!");
return true;
}
@Override
public Object invoke(Object proxy , Method method , Object[] args) throws Throwable {
Object returnObj = null;
try {
if (connect()) {
returnObj = method.invoke(this.target , args);
}
} finally {
close();
}
return returnObj;
}
}