import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* 代理模式的类型较多,不同类型的代理模式有不同的优缺点,它们应用于不同的场合:
(1) 当客户端对象需要访问远程主机中的对象时可以使用远程代理。
(2) 当需要用一个消耗资源较少的对象来代表一个消耗资源较多的对象,从而降低系统开销、缩短运行时间时可以使用虚拟代理,例如一个对象需要很长时间才能完成加载时。
(3) 当需要为某一个被频繁访问的操作结果提供一个临时存储空间,以供多个客户端共享访问这些结果时可以使用缓冲代理。通过使用缓冲代理,系统无须在客户端每一次访问时都重新执行操作,只需直接从临时缓冲区获取操作结果即可。
(4) 当需要控制对一个对象的访问,为不同用户提供不同级别的访问权限时可以使用保护代理。
(5) 当需要为一个对象的访问(引用)提供一些额外的操作时可以使用智能引用代理。
*
*
* 王宇龙 015729
*/
public class TestProxy {
/**事务管理对象*/
static class TransactionManager{
public void beginTransaction() {
System.out.println("begin transaction");
}
public void commitTransaction() {
System.out.println( "commit transaction");
}
}
public static void main(String[] args) {
TransactionManager transactionManager =new TransactionManager();
//1.创建目标对象
final TargetService target=new TargetServiceImpl();
//2.创建代理对象(动态代理)
TargetService proxy=(TargetService)
Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
new InvocationHandler() {//要扩展的业务,借助此对象处理
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
//获取具体实现类中的方法,如无此步骤,则无法获取实现类的注解,只有接口的
Method targetmethod =
target.getClass()
.getDeclaredMethod(method.getName(),
method.getParameterTypes());
if(targetmethod.isAnnotationPresent(Transaction.class)) {
//1.扩展功能
transactionManager.beginTransaction();
//2.核心业务
Object result=targetmethod.invoke(target, args);
//3.扩展功能
transactionManager.commitTransaction();
return result;
}
System.out.println("没有注解");
return null;
}
});
//3.执行具体业务
proxy.saveObject();
}
}
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* 代理模式的类型较多,不同类型的代理模式有不同的优缺点,它们应用于不同的场合:
(1) 当客户端对象需要访问远程主机中的对象时可以使用远程代理。
(2) 当需要用一个消耗资源较少的对象来代表一个消耗资源较多的对象,从而降低系统开销、缩短运行时间时可以使用虚拟代理,例如一个对象需要很长时间才能完成加载时。
(3) 当需要为某一个被频繁访问的操作结果提供一个临时存储空间,以供多个客户端共享访问这些结果时可以使用缓冲代理。通过使用缓冲代理,系统无须在客户端每一次访问时都重新执行操作,只需直接从临时缓冲区获取操作结果即可。
(4) 当需要控制对一个对象的访问,为不同用户提供不同级别的访问权限时可以使用保护代理。
(5) 当需要为一个对象的访问(引用)提供一些额外的操作时可以使用智能引用代理。
*
*
* 王宇龙 015729
*/
public class TestProxy {
/**事务管理对象*/
static class TransactionManager{
public void beginTransaction() {
System.out.println("begin transaction");
}
public void commitTransaction() {
System.out.println( "commit transaction");
}
}
public static void main(String[] args) {
TransactionManager transactionManager =new TransactionManager();
//1.创建目标对象
final TargetService target=new TargetServiceImpl();
//2.创建代理对象(动态代理)
TargetService proxy=(TargetService)
Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
new InvocationHandler() {//要扩展的业务,借助此对象处理
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
//获取具体实现类中的方法,如无此步骤,则无法获取实现类的注解,只有接口的
Method targetmethod =
target.getClass()
.getDeclaredMethod(method.getName(),
method.getParameterTypes());
if(targetmethod.isAnnotationPresent(Transaction.class)) {
//1.扩展功能
transactionManager.beginTransaction();
//2.核心业务
Object result=targetmethod.invoke(target, args);
//3.扩展功能
transactionManager.commitTransaction();
return result;
}
System.out.println("没有注解");
return null;
}
});
//3.执行具体业务
proxy.saveObject();
}
}