JDK 的基于接口的动态代理: 实现同一接口 利用proxy 和 newProxyInstance , invoke 实现
========MyJdkProxy========public class MyJdkProxy implements InvocationHandler{ private UserDao userDao; public MyJdkProxy(UserDao userDao){ this.userDao = userDao; } public Object createProxy(){ Object proxy = Proxy.newProxyInstance(userDao.getClass().getClassLoader(),userDao.getClass().getInterfaces(),this); return proxy; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if("save".equals(method.getName())){ System.out.println("权限校验..."); return method.invoke(userDao,args); } return method.invoke(userDao,args); }
CGLIB生成的动态代理: 继承这个目标类生成一个子类
通过 Enhancer实现 setSuperclass(对应类) enhancer.create()实现
========MyCglibPorxy========public class MyCglibPorxy implements MethodInterceptor { private ProdectDao prodectDao; public MyCglibPorxy(ProdectDao prodectDao){ this.prodectDao=prodectDao; } public Object createProxy(){
Original: https://www.cnblogs.com/lzbbbb/p/16518979.html
Author: 小北呦
Title: springAOP的动态代理的原理
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/574663/
转载文章受原作者版权保护。转载请注明原作者出处!