JdkDynamicAopProxy.java 文件源码

java
阅读 22 收藏 0 点赞 0 评论 0

项目:toy-spring 作者:
/**
 * InvocationHandler 接口中的 invoke 方法具体实现,封装了具体的代理逻辑
 *
 * @param proxy
 * @param method
 * @param args
 * @return 代理方法或原方法的返回值
 * @throws Throwable
 */
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    MethodMatcher methodMatcher = advised.getMethodMatcher();

    // 使用方法匹配器 methodMatcher 测试 bean 中原始方法 method 是否符合匹配规则
    if (methodMatcher != null && methodMatcher.matchers(method, advised.getTargetSource().getTargetClass())) {

        // 获取 Advice。MethodInterceptor 的父接口继承了 Advice
        MethodInterceptor methodInterceptor = advised.getMethodInterceptor();

        // 将 bean 的原始 method 封装成 MethodInvocation 实现类对象,
        // 将生成的对象传给 Adivce 实现类对象,执行通知逻辑
        return methodInterceptor.invoke(
                new ReflectiveMethodInvocation(advised.getTargetSource().getTarget(), method, args));
    } else {
        // 当前 method 不符合匹配规则,直接调用 bean 中的原始 method
        return method.invoke(advised.getTargetSource().getTarget(), args);
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号