当前位置: 首页> 娱乐> 八卦 > 云服务器多少钱一台_广州网站建设公司电话_快速排名网站_360搜索网址是多少

云服务器多少钱一台_广州网站建设公司电话_快速排名网站_360搜索网址是多少

时间:2025/10/1 2:18:25来源:https://blog.csdn.net/yangguo52047/article/details/129248517 浏览次数:0次
云服务器多少钱一台_广州网站建设公司电话_快速排名网站_360搜索网址是多少

一、目标

本章我们简化MethodInvocation的逻辑,将cglib与jdk动态代理的内容,统一封装到ReflectiveMethodInvocation

二、实现

简化cglib实现

public class Cglib2AopProxy implements AopProxy {private final AdvisedSupport advised;public Cglib2AopProxy(AdvisedSupport advised) {this.advised = advised;}@Overridepublic Object getProxy() {Enhancer enhancer = new Enhancer();enhancer.setSuperclass(advised.getTargetSource().getTarget().getClass());enhancer.setInterfaces(advised.getTargetSource().getTargetClass());enhancer.setCallback(new DynamicAdvisedInterceptor(advised));return enhancer.create();}private static class DynamicAdvisedInterceptor implements MethodInterceptor {private final AdvisedSupport advised;public DynamicAdvisedInterceptor(AdvisedSupport advised) {this.advised = advised;}@Overridepublic Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {// 创建方法调用链ReflectiveMethodInvocation methodInvocation = new ReflectiveMethodInvocation(advised.getTargetSource().getTarget(), method, objects, advised.getAdvisors());// 执行拦截器链return methodInvocation.proceed();}}}

简化jdk实现

public class JdkDynamicAopProxy implements AopProxy, InvocationHandler {private final AdvisedSupport advised;public JdkDynamicAopProxy(AdvisedSupport advised) {this.advised = advised;}@Overridepublic Object getProxy() {return Proxy.newProxyInstance(advised.getTargetSource().getTarget().getClass().getClassLoader(),advised.getTargetSource().getTargetClass(),this);}@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {// 创建方法调用链ReflectiveMethodInvocation methodInvocation = new ReflectiveMethodInvocation(advised.getTargetSource().getTarget(), method, args, advised.getAdvisors());// 执行拦截器链return methodInvocation.proceed();}}

将proceed代码,统一放置到ReflectiveMethodInvocation

public class ReflectiveMethodInvocation implements MethodInvocation {// 目标对象protected final Object target;// 方法protected final Method method;// 入参protected final Object[] arguments;// 当前执行的通知器索引private int currentInterceptorIndex = -1;// 通知器列表private final List<Advisor> advisors;public ReflectiveMethodInvocation(Object target, Method method, Object[] arguments, List<Advisor> advisors) {this.target = target;this.method = method;this.arguments = arguments;this.advisors = advisors;}@Overridepublic Method getMethod() {return method;}@Overridepublic Object[] getArguments() {return arguments;}@Overridepublic Object proceed() throws Throwable {// 如果所有通知器都执行完毕,调用目标方法if (this.currentInterceptorIndex == this.advisors.size() - 1) {return this.method.invoke(this.target, this.arguments);}// 获取下一个通知器Advisor advisor = this.advisors.get(++this.currentInterceptorIndex);// 判断方法是否匹配切点if (advisor.getPointcut().getMethodMatcher().matches(this.method, this.target.getClass())) {// 将 Advice 包装成 MethodInterceptorMethodInterceptor interceptor = getInterceptor(advisor);// 执行拦截器逻辑return interceptor.invoke(this);}// 如果方法不匹配,跳过当前通知器,继续执行下一个return proceed();}@Overridepublic Object getThis() {return target;}@Overridepublic AccessibleObject getStaticPart() {return method;}// 将 Advice 包装成 MethodInterceptorprotected MethodInterceptor getInterceptor(Advisor advisor) {if (advisor.getAdvice() instanceof MethodInterceptor) {return (MethodInterceptor) advisor.getAdvice();}// 如果是 MethodBeforeAdvice,使用适配器包装if (advisor.getAdvice() instanceof MethodBeforeAdvice) {return new MethodBeforeAdviceInterceptor((MethodBeforeAdvice) advisor.getAdvice());}throw new UnsupportedOperationException("Advice type not supported: " + advisor.getAdvice().getClass());}
}
关键字:云服务器多少钱一台_广州网站建设公司电话_快速排名网站_360搜索网址是多少

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: