静态代理
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
|
public interface IUserDao { void save(); }
public class UserDao implements IUserDao { public void save() { System.out.println("----已经保存数据!----"); } }
public class UserDaoProxy implements IUserDao{ private IUserDao target; public UserDaoProxy(IUserDao target){ this.target=target; }
public void save() { System.out.println("开始事务..."); target.save(); System.out.println("提交事务..."); } }
public class App { public static void main(String[] args) { UserDao target = new UserDao();
UserDaoProxy proxy = new UserDaoProxy(target);
proxy.save(); } }
|
jdk动态代理
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
|
public class ProxyFactory{
private Object target; public ProxyFactory(Object target){ this.target=target; }
public Object getProxyInstance(){ return Proxy.newProxyInstance( target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("开始事务2"); Object returnValue = method.invoke(target, args); System.out.println("提交事务2"); return returnValue; } } ); }
}
public class App { public static void main(String[] args) { IUserDao target = new UserDao(); System.out.println(target.getClass());
IUserDao proxy = (IUserDao) new ProxyFactory(target).getProxyInstance(); System.out.println(proxy.getClass());
proxy.save(); } }
|
cglib动态代理
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
|
public class UserDao {
public void save() { System.out.println("----已经保存数据!----"); } }
public class ProxyFactory implements MethodInterceptor{ private Object target;
public ProxyFactory(Object target) { this.target = target; }
public Object getProxyInstance(){ Enhancer en = new Enhancer(); en.setSuperclass(target.getClass()); en.setCallback(this); return en.create();
}
@Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { System.out.println("开始事务...");
Object returnValue = method.invoke(target, args);
System.out.println("提交事务...");
return returnValue; } }
public class App {
@Test public void test(){ UserDao target = new UserDao();
UserDao proxy = (UserDao)new ProxyFactory(target).getProxyInstance();
proxy.save(); } }
|
自动选择代理
1 2 3 4 5 6 7 8 9 10 11 12
|
public interface IProxy {
Object getProxyInstance(); }
|
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
|
public class NoneProxy implements InvocationHandler, IProxy {
private final Object target;
public NoneProxy(Object target) { this.target = target; }
@Override public Object getProxyInstance() { return this.target; }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return method.invoke(proxy, args); } }
|
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
|
public class DynamicProxy implements InvocationHandler, IProxy {
private final Logger log = LoggerFactory.getLogger(getClass());
private final Object target;
public DynamicProxy(Object target) { this.target = target; }
@Override public Object getProxyInstance() { return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), target.getClass().getInterfaces(), this); }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { log.info("JDK动态代理-前置处理"); Object invoke = method.invoke(target, args); log.info("JDK动态代理-后置处理"); return invoke; } }
|
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
|
public class CglibProxy implements MethodInterceptor, IProxy{
private final Logger log = LoggerFactory.getLogger(getClass());
private final Object target;
public CglibProxy(Object target) { this.target = target; }
@Override public Object getProxyInstance() { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(target.getClass()); enhancer.setCallback(this); return enhancer.create(); }
@Override public Object intercept(Object o, Method method, Object[] args, MethodProxy proxy) throws Throwable { log.info("Cglib动态代理-前置处理"); Object invoke = method.invoke(target, args); log.info("Cglib动态代理-后置处理"); return invoke; } }
|
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
|
public final class AutoProxy {
public AutoProxy() { }
@SneakyThrows @SuppressWarnings("all") public static <T> T getProxy(@NonNull final Class<T> targetClass) { if (targetClass.isInterface()) { throw new IllegalArgumentException("不支持接口类型的参数"); } T target = targetClass.getDeclaredConstructor().newInstance(); if(Objects.isNull(target)) { return (T) new NoneProxy(target).getProxyInstance(); } if (targetClass.getInterfaces().length > 0) { return (T) new DynamicProxy(target).getProxyInstance(); } return (T) new CglibProxy(target).getProxyInstance(); } }
|
单元测试
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
| class AutoProxyTest {
@Test public void p1() { IUserService proxy = AutoProxy.getProxy(UserService.class); proxy.sayHello("当我遇上你"); }
@Test public void p2() { RoleService proxy = AutoProxy.getProxy(RoleService.class); proxy.hasRole("admin"); }
}
|
最后
本文到此结束,感谢阅读。如果您觉得不错,请关注公众号【当我遇上你】,您的支持是我写作的最大动力。