精华内容
下载资源
问答
  • 这是目录jdk动态代理cglib动态代理cglib代理接口cglib代理实现类对比源码版权所有 jdk动态代理 jdk动态代理只可以代理接口,因为最后的实现类要继承Proxy并实现该接口 cglib动态代理 cglib既可代理接口又可以代理...

    jdk动态代理

    jdk动态代理只可以代理接口,因为最后的实现类要继承Proxy并实现该接口

    cglib动态代理

    cglib既可代理接口又可以代理实现类,那背后的源码是如何实现的呢?

    cglib代理接口

    1. 接口代码
    package com.tqy.document.reader.extention.demo1;
    
    /**
     * @author tengqingya
     * @create 2019-04-23 12:49
     */
    public interface TestInterface {
        String getHalloWorld();
    }
    
    
    1. MethodInterceptor代码
    package com.tqy.document.reader.extention.demo1;
    
    import net.sf.cglib.proxy.Enhancer;
    import net.sf.cglib.proxy.MethodInterceptor;
    import net.sf.cglib.proxy.MethodProxy;
    
    import java.lang.reflect.Method;
    
    /**
     * @author tengqingya
     * @create 2019-04-23 12:49
     */
    
    public class TestCGLib implements MethodInterceptor {
    
        public Object getInstance(Class claxx) {
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(claxx);
            // 回调方法
            enhancer.setCallback(this);
            // 创建代理对象
            return enhancer.create();
        }
    
        @Override
        public Object intercept( Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
    //        methodProxy.invokeSuper(o,objects);
            return method.getName();
        }
    }
    
    1. main函数
    package com.tqy.document.reader.extention.demo1;
    
    import net.sf.cglib.core.DebuggingClassWriter;
    
    /**
     * @author tengqingya
     * @create 2019-04-23 12:50
     */
    public class TestCGLibMain {
        public static void main(String[] args) {
            System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "D:\\gitCode\\mySpring\\DcumentReader\\src\\main\\java\\com\\tqy\\document\\reader\\extention\\demo1");
    
            TestCGLib testCGLib = new TestCGLib();
            TestInterface o = (TestInterface) testCGLib.getInstance(TestInterface.class);
            System.out.println(o.getHalloWorld());
        }
    }
    
    1. 运行结果
    CGLIB debugging enabled, writing to 'D:\gitCode\mySpring\DcumentReader\src\main\java\com\tqy\document\reader\extention\demo1'
    getHalloWorld
    
    1. 生成的源码
    //
    // Source code recreated from a .class file by IntelliJ IDEA
    // (powered by Fernflower decompiler)
    //
    
    package com.tqy.document.reader.extention.demo1;
    
    import java.lang.reflect.Method;
    import net.sf.cglib.core.ReflectUtils;
    import net.sf.cglib.core.Signature;
    import net.sf.cglib.proxy.Callback;
    import net.sf.cglib.proxy.Factory;
    import net.sf.cglib.proxy.MethodInterceptor;
    import net.sf.cglib.proxy.MethodProxy;
    
    public class TestInterface$$EnhancerByCGLIB$$72f42620 implements TestInterface, Factory {
        private boolean CGLIB$BOUND;
        private static final ThreadLocal CGLIB$THREAD_CALLBACKS;
        private static final Callback[] CGLIB$STATIC_CALLBACKS;
        private MethodInterceptor CGLIB$CALLBACK_0;
        private static final Method CGLIB$finalize$0$Method;
        private static final MethodProxy CGLIB$finalize$0$Proxy;
        private static final Object[] CGLIB$emptyArgs;
        private static final Method CGLIB$equals$1$Method;
        private static final MethodProxy CGLIB$equals$1$Proxy;
        private static final Method CGLIB$toString$2$Method;
        private static final MethodProxy CGLIB$toString$2$Proxy;
        private static final Method CGLIB$hashCode$3$Method;
        private static final MethodProxy CGLIB$hashCode$3$Proxy;
        private static final Method CGLIB$clone$4$Method;
        private static final MethodProxy CGLIB$clone$4$Proxy;
        private static final Method CGLIB$getHalloWorld$5$Method;
        private static final MethodProxy CGLIB$getHalloWorld$5$Proxy;
    
        static void CGLIB$STATICHOOK1() {
            CGLIB$THREAD_CALLBACKS = new ThreadLocal();
            CGLIB$emptyArgs = new Object[0];
            Class var0 = Class.forName("com.tqy.document.reader.extention.demo1.TestInterface$$EnhancerByCGLIB$$72f42620");
            Class var1;
            Method[] var10000 = ReflectUtils.findMethods(new String[]{"finalize", "()V", "equals", "(Ljava/lang/Object;)Z", "toString", "()Ljava/lang/String;", "hashCode", "()I", "clone", "()Ljava/lang/Object;"}, (var1 = Class.forName("java.lang.Object")).getDeclaredMethods());
            CGLIB$finalize$0$Method = var10000[0];
            CGLIB$finalize$0$Proxy = MethodProxy.create(var1, var0, "()V", "finalize", "CGLIB$finalize$0");
            CGLIB$equals$1$Method = var10000[1];
            CGLIB$equals$1$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/Object;)Z", "equals", "CGLIB$equals$1");
            CGLIB$toString$2$Method = var10000[2];
            CGLIB$toString$2$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/String;", "toString", "CGLIB$toString$2");
            CGLIB$hashCode$3$Method = var10000[3];
            CGLIB$hashCode$3$Proxy = MethodProxy.create(var1, var0, "()I", "hashCode", "CGLIB$hashCode$3");
            CGLIB$clone$4$Method = var10000[4];
            CGLIB$clone$4$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/Object;", "clone", "CGLIB$clone$4");
            CGLIB$getHalloWorld$5$Method = ReflectUtils.findMethods(new String[]{"getHalloWorld", "()Ljava/lang/String;"}, (var1 = Class.forName("com.tqy.document.reader.extention.demo1.TestInterface")).getDeclaredMethods())[0];
            CGLIB$getHalloWorld$5$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/String;", "getHalloWorld", "CGLIB$getHalloWorld$5");
        }
    
        final void CGLIB$finalize$0() throws Throwable {
            super.finalize();
        }
    
        protected final void finalize() throws Throwable {
            MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
            if (this.CGLIB$CALLBACK_0 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_0;
            }
    
            if (var10000 != null) {
                var10000.intercept(this, CGLIB$finalize$0$Method, CGLIB$emptyArgs, CGLIB$finalize$0$Proxy);
            } else {
                super.finalize();
            }
        }
    
        final boolean CGLIB$equals$1(Object var1) {
            return super.equals(var1);
        }
    
        public final boolean equals(Object var1) {
            MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
            if (this.CGLIB$CALLBACK_0 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_0;
            }
    
            if (var10000 != null) {
                Object var2 = var10000.intercept(this, CGLIB$equals$1$Method, new Object[]{var1}, CGLIB$equals$1$Proxy);
                return var2 == null ? false : (Boolean)var2;
            } else {
                return super.equals(var1);
            }
        }
    
        final String CGLIB$toString$2() {
            return super.toString();
        }
    
        public final String toString() {
            MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
            if (this.CGLIB$CALLBACK_0 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_0;
            }
    
            return var10000 != null ? (String)var10000.intercept(this, CGLIB$toString$2$Method, CGLIB$emptyArgs, CGLIB$toString$2$Proxy) : super.toString();
        }
    
        final int CGLIB$hashCode$3() {
            return super.hashCode();
        }
    
        public final int hashCode() {
            MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
            if (this.CGLIB$CALLBACK_0 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_0;
            }
    
            if (var10000 != null) {
                Object var1 = var10000.intercept(this, CGLIB$hashCode$3$Method, CGLIB$emptyArgs, CGLIB$hashCode$3$Proxy);
                return var1 == null ? 0 : ((Number)var1).intValue();
            } else {
                return super.hashCode();
            }
        }
    
        final Object CGLIB$clone$4() throws CloneNotSupportedException {
            return super.clone();
        }
    
        protected final Object clone() throws CloneNotSupportedException {
            MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
            if (this.CGLIB$CALLBACK_0 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_0;
            }
    
            return var10000 != null ? var10000.intercept(this, CGLIB$clone$4$Method, CGLIB$emptyArgs, CGLIB$clone$4$Proxy) : super.clone();
        }
    
        final String CGLIB$getHalloWorld$5() {
            return super.getHalloWorld();
        }
    
        public final String getHalloWorld() {
            MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
            if (this.CGLIB$CALLBACK_0 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_0;
            }
    
            return var10000 != null ? (String)var10000.intercept(this, CGLIB$getHalloWorld$5$Method, CGLIB$emptyArgs, CGLIB$getHalloWorld$5$Proxy) : super.getHalloWorld();
        }
    
        public static MethodProxy CGLIB$findMethodProxy(Signature var0) {
            String var10000 = var0.toString();
            switch(var10000.hashCode()) {
            case -1662339427:
                if (var10000.equals("getHalloWorld()Ljava/lang/String;")) {
                    return CGLIB$getHalloWorld$5$Proxy;
                }
                break;
            case -1574182249:
                if (var10000.equals("finalize()V")) {
                    return CGLIB$finalize$0$Proxy;
                }
                break;
            case -508378822:
                if (var10000.equals("clone()Ljava/lang/Object;")) {
                    return CGLIB$clone$4$Proxy;
                }
                break;
            case 1826985398:
                if (var10000.equals("equals(Ljava/lang/Object;)Z")) {
                    return CGLIB$equals$1$Proxy;
                }
                break;
            case 1913648695:
                if (var10000.equals("toString()Ljava/lang/String;")) {
                    return CGLIB$toString$2$Proxy;
                }
                break;
            case 1984935277:
                if (var10000.equals("hashCode()I")) {
                    return CGLIB$hashCode$3$Proxy;
                }
            }
    
            return null;
        }
    
        public TestInterface$$EnhancerByCGLIB$$72f42620() {
            CGLIB$BIND_CALLBACKS(this);
        }
    
        public static void CGLIB$SET_THREAD_CALLBACKS(Callback[] var0) {
            CGLIB$THREAD_CALLBACKS.set(var0);
        }
    
        public static void CGLIB$SET_STATIC_CALLBACKS(Callback[] var0) {
            CGLIB$STATIC_CALLBACKS = var0;
        }
    
        private static final void CGLIB$BIND_CALLBACKS(Object var0) {
            TestInterface$$EnhancerByCGLIB$$72f42620 var1 = (TestInterface$$EnhancerByCGLIB$$72f42620)var0;
            if (!var1.CGLIB$BOUND) {
                var1.CGLIB$BOUND = true;
                Object var10000 = CGLIB$THREAD_CALLBACKS.get();
                if (var10000 == null) {
                    var10000 = CGLIB$STATIC_CALLBACKS;
                    if (CGLIB$STATIC_CALLBACKS == null) {
                        return;
                    }
                }
    
                var1.CGLIB$CALLBACK_0 = (MethodInterceptor)((Callback[])var10000)[0];
            }
    
        }
    
        public Object newInstance(Callback[] var1) {
            CGLIB$SET_THREAD_CALLBACKS(var1);
            TestInterface$$EnhancerByCGLIB$$72f42620 var10000 = new TestInterface$$EnhancerByCGLIB$$72f42620();
            CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
            return var10000;
        }
    
        public Object newInstance(Callback var1) {
            CGLIB$SET_THREAD_CALLBACKS(new Callback[]{var1});
            TestInterface$$EnhancerByCGLIB$$72f42620 var10000 = new TestInterface$$EnhancerByCGLIB$$72f42620();
            CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
            return var10000;
        }
    
        public Object newInstance(Class[] var1, Object[] var2, Callback[] var3) {
            CGLIB$SET_THREAD_CALLBACKS(var3);
            TestInterface$$EnhancerByCGLIB$$72f42620 var10000 = new TestInterface$$EnhancerByCGLIB$$72f42620;
            switch(var1.length) {
            case 0:
                var10000.<init>();
                CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
                return var10000;
            default:
                throw new IllegalArgumentException("Constructor not found");
            }
        }
    
        public Callback getCallback(int var1) {
            CGLIB$BIND_CALLBACKS(this);
            MethodInterceptor var10000;
            switch(var1) {
            case 0:
                var10000 = this.CGLIB$CALLBACK_0;
                break;
            default:
                var10000 = null;
            }
    
            return var10000;
        }
    
        public void setCallback(int var1, Callback var2) {
            switch(var1) {
            case 0:
                this.CGLIB$CALLBACK_0 = (MethodInterceptor)var2;
            default:
            }
        }
    
        public Callback[] getCallbacks() {
            CGLIB$BIND_CALLBACKS(this);
            return new Callback[]{this.CGLIB$CALLBACK_0};
        }
    
        public void setCallbacks(Callback[] var1) {
            this.CGLIB$CALLBACK_0 = (MethodInterceptor)var1[0];
        }
    
        static {
            CGLIB$STATICHOOK1();
        }
    }
    
    

    cglib代理实现类

    1. 接口代码
    package com.tqy.document.reader.extention.demo2;
    
    /**
     * @author tengqingya
     * @create 2019-04-23 12:49
     */
    public interface TestInterface {
        String getHalloWorld();
    }
    
    1. 实现类代码
    package com.tqy.document.reader.extention.demo2;
    
    import static java.lang.System.out;
    
    /**
     * @author tengqingya
     * @create 2019-04-29 15:41
     */
    public class TestInterfaceImpl implements TestInterface {
        @Override
        public String getHalloWorld() {
            out.println("hello");
            return "hello";
        }
    }
    
    
    1. MethodInterceptor代码
    package com.tqy.document.reader.extention.demo2;
    
    import net.sf.cglib.proxy.Enhancer;
    import net.sf.cglib.proxy.MethodInterceptor;
    import net.sf.cglib.proxy.MethodProxy;
    
    import java.lang.reflect.Method;
    
    /**
     * @author tengqingya
     * @create 2019-04-23 12:49
     */
    
    public class TestCGLib implements MethodInterceptor {
    
        public Object getInstance(Class claxx) {
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(claxx);
            // 回调方法
            enhancer.setCallback(this);
            // 创建代理对象
            return enhancer.create();
        }
    
        @Override
        public Object intercept( Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
            methodProxy.invokeSuper(o,objects);
            return method.getName();
        }
    }
    
    1. main函数
    package com.tqy.document.reader.extention.demo2;
    
    import net.sf.cglib.core.DebuggingClassWriter;
    
    /**
     * @author tengqingya
     * @create 2019-04-23 12:50
     */
    public class TestCGLibMain {
        public static void main(String[] args) {
            System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "D:\\gitCode\\mySpring\\DcumentReader\\src\\main\\java\\com\\tqy\\document\\reader\\extention\\demo2");
    
            TestCGLib testCGLib = new TestCGLib();
            TestInterface o = (TestInterface) testCGLib.getInstance(TestInterfaceImpl.class);
            System.out.println(o.getHalloWorld());
        }
    }
    
    
    1. 运行结果
    CGLIB debugging enabled, writing to 'D:\gitCode\mySpring\DcumentReader\src\main\java\com\tqy\document\reader\extention\demo2'
    hello
    getHalloWorld
    
    1. 生成的源码
    //
    // Source code recreated from a .class file by IntelliJ IDEA
    // (powered by Fernflower decompiler)
    //
    
    package com.tqy.document.reader.extention.demo2;
    
    import java.lang.reflect.Method;
    import net.sf.cglib.core.ReflectUtils;
    import net.sf.cglib.core.Signature;
    import net.sf.cglib.proxy.Callback;
    import net.sf.cglib.proxy.Factory;
    import net.sf.cglib.proxy.MethodInterceptor;
    import net.sf.cglib.proxy.MethodProxy;
    
    public class TestInterfaceImpl$$EnhancerByCGLIB$$6ad87311 extends TestInterfaceImpl implements Factory {
        private boolean CGLIB$BOUND;
        private static final ThreadLocal CGLIB$THREAD_CALLBACKS;
        private static final Callback[] CGLIB$STATIC_CALLBACKS;
        private MethodInterceptor CGLIB$CALLBACK_0;
        private static final Method CGLIB$getHalloWorld$0$Method;
        private static final MethodProxy CGLIB$getHalloWorld$0$Proxy;
        private static final Object[] CGLIB$emptyArgs;
        private static final Method CGLIB$finalize$1$Method;
        private static final MethodProxy CGLIB$finalize$1$Proxy;
        private static final Method CGLIB$equals$2$Method;
        private static final MethodProxy CGLIB$equals$2$Proxy;
        private static final Method CGLIB$toString$3$Method;
        private static final MethodProxy CGLIB$toString$3$Proxy;
        private static final Method CGLIB$hashCode$4$Method;
        private static final MethodProxy CGLIB$hashCode$4$Proxy;
        private static final Method CGLIB$clone$5$Method;
        private static final MethodProxy CGLIB$clone$5$Proxy;
    
        static void CGLIB$STATICHOOK1() {
            CGLIB$THREAD_CALLBACKS = new ThreadLocal();
            CGLIB$emptyArgs = new Object[0];
            Class var0 = Class.forName("com.tqy.document.reader.extention.demo2.TestInterfaceImpl$$EnhancerByCGLIB$$6ad87311");
            Class var1;
            Method[] var10000 = ReflectUtils.findMethods(new String[]{"finalize", "()V", "equals", "(Ljava/lang/Object;)Z", "toString", "()Ljava/lang/String;", "hashCode", "()I", "clone", "()Ljava/lang/Object;"}, (var1 = Class.forName("java.lang.Object")).getDeclaredMethods());
            CGLIB$finalize$1$Method = var10000[0];
            CGLIB$finalize$1$Proxy = MethodProxy.create(var1, var0, "()V", "finalize", "CGLIB$finalize$1");
            CGLIB$equals$2$Method = var10000[1];
            CGLIB$equals$2$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/Object;)Z", "equals", "CGLIB$equals$2");
            CGLIB$toString$3$Method = var10000[2];
            CGLIB$toString$3$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/String;", "toString", "CGLIB$toString$3");
            CGLIB$hashCode$4$Method = var10000[3];
            CGLIB$hashCode$4$Proxy = MethodProxy.create(var1, var0, "()I", "hashCode", "CGLIB$hashCode$4");
            CGLIB$clone$5$Method = var10000[4];
            CGLIB$clone$5$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/Object;", "clone", "CGLIB$clone$5");
            CGLIB$getHalloWorld$0$Method = ReflectUtils.findMethods(new String[]{"getHalloWorld", "()Ljava/lang/String;"}, (var1 = Class.forName("com.tqy.document.reader.extention.demo2.TestInterfaceImpl")).getDeclaredMethods())[0];
            CGLIB$getHalloWorld$0$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/String;", "getHalloWorld", "CGLIB$getHalloWorld$0");
        }
    
        final String CGLIB$getHalloWorld$0() {
            return super.getHalloWorld();
        }
    
        public final String getHalloWorld() {
            MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
            if (this.CGLIB$CALLBACK_0 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_0;
            }
    
            return var10000 != null ? (String)var10000.intercept(this, CGLIB$getHalloWorld$0$Method, CGLIB$emptyArgs, CGLIB$getHalloWorld$0$Proxy) : super.getHalloWorld();
        }
    
        final void CGLIB$finalize$1() throws Throwable {
            super.finalize();
        }
    
        protected final void finalize() throws Throwable {
            MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
            if (this.CGLIB$CALLBACK_0 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_0;
            }
    
            if (var10000 != null) {
                var10000.intercept(this, CGLIB$finalize$1$Method, CGLIB$emptyArgs, CGLIB$finalize$1$Proxy);
            } else {
                super.finalize();
            }
        }
    
        final boolean CGLIB$equals$2(Object var1) {
            return super.equals(var1);
        }
    
        public final boolean equals(Object var1) {
            MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
            if (this.CGLIB$CALLBACK_0 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_0;
            }
    
            if (var10000 != null) {
                Object var2 = var10000.intercept(this, CGLIB$equals$2$Method, new Object[]{var1}, CGLIB$equals$2$Proxy);
                return var2 == null ? false : (Boolean)var2;
            } else {
                return super.equals(var1);
            }
        }
    
        final String CGLIB$toString$3() {
            return super.toString();
        }
    
        public final String toString() {
            MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
            if (this.CGLIB$CALLBACK_0 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_0;
            }
    
            return var10000 != null ? (String)var10000.intercept(this, CGLIB$toString$3$Method, CGLIB$emptyArgs, CGLIB$toString$3$Proxy) : super.toString();
        }
    
        final int CGLIB$hashCode$4() {
            return super.hashCode();
        }
    
        public final int hashCode() {
            MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
            if (this.CGLIB$CALLBACK_0 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_0;
            }
    
            if (var10000 != null) {
                Object var1 = var10000.intercept(this, CGLIB$hashCode$4$Method, CGLIB$emptyArgs, CGLIB$hashCode$4$Proxy);
                return var1 == null ? 0 : ((Number)var1).intValue();
            } else {
                return super.hashCode();
            }
        }
    
        final Object CGLIB$clone$5() throws CloneNotSupportedException {
            return super.clone();
        }
    
        protected final Object clone() throws CloneNotSupportedException {
            MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
            if (this.CGLIB$CALLBACK_0 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_0;
            }
    
            return var10000 != null ? var10000.intercept(this, CGLIB$clone$5$Method, CGLIB$emptyArgs, CGLIB$clone$5$Proxy) : super.clone();
        }
    
        public static MethodProxy CGLIB$findMethodProxy(Signature var0) {
            String var10000 = var0.toString();
            switch(var10000.hashCode()) {
            case -1662339427:
                if (var10000.equals("getHalloWorld()Ljava/lang/String;")) {
                    return CGLIB$getHalloWorld$0$Proxy;
                }
                break;
            case -1574182249:
                if (var10000.equals("finalize()V")) {
                    return CGLIB$finalize$1$Proxy;
                }
                break;
            case -508378822:
                if (var10000.equals("clone()Ljava/lang/Object;")) {
                    return CGLIB$clone$5$Proxy;
                }
                break;
            case 1826985398:
                if (var10000.equals("equals(Ljava/lang/Object;)Z")) {
                    return CGLIB$equals$2$Proxy;
                }
                break;
            case 1913648695:
                if (var10000.equals("toString()Ljava/lang/String;")) {
                    return CGLIB$toString$3$Proxy;
                }
                break;
            case 1984935277:
                if (var10000.equals("hashCode()I")) {
                    return CGLIB$hashCode$4$Proxy;
                }
            }
    
            return null;
        }
    
        public TestInterfaceImpl$$EnhancerByCGLIB$$6ad87311() {
            CGLIB$BIND_CALLBACKS(this);
        }
    
        public static void CGLIB$SET_THREAD_CALLBACKS(Callback[] var0) {
            CGLIB$THREAD_CALLBACKS.set(var0);
        }
    
        public static void CGLIB$SET_STATIC_CALLBACKS(Callback[] var0) {
            CGLIB$STATIC_CALLBACKS = var0;
        }
    
        private static final void CGLIB$BIND_CALLBACKS(Object var0) {
            TestInterfaceImpl$$EnhancerByCGLIB$$6ad87311 var1 = (TestInterfaceImpl$$EnhancerByCGLIB$$6ad87311)var0;
            if (!var1.CGLIB$BOUND) {
                var1.CGLIB$BOUND = true;
                Object var10000 = CGLIB$THREAD_CALLBACKS.get();
                if (var10000 == null) {
                    var10000 = CGLIB$STATIC_CALLBACKS;
                    if (CGLIB$STATIC_CALLBACKS == null) {
                        return;
                    }
                }
    
                var1.CGLIB$CALLBACK_0 = (MethodInterceptor)((Callback[])var10000)[0];
            }
    
        }
    
        public Object newInstance(Callback[] var1) {
            CGLIB$SET_THREAD_CALLBACKS(var1);
            TestInterfaceImpl$$EnhancerByCGLIB$$6ad87311 var10000 = new TestInterfaceImpl$$EnhancerByCGLIB$$6ad87311();
            CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
            return var10000;
        }
    
        public Object newInstance(Callback var1) {
            CGLIB$SET_THREAD_CALLBACKS(new Callback[]{var1});
            TestInterfaceImpl$$EnhancerByCGLIB$$6ad87311 var10000 = new TestInterfaceImpl$$EnhancerByCGLIB$$6ad87311();
            CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
            return var10000;
        }
    
        public Object newInstance(Class[] var1, Object[] var2, Callback[] var3) {
            CGLIB$SET_THREAD_CALLBACKS(var3);
            TestInterfaceImpl$$EnhancerByCGLIB$$6ad87311 var10000 = new TestInterfaceImpl$$EnhancerByCGLIB$$6ad87311;
            switch(var1.length) {
            case 0:
                var10000.<init>();
                CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
                return var10000;
            default:
                throw new IllegalArgumentException("Constructor not found");
            }
        }
    
        public Callback getCallback(int var1) {
            CGLIB$BIND_CALLBACKS(this);
            MethodInterceptor var10000;
            switch(var1) {
            case 0:
                var10000 = this.CGLIB$CALLBACK_0;
                break;
            default:
                var10000 = null;
            }
    
            return var10000;
        }
    
        public void setCallback(int var1, Callback var2) {
            switch(var1) {
            case 0:
                this.CGLIB$CALLBACK_0 = (MethodInterceptor)var2;
            default:
            }
        }
    
        public Callback[] getCallbacks() {
            CGLIB$BIND_CALLBACKS(this);
            return new Callback[]{this.CGLIB$CALLBACK_0};
        }
    
        public void setCallbacks(Callback[] var1) {
            this.CGLIB$CALLBACK_0 = (MethodInterceptor)var1[0];
        }
    
        static {
            CGLIB$STATICHOOK1();
        }
    }
    
    

    对比源码

    public class TestInterface$$EnhancerByCGLIB$$72f42620 implements TestInterface, Factory
    
    public class TestInterfaceImpl$$EnhancerByCGLIB$$6ad87311 extends TestInterfaceImpl implements Factory
    

    可知

    对接口进行代理的cglib,最后生成的源码是实现了该接口和Factory接口
    对实现类进行代理的cglib,最后生成的源码是继承了实现类并实现了Factory接口
    

    版权所有

    版权所有:tengqingya 转载请注明出处

    展开全文
  • vue-cli 接口代理配置

    千次阅读 2017-12-14 15:48:52
    一些同学在配置接口代理时,会有疑问 我配置成功了为什么接口还是不通 ,其实代理已经成功 只是 接口访问地址规则没搞清楚 下面以本地测试为栗子 向大家介绍  一些基本操作这里就不介绍了  ...

    一些同学在配置接口代理时,会有疑问 我配置成功了为什么接口还是不通 ,其实代理已经成功 只是 接口访问地址规则没搞清楚


    下面以本地测试为栗子 向大家介绍 


    一些基本操作这里就不介绍了 


    找到vue-cli config 文件夹下的 index.js  这是是配置接口规则的文件 同目录下建立 proxyConfig.js  一些同学习惯直接在原文件修改 ,也是可以但推荐使用新建文件的方式:

    module.exports = {

        proxyList: {

            '/api': {

                target: 'http://localhost:3000',   (这里是代理接口的位置)

                changeOrigin: true,(允许跨域,如果这不写,调用接口接口时会有跨域错误说缺少请求头)

                pathRewrite: {'^/api': '' } (路由规则下面详细介绍)

            }

        }

    }


    ******路由规则*******


        上述代码执行之后,意思就是 只要在接口中看到‘/api‘ 会自动变成我们设置的地址,那么我们在设置的时候可以规范接口书写标准 就用到了这个属性 pathRewrite 路径重写{'^/api': '' } 是指你想让路由变成什么样子 ,如果是空 那么在解析时 如果我们的接口写的是 $http.get('/api/good') 那么在服务端我们将会看到 /good的访问,如果此时在服务端并没有相应的路由匹配,客服端就会显示报错,端口号还是客户端的端口号,所以很多同学就错觉没有起作用 ,同理如果我们设置{'^/api': '/api' } 那么在服务端将会看到/api/goods 的接口访问,意思就是不仅将/api自动解析成代理地址,还会作为服务端的路由展示。

        另外在设置路由规则注意分隔符/  有些同学喜欢这样写 target: 'http://localhost:3000/', 自己写端口后面加上一个分隔符,会发现路由对不上,这时你需要在命名的时候也要加上‘/’ 路由规则也要加上‘/’ 接口才能对,不然 会发现请求接口的时候多了一个分隔符 如:'/api//goods'。

        此外,在配置代理规则时,可能根据服务端的路由,我们要进行多层级路由匹配 例如我们配置 我们服务端配置了 /api/A/C 或者/api/B 这样多层级路由 都要匹配到 /api 的代理规则下

    '/users/*': {
                target: 'http://localhost:3000/',
                changeOrigin: true,
                pathRewrite: {
                    '^/users/*': '/users/'
                }
            }

    规则后面匹配一个* 代表一层及 如果想匹配两个层级 就可以/users/**

    匹配规则可以依照项目接口层级规划来细细划分

    ********************    

    设置完之后,在index.js 里面 引入 proxyConfig.js,设置 proxyTable 完成设置 

    const proxyConfig = require('./proxyConfig')

    module.exports = {

        dev: {

            env: require('./dev.env'),

            host: 'localhost',

            port: 8188,

            autoOpenBrowser: true,

            assetsSubDirectory: 'static',

            assetsPublicPath: '/',

            proxyTable: proxyConfig.proxyList,

            cssSourceMap: false,

        }

     }

    请求时 这样写

    axios.get('/api/goods').then((res)=>{console.log(res.data);}}).catch((err)=>{console.log(err);}})

    展开全文
  • 关于前端的,关于接口代理

    千次阅读 2018-03-27 13:21:35
    关于前端的,朋友让我查前端接口代理,不知道怎么查给他
    
    关于前端的,朋友让我查前端接口代理,不知道怎么查给他
    展开全文
  • 前言: 项目使用vue-cli直接生成项目,然后使用proxyTable接口代理后, 在本地调用后台接口,一切正常,但是后期打包上线时,发现页面接口报404,后找到解决办法为: 后台修改nginx 代理配置: location /api { ...

    前言: 项目使用vue-cli直接生成项目,然后使用proxyTable接口代理后, 在本地调用后台接口,一切正常,但是后期打包上线时,发现页面接口报404,后找到解决办法为:
    在这里插入图片描述
    后台修改nginx 代理配置:
    location /api {
    proxy_set_header Host $host;
    proxy_set_header x-forwarded-for $remote_addr;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_pass 接口地址; // 接口地址,如果在同一个服务器上,可以直接这样写
    }

    再重启一下,就可以成功访问。

    备注:前端新手,有问题的地方请大神指出,如有侵权,属实无意,立删,谢谢~

    展开全文
  • JDK基于接口代理 import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; interface BookInterface { void addBook(); void intercept();...
  • 用node服务做接口代理转发

    千次阅读 2019-06-14 09:32:15
    前后端分离的项目中,前端项目本地开发的时候需要解决跨域的问题,这个时候可以通过本地启动node服务来进行接口代理转发。现在将实现的逻辑记录一下。 主要用到的是express和http-proxy-middleware这两个模块 ...
  • SpringAop 类代理模式与接口代理模式

    千次阅读 2017-07-02 12:23:25
    结论:MyBean未实现任何接口,spring采用CGLIBl类代理模式,当bean实现了接口,spring默认采用jdk的接口代理,当设置proxyTargetClass=true时,spring采用cglib类代理。 Demo采用spring的jar包如下:
  • JDK Proxy实现接口代理

    千次阅读 2014-04-27 00:14:03
    JDK提供的java.reflect.Proxy代理类基于接口提供代理功能。
  • 代理模式是常用的java设计模式,它的特征是代理类与委托类有同样的接口代理类主要负责为委托类预处理消息、过滤消息、把消息转发给委托类,以及事后处理消息等。代理类与委托类之间通常会存在关联关系,一个代理类...
  • 使用nginx作为webservice接口代理

    千次阅读 2019-10-02 10:07:24
    通常情况下,企业并不会直接开放系统接口给到外网,并且在企业内部同样有SOA或者ESB这样的接口统一管理的工具。 那么,大多数情况下,如果需要与外部系统,如云系统,或者其他企业的系统做接口时采取的方式如下 ...
  • nginx api接口代理配置细节

    千次阅读 2021-01-28 11:17:30
    } } 倒数第二行代理的配置,后面如果有 斜杠 ‘/’ ,例如:proxy_pass http://127.0.0.1:7001/; 转发接口会把 api去掉,会导致404 ,找不到接口 去掉斜杠 “/ ”, 则转发接口会带 '/api/'。 这是一个很容易忽视...
  • 如果项目是用 react webpack 搭建的,或者是vue-cli 搭建的话,webpack配置中的proxy插件就可以实现服务器代理。 但是如果平时开发一些简单的页面,几个页面的小项目,但是也涉及到和后端的数据交互,我们还想做代理...
  • vue项目nginx配置API 接口代理

    千次阅读 2019-09-06 17:02:53
    vue中配置代理 "/api": { target: "http://127.0.0.1:9200", changeOrigin: true, pathRewrite: { "^/api": "" } } 那么问题来了这里的配置只有在本机没打包的时候才有效的,打包之后/API 径开头的请求就...
  • vue-cli 搭建的项目我们在本地是可以设置代理的,比如 /api 路径开头的请求都会代理到本地的3001 端口,并且代理以后路径上还会加上 /api 接口前缀。 ProxyTable: { '/api': { target: 'http://127.0.0.1:3001', ...
  • vue使用proxyTable设置接口代理

    万次阅读 2018-08-06 20:12:08
    1、修改config/index.js里proxyTable proxyTable: { '/api': { target: 'http://192.168.42.182:8080', changeOrigin: true, pathRewrite: { '^/api': '/' } }, } 2、修改c...
  • 前言: 部分qq音乐的api接口不能直接通过jsonp访问,需要通过官方的代理才能获取,如:歌词,推荐歌单等 1. webpack.dev.conf.js中创建接口: // 开头调用: var express = require('express') var axios = require('...
  • vue.config.js ...// 如果要代理 websockets ws: true , // 将主机标头的原点更改为目标URL changeOrigin: false } } } } 调用示例 axios.defaults.baseURL = '/api/'
  • webpack.dev.conf.js中创建接口: // 开头调用: var express = require(‘express’) var axios = require(‘axios’) var app = express() var apiRoutes = express.Router() app.use(‘/api’, apiRoutes) ...
  • 评论
  • vue.config.js是一个可选的配置文件 新建vue.config.js文件,存放在项目根目录(将自动加载)中: module.exports = {  devServer: {  // 设置主机地址  host: 'localhost', ... // 设置代理  ...
  • 但是无奈的是,最新的 chrome 浏览器好像已经关闭了这个选项,那么我们只能采用另外一种方法了——将接口代理到本地。 参考: https://blog.csdn.net/FungLeo/article/details/77601761 为了解决这个问题,...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 538,999
精华内容 215,599
关键字:

接口代理