{"id":1493,"date":"2023-03-25T13:42:57","date_gmt":"2023-03-25T05:42:57","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1493"},"modified":"2023-04-28T20:29:15","modified_gmt":"2023-04-28T12:29:15","slug":"principle-of-jdk-dynamic-proxy","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/25\/principle-of-jdk-dynamic-proxy\/","title":{"rendered":"JDK\u52a8\u6001\u4ee3\u7406\u7684\u539f\u7406"},"content":{"rendered":"<h2>Proxy\u4ecb\u7ecd<\/h2>\n<p>\u4e4b\u524d\u5728\u7814\u7a76hook\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u5230\u4e86Proxy\u52a8\u6001\u4ee3\u7406\u3002\u5b83\u662f\u6765\u81eaJDK\u7684\u4e00\u4e2a\u7c7b\uff1a<\/p>\n<pre><code class=\"language-java\">package java.lang.reflect;\n\npublic class Proxy implements java.io.Serializable {<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>\u5173\u952e\u65b9\u6cd5\u662f\uff1a<\/p>\n<pre><code class=\"language-java\">@CallerSensitive\npublic static Object newProxyInstance(ClassLoader loader,\n                                      Class&lt;?&gt;[] interfaces,\n                                      InvocationHandler h)\n    throws IllegalArgumentException\n{\n}<\/code><\/pre>\n<p>\u4f7f\u7528\u8d77\u6765\u5f88\u7b80\u5355\uff1a<\/p>\n<pre><code class=\"language-java\">\/**\n * \u83b7\u5f97\u52a8\u6001\u4ee3\u7406\n * \n * @return\n *\/\npublic Object getProxyInstance() {\n    return Proxy.newProxyInstance(\n            realInstance.getClass().getClassLoader(),\n            realInstance.getClass().getInterfaces(),\n            this);\n    \/\/\u90a3\u4e48\u8fd9\u4e2a\u5bf9\u8c61\u5230\u5e95\u662f\u5982\u4f55\u521b\u5efa\u51fa\u6765\u7684\u5462\uff1f\n\n}<\/code><\/pre>\n<p>\u53ea\u662f\u4e00\u53e5\u8bdd\u800c\u5df2\uff0c\u5c31\u80fd\u5f97\u5230\u4e00\u4e2a\u63a5\u53e3\u7684\u52a8\u6001\u4ee3\u7406\u3002<\/p>\n<h2>\u52a8\u6001\u4ee3\u7406\u5b9e\u73b0<\/h2>\n<p>\u53ea\u662f\u4e00\u4e2a\u5f88\u7b80\u5355\u7684\u7c7bDynamicProxyInstance.java<\/p>\n<pre><code class=\"language-java\">import java.lang.reflect.InvocationHandler;\nimport java.lang.reflect.Method;\nimport java.lang.reflect.Proxy;\n\n\/**\n * \u52a8\u6001\u4ee3\u7406\u5bf9\u8c61\n * \u6240\u8c13\uff0c\u52a8\u6001\u4ee3\u7406\uff0c\u5b83\u7684\u4f18\u52bf\uff0c\u5c31\u662f\u6211\u4f7f\u7528\u4e4b\u540e\uff0c\u83b7\u53d6\u4ee3\u7406\u5bf9\u8c61\uff0c\u4e0d\u518d\u4f9d\u8d56\u5916\u90e8\u4f20\u5165\u7684\u63a5\u53e3\uff0c\u5916\u90e8\u63a5\u53e3\u53d8\u5316\uff0c\u6211\u53ea\u9700\u8981\u6539\u53d8\u771f\u5b9e\u5bf9\u8c61\u7684\u5b9e\u4f8b\u5373\u53ef\n *\/\npublic class DynamicProxyInstance implements InvocationHandler {\n\n    \/\/ \u52a8\u6001\u4ee3\u7406\uff0c\u8fd9\u91cc\u4e0d\u9650\u5236\u5177\u4f53\u4f7f\u7528\u7684\u63a5\u53e3\u662f\u4ec0\u4e48\uff0c\u800c\u662f\u4f7f\u7528Object\u8868\u793a\u771f\u5b9e\u5bf9\u8c61\n    private Object realInstance;\n\n    public void init(Object realInstance) {\n        this.realInstance = realInstance;\n    }\n\n    \/**\n     * \u83b7\u5f97\u52a8\u6001\u4ee3\u7406\n     * \n     * @return\n     *\/\n    public Object getProxyInstance() {\n        return Proxy.newProxyInstance(realInstance.getClass().getClassLoader(), realInstance.getClass().getInterfaces(),\n                this);\n        \/\/ \u90a3\u4e48\u8fd9\u4e2a\u5bf9\u8c61\u5230\u5e95\u662f\u5982\u4f55\u521b\u5efa\u51fa\u6765\u7684\u5462\uff1f\n\n    }\n\n    \/**\n     * \u552e\u524d\n     *\/\n    private void doBefore() {\n        System.out.println(&quot;\u552e\u524d&quot;);\n    }\n\n    \/**\n     * \u552e\u540e\n     *\/\n    private void doAfter() {\n        System.out.println(&quot;\u552e\u540e&quot;);\n    }\n\n    @Override\n    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {\n        doBefore();\n        Object res = method.invoke(realInstance, args);\n        doAfter();\n        return res;\n    }\n\n    public static void main(String[] args) {\n\n        \/\/ \u52a8\u6001\u4ee3\u7406\u548c\u9759\u6001\u4ee3\u7406\u7684\u552f\u4e00\u533a\u522b\uff0c\u5c31\u662f\u5728\u83b7\u53d6\u4ee3\u7406\u5bf9\u8c61\u7684\u65f6\u5019\uff0c\u9759\u6001\u4ee3\u7406\u83b7\u53d6\u7684\u662f\u4e00\u4e2a\u4e0d\u53ef\u53d8\u529f\u80fd\u7684\u5bf9\u8c61\uff0c\u4e00\u65e6\u83b7\u53d6\u5230\uff0c\u90a3\u4e48\u5b83\u7684\u529f\u80fd\u4e5f\u5c31\u786e\u5b9a\u4e86\n        \/\/ \u800c\uff0c\u52a8\u6001\u4ee3\u7406\n\n        DynamicProxyInstance instance = new DynamicProxyInstance();\n        instance.init(new RealInstance());\n        CommInterface commInterface = (CommInterface) instance.getProxyInstance();\n        \/\/ \u5148\u770b\u770b\u8fd9\u4e2a\u5bf9\u8c61\u5728debug\u7684\u65f6\u5019\uff0c\u662f\u600e\u4e48\u6837\u7684\u4e00\u4e2a\u5bf9\u8c61\u5f15\u7528\n        commInterface.buyManTools(30);\n\n        \/\/ \u73b0\u5728\uff0c\u51fa\u73b0\u4e86\u4e00\u4e2a\u8d2d\u4e70\u5973\u6027\u7528\u54c1\u7684\u63a5\u53e3\uff0c\u548c\u5b83\u7684\u5b9e\u73b0\u7c7b\uff0c\u4e5f\u8981\u7528\u4ee3\u7406\u6765\u8bbf\u95ee\n        \/\/ \u8fd9\u6837\u505a\n        instance.init(new RealInstance2());\n        CommInterface2 commInterface2 = (CommInterface2) instance.getProxyInstance();\n        commInterface2.buyWomenTools(60);\n\n        \/\/ \u770b\u5230\u4e86\u5427\uff0c\u540c\u6837\u7684\u4e00\u4e2a\u4ee3\u7406\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5b9e\u73b0\u591a\u4e2a\u771f\u5b9e\u5bf9\u8c61\u7684\u4ee3\u7406\u529f\u80fd\uff0c\u800c\u4e0d\u662f\u50cf\u9759\u6001\u4ee3\u7406\u4e00\u6837\uff0c\u9700\u8981\u6539\u52a8\u4ee3\u7406\u7c7b\n    }\n}<\/code><\/pre>\n<p>\u5176\u4ed6\u5173\u8054\u7684\u7c7b\u548c\u63a5\u53e3\u662f:<\/p>\n<pre><code class=\"language-java\">\/**\n * \u516c\u5171\u63a5\u53e3\uff0c\u7edf\u4e00\u5b9a\u4e49\u4ee3\u7406\u7c7b\u548c\u88ab\u4ee3\u7406\u7c7b\u7684\u884c\u4e3a\n *\/\npublic interface CommInterface {\n    \/**\n     * \u8d2d\u4e70\u60c5\u8da3\u5a03\u5a03\n     * \n     * @param size \u5a03\u5a03\u7684\u5c3a\u5bf8\n     *\/\n    abstract void buyManTools(int size);\n}<\/code><\/pre>\n<pre><code class=\"language-java\">\/**\n * \u516c\u5171\u63a5\u53e3\uff0c\u7edf\u4e00\u5b9a\u4e49\u4ee3\u7406\u7c7b\u548c\u88ab\u4ee3\u7406\u7c7b\u7684\u884c\u4e3a\n *\/\npublic interface CommInterface2 {\n    \/**\n     * \u8d2d\u4e70\u60c5\u8da3\u5a03\u5a03\n     * \n     * @param size \u5a03\u5a03\u7684\u5c3a\u5bf8\n     *\/\n    abstract void buyWomenTools(int size);\n}<\/code><\/pre>\n<pre><code class=\"language-java\">\/**\n * \u771f\u5b9e\u5bf9\u8c61\n *\/\npublic class RealInstance implements CommInterface {\n\n    @Override\n    public void buyManTools(int size) {\n        System.out.println(&quot;\u8d2d\u4e70\u7537\u6027\u7528\u54c1:size&quot; + size);\n    }\n\n}<\/code><\/pre>\n<pre><code class=\"language-java\">\/**\n * \u771f\u5b9e\u5bf9\u8c61\n *\/\npublic class RealInstance2 implements CommInterface2 {\n\n    @Override\n    public void buyWomenTools(int size) {\n        System.out.println(&quot;\u8d2d\u4e70\u5973\u6027\u7528\u54c1::size&quot; + size);\n    }\n\n}<\/code><\/pre>\n<p>OK\uff0c\u4ee3\u7801\u8d34\u5b8c\u4e86\uff0c\u73b0\u5728\u5f00\u59cb\u5b9e\u4f8b\u8bb2\u89e3\uff1a<br \/>\n\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u662f\u5728\u6a21\u62dfXX\u7528\u54c1\u9500\u552e\uff0c\u7537\u7528\u3001\u5973\u7528\u7684\u5206\u522b\u7528\u4e24\u4e2a\u63a5\u53e3\u6765\u5b9a\u4e49\uff0c\u4e3a\u4e86\u5b9a\u4e49\u8fd9\u4e24\u4e2a\u529f\u80fd\uff0c\u6211\u7528\u4e86\u4e24\u4e2a\u63a5\u53e3\uff0c\u5e76\u4e14\u7528\u4e24\u4e2a\u5b9e\u73b0\u7c7b\u5206\u522b\u5b9e\u73b0\u4e86\u8fd9\u4e24\u4e2a\u63a5\u53e3\u3002<code>DynamicProxyInstance.java<\/code>\u5219\u662f\u4ee3\u7406\u7c7b\uff0c\u5916\u754c\uff08\u4e5f\u5c31\u662f\u8fd9\u91cc\u7684<code>static void main<\/code>\u51fd\u6570\uff09\u901a\u8fc7\u8be5\u4ee3\u7406\u7c7b\u6765\u8fdb\u884c\u8d2d\u4e70\u52a8\u4f5c<\/p>\n<p>\u8bf7\u770b\u8fd9\u6bb5\u4ee3\u7801\uff0c\u5b83\u662f<code>new<\/code>\u51fa\u4e86\u4e00\u4e2a<code>proxy<\/code>\u7684<code>Object<\/code>\uff0c\u5e76\u4e14<code>return<\/code>\u51fa\u53bb<\/p>\n<pre><code class=\"language-java\">public Object getProxyInstance() {\n    return Proxy.newProxyInstance(realInstance.getClass().getClassLoader(), realInstance.getClass().getInterfaces(),\n            this);\n    \/\/ \u90a3\u4e48\u8fd9\u4e2a\u5bf9\u8c61\u5230\u5e95\u662f\u5982\u4f55\u521b\u5efa\u51fa\u6765\u7684\u5462\uff1f\n\n}<\/code><\/pre>\n<p>\u800c\uff0c\u62ff\u5230\u8fd9\u4e2a\u5bf9\u8c61\u4e4b\u540e\uff0c\u5f3a\u8f6c\u4e3a\u4e86<code>CommInterface<\/code><\/p>\n<pre><code class=\"language-java\">\/\/ \u5148\u770b\u770b\u8fd9\u4e2a\u5bf9\u8c61\u5728debug\u7684\u65f6\u5019\uff0c\u662f\u600e\u4e48\u6837\u7684\u4e00\u4e2a\u5bf9\u8c61\u5f15\u7528\nCommInterface commInterface = (CommInterface) instance.getProxyInstance();\ncommInterface.buyManTools(30);<\/code><\/pre>\n<p>\u4e3a\u4ec0\u4e48\u5b83\u53ef\u4ee5\u5f3a\u8f6c\u4e3a<code>CommInterface<\/code>\uff1f\u56e0\u4e3a<code>instance.init(new RealInstance())<\/code>\uff0c\u8fd9\u4e00\u53e5\u5f53\u4e2d<code>RealInstance<\/code>\u662f<code>CommIntance<\/code>\u7684\u5b50\u7c7b\u3002\u4ece\u7ed3\u679c\u4e0a\u6765\u770b\uff0c\u901a\u8fc7<code>getInstance<\/code>\u65b9\u6cd5\u62ff\u5230\u7684<code>Object<\/code>\uff0c\u76f4\u63a5\u5c31\u5177\u5907<code>realInstance<\/code>\u7684\u529f\u80fd\u3002\u90a3\u8fd9\u4e2a<code>Object<\/code>\u5230\u5e95\u662f\u4ec0\u4e48\u4e1c\u897f\uff1f\u5b83\u662f\u5982\u4f55\u751f\u6210\u7684\uff1f<\/p>\n<p>\u56de\u5230<code>getInstance()<\/code>\u65b9\u6cd5\uff0c\u901a\u8fc7debug\uff0c\u6211\u4eec\u53d1\u73b0<\/p>\n<pre><code class=\"language-java\">CommInterface commInterface = (CommInterface) instance.getProxyInstance();<\/code><\/pre>\n<p>\u5b83\u662f\u4e00\u4e2a\u540d\u4e3a<code>$Proxy0<\/code>\u7684\u7c7b\u7684\u5b9e\u4f8b\uff0c\u5176\u771f\u5b9e\u7c7b\u578b\u662f<code>RealInstance<\/code>\uff0c<code>$Proxy0<\/code>\u540d\u5b57\u5f88\u8be1\u5f02\uff0c\u4f46\u662f\u5148\u522b\u60f3\u8fd9\u4e48\u591a\uff0c\u7ee7\u7eed\u5f80\u4e0b\u770b<\/p>\n<pre><code class=\"language-java\">CommInterface2 commInterface2 = (CommInterface2) instance.getProxyInstance();<\/code><\/pre>\n<p>\u8fd9\u4f1a\u53c8\u53d8\u6210\u4e86<code>$Proxy1<\/code>\uff0c\u5176\u771f\u5b9e\u7c7b\u578b\u662f<code>RealInstance2<\/code><\/p>\n<p>\u4ece0\u52301\uff0c\u770b\u4e0a\u53bb\u50cf\u662f\u6709\u4e00\u4e2a\u6574\u5f62\u5e8f\u5217\u5728\u53c2\u4e0e\u5b83\u7684\u547d\u540d\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u6211\u4eec\u518d\u6765\u4e00\u6b21\uff0c\u80af\u5b9a\u5c31\u662f<code>$Proxy2<\/code><br \/>\n\u90a3<code>$Proxy<\/code> + 1\u4e2a\u6570\u5b57\uff0c\u8fd9\u4e2a\u7c7b\u662f\u5982\u4f55\u751f\u6210\u7684\uff1f\u5728\u6211\u4eec\u7684\u4ee3\u7801\u91cc\u9762\u547d\u540d\u6ca1\u6709\u53bb\u5b9a\u4e49\u5b83\u554a\uff1f<br \/>\n\u8fdb\u5165\u6e90\u7801\uff1a<code>Proxy.java<\/code>\u7684<code>newProxyInstance<\/code>\u65b9\u6cd5<\/p>\n<pre><code class=\"language-java\">@CallerSensitive\npublic static Object newProxyInstance(ClassLoader loader,\n                                      Class&lt;?&gt;[] interfaces,\n                                      InvocationHandler h)\n    throws IllegalArgumentException {\n    Objects.requireNonNull(h);\n\n    final Class&lt;?&gt;[] intfs = interfaces.clone();\n    final SecurityManager sm = System.getSecurityManager();\n    if (sm != null) {\n        checkProxyAccess(Reflection.getCallerClass(), loader, intfs);\n    }\n\n    \/*\n     * Look up or generate the designated proxy class.\n     *\/\n    Class&lt;?&gt; cl = getProxyClass0(loader, intfs);<\/code><\/pre>\n<p>\u8fd9\u91cc\u67093\u4e2a\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>\u7b2c\u4e00\u4e2a\u662f <code>\u7c7b\u52a0\u8f7d\u5668ClassLoader<\/code><\/li>\n<li>\u7b2c\u4e8c\u4e2a\u662f <code>\u63a5\u53e3\uff08\u5b83\u7684\u4f5c\u7528\u662f\u5b9a\u4e49\u8fd9\u4e2a\u52a8\u6001\u4ee3\u7406\u7c7b\u7684\u884c\u4e3a\uff09<\/code><\/li>\n<li>\u7b2c\u4e09\u4e2a\u662f <code>InvocationHanlder\u5b9e\u4f8b\uff0c\u5b83\u7684\u4f5c\u7528\u662f\u7528\u6765\u5bf9 \u88ab\u4ee3\u7406\u5bf9\u8c61\u8fdb\u884c\u529f\u80fd\u6269\u5c55<\/code><\/li>\n<\/ul>\n<p><code>newProxyInstance<\/code>\u65b9\u6cd5\u5f80\u4e0b\u770b\uff1a<code>Class&lt;?&gt; cl = getProxyClass0(loader, intfs);<\/code>\uff0c\u8fd9\u91cc\u662f\u4e0d\u662f\u53ef\u80fd\u751f\u6210<code>$Proxy1<\/code>\u8fd9\u79cd<code>class<\/code>\u7684\u5730\u65b9\uff1f\u8fdb\u53bb\uff1a<\/p>\n<pre><code class=\"language-java\">private static Class&lt;?&gt; getProxyClass0(ClassLoader loader,\n                                       Class&lt;?&gt;... interfaces) {\n    if (interfaces.length &gt; 65535) {\n        throw new IllegalArgumentException(&quot;interface limit exceeded&quot;);\n    }\n\n    \/\/ If the proxy class defined by the given loader implementing\n    \/\/ the given interfaces exists, this will simply return the cached copy;\n    \/\/ otherwise, it will create the proxy class via the ProxyClassFactory\n    return proxyClassCache.get(loader, interfaces);\n}<\/code><\/pre>\n<p>\u89c2\u5bdf\u4e00\u4e0b\u8fd9\u4e2a<code>proxyClassCache<\/code>\u5bf9\u8c61\uff0c\u5b83\u7684\u7c7b\u578b\u662f\uff1a<code>WeakCache&lt;ClassLoader, Class&lt;?&gt;[], Class&lt;?&gt;&gt;<\/code><br \/>\n\u5b83\u662f\u4e00\u4e2a\u6cdb\u578b\u7c7b\uff1a<code>final class WeakCache&lt;K, P, V&gt;<\/code>3\u4e2a\u6cdb\u578b\u53c2\u6570\u3002<\/p>\n<pre><code class=\"language-java\">\/**\n * a cache of proxy classes\n *\/\nprivate static final WeakCache&lt;ClassLoader, Class&lt;?&gt;[], Class&lt;?&gt;&gt;\n    proxyClassCache = new WeakCache&lt;&gt;(new KeyFactory(), new ProxyClassFactory());<\/code><\/pre>\n<p>\u8fdb\u5165<code>proxyClassCache.get(loader, interfaces)<\/code><\/p>\n<pre><code class=\"language-java\">public V get(K key, P parameter) {  \/\/\u5173\u6ce8\u70b91\uff1a\u5165\u53c2\n    Objects.requireNonNull(parameter);\n\n    expungeStaleEntries();\n\n    Object cacheKey = CacheKey.valueOf(key, refQueue);\n\n    \/\/ lazily install the 2nd level valuesMap for the particular cacheKey\n    ConcurrentMap&lt;Object, Supplier&lt;V&gt;&gt; valuesMap = map.get(cacheKey);\n    if (valuesMap == null) {\n        ConcurrentMap&lt;Object, Supplier&lt;V&gt;&gt; oldValuesMap\n            = map.putIfAbsent(cacheKey,\n                              valuesMap = new ConcurrentHashMap&lt;&gt;());\n        if (oldValuesMap != null) {\n            valuesMap = oldValuesMap;\n        }\n    }\n\n    \/\/ create subKey and retrieve the possible Supplier&lt;V&gt; stored by that\n    \/\/ subKey from valuesMap\n    Object subKey = Objects.requireNonNull(subKeyFactory.apply(key, parameter));  \/\/\u5173\u6ce8\u70b92\uff1a\u5165\u53c2\u7684\u4f7f\u7528\n    Supplier&lt;V&gt; supplier = valuesMap.get(subKey);\n    Factory factory = null;\n\n    while (true) {\n        if (supplier != null) {\n            \/\/ supplier might be a Factory or a CacheValue&lt;V&gt; instance\n            V value = supplier.get();\n            if (value != null) {\n                return value;  \/\/\u5173\u6ce8\u70b93\uff1a\u51fd\u6570\u8fd4\u56de\u503c\n            }\n        }<\/code><\/pre>\n<p>\u5b83\u7684\u4f5c\u7528\u5c31\u662f\uff0c\u901a\u8fc7<code>K<\/code>\u548c<code>P<\/code>\uff0c\u5f97\u5230<code>V<\/code>\uff0c\u4e5f\u5c31\u662f\u901a\u8fc7<code>ClassLoader<\/code>\u548c<code>Class&lt;?&gt;[]<\/code>\u5f97\u5230<code>Class&lt;?&gt;<\/code>\u3002<code>ClassLoader<\/code>\u662f\u7c7b\u52a0\u8f7d\u5668\uff0c\u7528\u6765\u521b\u5efa<code>Class<\/code>\u5bf9\u8c61\u7684\uff0c<code>Class&lt;?&gt;[]<\/code>\u5176\u5b9e\u5c31\u662f\u63a5\u53e3\u6570\u7ec4\uff0c<code>Class&lt;?&gt;<\/code>\u662f\u6700\u7ec8\u5f97\u5230\u7684Class\u5bf9\u8c61\u3002<br \/>\n\u4e4b\u524d\u6211\u4eec\u63a2\u7d22\u7684\u5c31\u662f<code>$Proxy0<\/code>\u3001<code>$Proxy1<\/code>\u8fd9\u4e2a\u7c7b\u7684\u6765\u6e90\uff0c\u90a3\u4e48\u5230\u5e95\u662f\u4e0d\u662f\u6765\u81ea\u4e8e\u8fd9\u91cc\u5462\uff1f<br \/>\n\u5f53\u6211\u4eec\u63a2\u7a76\u6e90\u7801\u4e2d\u7684\u4e00\u4e2a\u65b9\u6cd5\u7684\u65f6\u5019\uff0c\u5173\u6ce83\u4e2a\u70b9\uff1a<\/p>\n<ul>\n<li>\u5165\u53c2\u7c7b\u578b\uff08\u8fd9\u91cc\u7684\u5165\u53c2\u7c7b\u578b\u662f\u6cdb\u578b\uff0c\u5177\u4f53\u7c7b\u578b\u8981\u770b\u8fd9\u4e2a\u5f53\u524d\u5bf9\u8c61\u5728\u521b\u5efa\u7684\u65f6\u5019\uff0c\u6cdb\u578b\u5177\u4f53\u5316\u4f7f\u7528\u7684\u662f\u4ec0\u4e48\u7c7b\uff09<\/li>\n<li>\u4f7f\u7528\u53c2\u6570\u7684\u5730\u65b9\uff08\u6211\u4eec\u5165\u53c2\u4f20\u9012\u8fdb\u6765\uff0c\u81ea\u7136\u8981\u4ea7\u751f\u4f5c\u7528\uff0c\u90a3\u4e48\u76f4\u63a5\u5173\u5fc3\u5165\u53c2\u4f7f\u7528\u7684\u4ee3\u7801\u5373\u53ef\uff09<\/li>\n<li>\u65b9\u6cd5\u8fd4\u56de\u503c\uff0c\uff08\u4e0a\u9762\u7684\u8fd9\u4e2a\u65b9\u6cd5\u6709\u8fd4\u56de\u503c\uff0c\u7c7b\u578b\u662f\u6cdb\u578b\uff0c\u4e5f\u53bb\u770b\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u65f6\u5019\u4f7f\u7528\u7684<code>V<\/code>\u662f\u4ec0\u4e48\u7c7b\u578b\uff0c\u7136\u540e\u65b9\u6cd5\u91cc\u9762\u8fd4\u56de\u7684value\uff0c\u5c31\u662f\u6211\u4eec\u6700\u7ec8\u9700\u8981\u7684\u5bf9\u8c61)<\/li>\n<\/ul>\n<p>\u5173\u6ce8<code>subkeyFactory.apply<\/code>\uff0c\u5728Proxy\u4e2d\uff0c\u5b9a\u4e49WeakCache\u7684\u65f6\u5019\uff0c\u5c31<code>new<\/code>\u4e86\u4e00\u4e2a<code>KeyFactory<\/code>\u548c\u4e00\u4e2a<code>ProxyClassFactory<\/code>\u4f20\u8fdb\u53bb\uff0c\u5206\u522b\u4f5c\u4e3a<code>WeakCache<\/code>\u7684<code>subkeyFactory<\/code>\u6210\u5458\u548c<code>valueFactory<\/code>\u6210\u5458<\/p>\n<pre><code class=\"language-java\">final class WeakCache&lt;K, P, V&gt; {\n    public WeakCache(BiFunction&lt;K, P, ?&gt; subKeyFactory,\n                     BiFunction&lt;K, P, V&gt; valueFactory) {\n        this.subKeyFactory = Objects.requireNonNull(subKeyFactory);\n        this.valueFactory = Objects.requireNonNull(valueFactory);\n    }<\/code><\/pre>\n<p>\u4e0a\u9762\uff0c\u6211\u4eec\u53ea\u4f7f\u7528\u4e86<code>subkeyFactory.apply<\/code>\uff0c\u5b83\u8d70\u7684\u662f\u8fd9\u4e2a\u8fc7\u7a0b\uff1a<\/p>\n<pre><code class=\"language-java\">public class Proxy implements java.io.Serializable {\n\n    \/**\n     * A function that maps an array of interfaces to an optimal key where\n     * Class objects representing interfaces are weakly referenced.\n     *\/\n    private static final class KeyFactory\n        implements BiFunction&lt;ClassLoader, Class&lt;?&gt;[], Object&gt;\n    {\n        @Override\n        public Object apply(ClassLoader classLoader, Class&lt;?&gt;[] interfaces) {\n            switch (interfaces.length) {\n                case 1: return new Key1(interfaces[0]); \/\/ the most frequent\n                case 2: return new Key2(interfaces[0], interfaces[1]);\n                case 0: return key0;\n                default: return new KeyX(interfaces);\n            }\n        }\n    }<\/code><\/pre>\n<p>\u6839\u636e\u53c2\u6570<code>interfaces.length<\/code>\u7684\u4e0d\u540c\uff0c\u8fd4\u56de\u4e0d\u540c\u7684Key\u5bf9\u8c61\u3002<\/p>\n<pre><code class=\"language-java\">\/\/ create subKey and retrieve the possible Supplier&lt;V&gt; stored by that\n\/\/ subKey from valuesMap\nObject subKey = Objects.requireNonNull(subKeyFactory.apply(key, parameter));  \/\/\u62ff\u5230key\u5bf9\u8c61\nSupplier&lt;V&gt; supplier = valuesMap.get(subKey);  \/\/\u4f7f\u7528key\u5bf9\u8c61\uff0c\u5c1d\u8bd5\u4ecevaluesMap\u4e2d\u53d6\u51favalueFactory\nFactory factory = null;\n\nwhile (true) {  \/\/\u65e0\u9650\u5faa\u73af\n    if (supplier != null) {  \/\/\u5982\u679c\u524d\u9762\u53d6\u5230\u7684supplier\u4e0d\u662f\u7a7a\n        \/\/ supplier might be a Factory or a CacheValue&lt;V&gt; instance\n        V value = supplier.get();  \/\/\u90a3\u5c31\u4ecesupplier\u4e2d\u53d6\u51favalue\uff0c\u5e76\u4e14\u8fd4\u56de\u51fa\u53bb\n        if (value != null) {\n            return value;\n        }\n    }\n    \/\/ else no supplier in cache\n    \/\/ or a supplier that returned null (could be a cleared CacheValue\n    \/\/ or a Factory that wasn&#039;t successful in installing the CacheValue)\n\n    \/\/ lazily construct a Factory\n    if (factory == null) {  \/\/\u5982\u679c\u5230\u4e86\u8fd9\u4e00\u6b65\uff0c\u8bf4\u660e\u524d\u9762\u6ca1\u6709return\uff0c\u5efa\u7acb\u4e00\u4e2a\u5de5\u5382\n        factory = new Factory(key, parameter, subKey, valuesMap);\n    }\n\n    if (supplier == null) {\n        supplier = valuesMap.putIfAbsent(subKey, factory);  \/\/putIfAbsent\u7684\u4f5c\u7528\u76f8\u5f53\u4e8e\uff0c\u4e00\u4e2amap\uff0c\u5982\u679c\u5b58\u4e86subKey\u5bf9\u5e94\u7684value\uff0c\u5c31\u8fd4\u56de\u8fd9\u4e2avalue\uff0c\u5982\u679c\u6ca1\u6709\u5b58\uff0c\u5c31\u5b58\u8fdb\u53bb\n        if (supplier == null) {\n            \/\/ successfully installed Factory\n            supplier = factory;\n        }\n        \/\/ else retry with winning supplier\n    } else {\n        if (valuesMap.replace(subKey, supplier, factory)) {\n            \/\/ successfully replaced\n            \/\/ cleared CacheEntry \/ unsuccessful Factory\n            \/\/ with our Factory\n            supplier = factory;\n        } else {\n            \/\/ retry with current supplier\n            supplier = valuesMap.get(subKey);\n        }\n    }\n}<\/code><\/pre>\n<p>\u4e0a\u9762\u8fd9\u6bb5\u4ee3\u7801\u7684\u6ce8\u91ca\u5982\u679c\u770b\u61c2\u4e86\u7684\u8bdd\uff0c\u5c31\u57fa\u672c\u660e\u767d\u4e86\uff0c\u5b83\u8fd9\u91cc\u5c31\u662f\u5efa\u4e86\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u68c0\u6d4b\u673a\u5236\uff0c\u6700\u7ec8\u7ed3\u679c\u90fd\u662f\u8981\u4ece<code>supplier<\/code>\u4e2d<code>get<\/code>\u4e00\u4e2a<code>value<\/code>\u8fd4\u56de\u51fa\u53bb\uff0c\u5982\u679c<code>supplier<\/code>\u662f\u7a7a\uff0c\u90a3\u5c31\u5efa\u4e00\u4e2a<code>Factory<\/code>\u8d4b\u503c\u7ed9<code>supplier<\/code>(\u5176\u5b9e<code>Factory<\/code>\u662f\u7ee7\u627f\u81f3<code>Supplier<\/code>\u7684)<\/p>\n<p>\u90a3\u4e48\u4e0b\u4e00\u6b65\uff0c\u6211\u4eec\u7684\u5173\u6ce8\u70b9\u8f6c\u79fb\u5230\uff0c\u5f53<code>supplier<\/code>\u4e3a\u7a7a\u7684\u65f6\u5019\uff0c\u6e90\u7801\u662f\u5982\u4f55\u5efa\u7acb\u4e00\u4e2a<code>Factory<\/code>\u4f5c\u4e3a\u5b83\u7684\u5b9e\u4f8b\u7684\uff1f<\/p>\n<pre><code class=\"language-java\">\/\/ lazily construct a Factory\nif (factory == null) {\n    factory = new Factory(key, parameter, subKey, valuesMap);\n}<\/code><\/pre>\n<p>\u8fdb\u5165<code>Factory<\/code>\uff0c\u5b83\u5b9e\u73b0<code>Supplier<\/code>\u63a5\u53e3\uff0c\u8fd9\u4e2a\u63a5\u53e3\u4e2d\u53ea\u6709\u4e00\u4e2a\u65b9\u6cd5<code>get<\/code>\uff0c\u770b\u770b\u8fd9\u4e2a<code>get<\/code>\u65b9\u6cd5\u662f\u5982\u4f55\u5b9e\u73b0\u7684<\/p>\n<pre><code class=\"language-java\">private final class Factory implements Supplier&lt;V&gt; {\n\n    private final K key;\n    private final P parameter;\n    private final Object subKey;\n    private final ConcurrentMap&lt;Object, Supplier&lt;V&gt;&gt; valuesMap;\n\n    Factory(K key, P parameter, Object subKey,\n            ConcurrentMap&lt;Object, Supplier&lt;V&gt;&gt; valuesMap) {\n        this.key = key;\n        this.parameter = parameter;\n        this.subKey = subKey;\n        this.valuesMap = valuesMap;\n    }\n\n    @Override\n    public synchronized V get() { \/\/ serialize access\n        \/\/ re-check\n        Supplier&lt;V&gt; supplier = valuesMap.get(subKey);  \/\/\u770b\u6765\u8fd9\u91cc\u6709\u7f13\u5b58\u673a\u5236\n        if (supplier != this) {\n            \/\/ something changed while we were waiting:\n            \/\/ might be that we were replaced by a CacheValue\n            \/\/ or were removed because of failure -&gt;\n            \/\/ return null to signal WeakCache.get() to retry\n            \/\/ the loop\n            return null;\n        }\n        \/\/ else still us (supplier == this)\n\n        \/\/ create new value\n        V value = null;\n        try {\n            value = Objects.requireNonNull(valueFactory.apply(key, parameter));  \/\/\u8fd9\u91cc\u901a\u8fc7valueFactory.apply\u62ff\u5230value\n        } finally {\n            if (value == null) { \/\/ remove us on failure\n                valuesMap.remove(subKey, this);\n            }\n        }\n        \/\/ the only path to reach here is with non-null value\n        assert value != null;\n\n        \/\/ wrap value with CacheValue (WeakReference)\n        CacheValue&lt;V&gt; cacheValue = new CacheValue&lt;&gt;(value);\n\n        \/\/ put into reverseMap\n        reverseMap.put(cacheValue, Boolean.TRUE);\n\n        \/\/ try replacing us with CacheValue (this should always succeed)\n        if (!valuesMap.replace(subKey, this, cacheValue)) {\n            throw new AssertionError(&quot;Should not reach here&quot;);\n        }\n\n        \/\/ successfully replaced us with new CacheValue -&gt; return the value\n        \/\/ wrapped by it\n        return value;  \/\/\u6700\u7ec8\u8fd4\u56de\u51fa\u53bb\u7684\u5bf9\u8c61\n    }\n}<\/code><\/pre>\n<p>\u6240\u4ee5\u6700\u7ec8\uff0c\u6211\u4eec\u8fd8\u8981\u8fdb\u5165<code>valueFactory<\/code>\u4e2d\u53bb\u770b\u770b\u5b83\u7684<code>apply<\/code>\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff1a<\/p>\n<pre><code class=\"language-java\">public class Proxy implements java.io.Serializable {\n\n    private static final class ProxyClassFactory\n        implements BiFunction&lt;ClassLoader, Class&lt;?&gt;[], Class&lt;?&gt;&gt;\n    {\n        \/\/ prefix for all proxy class names\n        private static final String proxyClassNamePrefix = &quot;$Proxy&quot;;  \/\/\u7c7b\u540d\u524d\u7f00\n\n        \/\/ next number to use for generation of unique proxy class names\n        private static final AtomicLong nextUniqueNumber = new AtomicLong();  \/\/\u5168\u5c40\u8ba1\u6570\u5668\uff0c\u8ba1\u7b97\u4e0b\u4e00\u4e2a\u4ee3\u7406\u7c7b\u5bf9\u8c61\u5e94\u8be5\u4ee5\u4ec0\u4e48\u6570\u5b57\u547d\u540d\n\n        @Override\n        public Class&lt;?&gt; apply(ClassLoader loader, Class&lt;?&gt;[] interfaces) {\n\n            ...\n\n            String proxyPkg = null;     \/\/ package to define proxy class in\n            int accessFlags = Modifier.PUBLIC | Modifier.FINAL;\n\n            \/*\n             * Record the package of a non-public proxy interface so that the\n             * proxy class will be defined in the same package.  Verify that\n             * all non-public proxy interfaces are in the same package.\n             *\/\n            for (Class&lt;?&gt; intf : interfaces) {\n                int flags = intf.getModifiers();\n                if (!Modifier.isPublic(flags)) {\n                    accessFlags = Modifier.FINAL;\n                    String name = intf.getName();\n                    int n = name.lastIndexOf(&#039;.&#039;);\n                    String pkg = ((n == -1) ? &quot;&quot; : name.substring(0, n + 1));\n                    if (proxyPkg == null) {\n                        proxyPkg = pkg;\n                    } else if (!pkg.equals(proxyPkg)) {\n                        throw new IllegalArgumentException(\n                            &quot;non-public interfaces from different packages&quot;);\n                    }\n                }\n            }\n\n            if (proxyPkg == null) {\n                \/\/ if no non-public proxy interfaces, use com.sun.proxy package\n                proxyPkg = ReflectUtil.PROXY_PACKAGE + &quot;.&quot;;\n            }\n\n            \/*\n             * Choose a name for the proxy class to generate.\n             *\/\n            long num = nextUniqueNumber.getAndIncrement();\n            String proxyName = proxyPkg + proxyClassNamePrefix + num;\n\n            \/*\n             * Generate the specified proxy class.\n             *\/\n            byte[] proxyClassFile = ProxyGenerator.generateProxyClass(\n                proxyName, interfaces, accessFlags);\n            try {\n                return defineClass0(loader, proxyName,\n                                    proxyClassFile, 0, proxyClassFile.length);\n            } catch (ClassFormatError e) {\n                \/*\n                 * A ClassFormatError here means that (barring bugs in the\n                 * proxy class generation code) there was some other\n                 * invalid aspect of the arguments supplied to the proxy\n                 * class creation (such as virtual machine limitations\n                 * exceeded).\n                 *\/\n                throw new IllegalArgumentException(e.toString());\n            }\n        }\n    }<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u592a\u590d\u6742\uff0c\u8fd9\u91cc\u53ea\u8d34\u51fa\u5173\u952e\u70b9\uff08\u4f9d\u7136\u548c\u4e4b\u524d\u4e00\u6837\uff0c\u5165\u53c2\uff0c\u4f7f\u7528\u5165\u53c2\u7684\u5730\u65b9\uff0c\u8fd4\u56de\u503c\uff09\uff0c\u8fd9\u6b21\u6211\u4eec\u4ece\u8fd4\u56de\u503c\u5f00\u59cb\u53cd\u8fc7\u6765\u63a8\u65ad\uff1a<\/p>\n<pre><code class=\"language-java\">try {\n    return defineClass0(loader, proxyName,\n                        proxyClassFile, 0, proxyClassFile.length);\n} catch (ClassFormatError e) {<\/code><\/pre>\n<p>\u6e90\u7801\u76f4\u63a5\u8fd4\u56de\u4e86\u4e00\u4e2a<code>defineClass0(.....)<\/code>\uff0c\u70b9\u8fdb\u53bb\u4e00\u770b\uff0c\u5c45\u7136\u662f\u4e00\u4e2anative\u65b9\u6cd5\uff0c\u90a3\u4e48\u63a2\u7d22\u53ea\u80fd\u5230\u5934\u4e86\u3002\u5f53\u7136\uff0c\u4ece\u7ed3\u679c\u4e0a\u6765\u770b\uff0c\u5b83\u662f\u751f\u6210\u4e86\u4e00\u4e2a\u50cf\u8fd9\u6837<code>$Proxy0<\/code>\u7684<code>Class<\/code>\u4ee3\u7406\u5bf9\u8c61\uff0c\u90a3\u4e48\u4ee3\u7406<code>Class<\/code>\u5bf9\u8c61\u7684\u7279\u5f81\uff0c\u5c31\u53ea\u80fd\u4ece\u53c2\u6570\u503c\u6765\u5224\u65ad\u4e86\uff0c\u4e00\u4e2a\u4e00\u4e2a\u53c2\u6570\u770b:<\/p>\n<pre><code class=\"language-java\">private static native Class&lt;?&gt; defineClass0(ClassLoader loader, String name,\n                                                byte[] b, int off, int len);<\/code><\/pre>\n<ul>\n<li><code>ClassLoader loader<\/code> \u7c7b\u52a0\u8f7d\u5668<\/li>\n<li><code>String name<\/code> \u7c7b\u540d<\/li>\n<li><code>byte[] b, int off, int len<\/code> \u5b57\u8282\u7801IO\u6d41\u76f8\u5173\u7684\u53c2\u6570<\/li>\n<\/ul>\n<p>\u53cd\u63a8\uff0c\u7c7b\u540d\u662f\u5982\u4f55\u6765\u7684\uff1fIO\u6d41\u5b57\u8282\u7801\u76843\u4e2a\u53c2\u6570\u662f\u5e72\u4ec0\u4e48\u7528\u7684\uff1f<\/p>\n<pre><code class=\"language-java\">\/*\n * Choose a name for the proxy class to generate.\n *\/\nlong num = nextUniqueNumber.getAndIncrement();\nString proxyName = proxyPkg + proxyClassNamePrefix + num;  \/\/\u5305\u540d \u7c7b\u540d\u524d\u7f00$Proxy \u5e8f\u53f7(AtomicLong nextUniqueNumber\u81ea\u589e\u5f97\u5230)\n\n\/*\n * Generate the specified proxy class.\n *\/\nbyte[] proxyClassFile = ProxyGenerator.generateProxyClass(  \/\/\u5b57\u8282\u7801\n    proxyName, interfaces, accessFlags);  \/\/\u7c7b\u540d \u63a5\u53e3 \u63a5\u53e3\u4fee\u9970\u7b26\ntry {\n    return defineClass0(loader, proxyName,\n                        proxyClassFile, 0, proxyClassFile.length);\n} catch (ClassFormatError e) {\n    \/*\n     * A ClassFormatError here means that (barring bugs in the\n     * proxy class generation code) there was some other\n     * invalid aspect of the arguments supplied to the proxy\n     * class creation (such as virtual machine limitations\n     * exceeded).\n     *\/\n    throw new IllegalArgumentException(e.toString());\n}<\/code><\/pre>\n<p>\u800c\u4e0a\u9762\u7684\u5b57\u8282\u6570\u7ec4<code>byte[] proxyClassFile<\/code>\u5176\u5b9e\u5c31\u662f\u5b9a\u4e49\u4e86\u8fd9\u4e2a\u4ee3\u7406\u7c7b\u7684\u884c\u4e3a\uff0c\u5b83\u5e94\u8be5\u80fd\u5305\u542b\u4ec0\u4e48\u6837\u7684\u65b9\u6cd5\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5c31\u662f\u5f53\u521d\u4f20\u8fdb\u6765\u7684<code>interfaces<\/code>\u51b3\u5b9a\u7684\u3002<\/p>\n<p>OK\uff0c\u63a2\u7a76\u5b8c\u4e86\uff0c\u4ee3\u7406\u7c7b\u7684\u751f\u6210\u5df2\u7ecf\u67e5\u5b8c\uff0c\u90a3\u4e48\u56de\u5230\u8d77\u70b9<code>Class&lt;?&gt; cl = getProxyClass0(loader, intfs)<\/code>\uff0c\u6211\u4eec\u62ff\u5230\u8fd9\u4e2a\u4ee3\u7406\u7c7b\u5bf9\u8c61<code>Class&lt;?&gt; cl<\/code>\u4e4b\u540e\u80fd\u5e72\u5565? \u80af\u5b9a\u662f\u4f7f\u7528\u5b83\u6765\u53cd\u5c04\u521b\u5efa\u4ee3\u7406\u5bf9\u8c61\u554a<\/p>\n<pre><code class=\"language-java\">public static Object newProxyInstance(ClassLoader loader,\n                                      Class&lt;?&gt;[] interfaces,\n                                      InvocationHandler h)\n    throws IllegalArgumentException\n{\n    ...\n\n    \/*\n     * Look up or generate the designated proxy class.\n     *\/\n    Class&lt;?&gt; cl = getProxyClass0(loader, intfs);  \/\/\u524d\u9762\u62ff\u5230\u7684\u4ee3\u7406\u7c7b\u5bf9\u8c61$Proxy0\n\n    \/*\n     * Invoke its constructor with the designated invocation handler.\n     *\/\n    try {\n        if (sm != null) {\n            checkNewProxyPermission(Reflection.getCallerClass(), cl);\n        }\n\n        final Constructor&lt;?&gt; cons = cl.getConstructor(constructorParams);  \/\/\u53d6\u5f97\u6784\u9020\u51fd\u6570\n        final InvocationHandler ih = h;  \/\/\u8fd9\u662f\u5916\u90e8\u4f20\u8fdb\u6765\u7684InvocationHandler\u5bf9\u8c61\n        if (!Modifier.isPublic(cl.getModifiers())) {\n            AccessController.doPrivileged(new PrivilegedAction&lt;Void&gt;() {\n                public Void run() {\n                    cons.setAccessible(true);\n                    return null;\n                }\n            });\n        }\n        return cons.newInstance(new Object[]{h});  \/\/\u8fd9\u91cc\u53cd\u5c04\u521b\u5efa\u4e00\u4e2a\u4ee3\u7406\u7c7b\u5bf9\u8c61\u5e76return\u51fa\u53bb\uff0c\u6ce8\u610f\u53c2\u6570h\n    } catch (IllegalAccessException|InstantiationException e) {\n        throw new InternalError(e.toString(), e);\n    } catch (InvocationTargetException e) {\n        Throwable t = e.getCause();\n        if (t instanceof RuntimeException) {\n            throw (RuntimeException) t;\n        } else {\n            throw new InternalError(t.toString(), t);\n        }\n    } catch (NoSuchMethodException e) {\n        throw new InternalError(e.toString(), e);\n    }\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\u6709\u4e00\u4e2a\u5173\u952e\u70b9\uff0c\u53c2\u6570<code>h<\/code>\uff0c\u5b83\u662f\u6211\u4eec\u4ece\u5916\u90e8\u4f20\u5165\u7684<code>InvocationHandler<\/code>\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u5b9a\u4e49\u4e86\u6211\u4eec\u5bf9\u88ab\u4ee3\u7406\u7c7b\u7684\u65b9\u6cd5\u589e\u5f3a\uff0c\u5373\u6211\u4eec\u53ef\u4ee5\u5728\u88ab\u4ee3\u7406\u7c7b\u7684\u6307\u5b9a\u65b9\u6cd5\uff08\u6216\u8005\u6240\u6709\u65b9\u6cd5\uff09\u6267\u884c\u7684\u8fc7\u7a0b\u4e2d\uff0c\u52a0\u5165\u524d\u7f6e\u6216\u8005\u540e\u7f6e\u8fc7\u7a0b\uff0c\u8fd8\u53ef\u4ee5\u8bbe\u7f6e\u6761\u4ef6\uff0c\u6839\u636e\u6761\u4ef6\u51b3\u5b9a\u88ab\u4ee3\u7406\u7c7b\u7684\u65b9\u6cd5\u8981\u4e0d\u8981\u6267\u884c\u3002\u90a3\u4e48\u5b83\u662f\u5982\u4f55\u53d1\u6325\u4f5c\u7528\u7684\u5462\uff1f<\/p>\n<pre><code class=\"language-java\">\/*\n * Generate the specified proxy class.\n *\/\nbyte[] proxyClassFile = ProxyGenerator.generateProxyClass(  \/\/\u5b57\u8282\u7801\n    proxyName, interfaces, accessFlags);  \/\/\u7c7b\u540d \u63a5\u53e3 \u63a5\u53e3\u4fee\u9970\u7b26\ntry {\n    return defineClass0(loader, proxyName,\n                        proxyClassFile, 0, proxyClassFile.length);\n} catch (ClassFormatError e) {<\/code><\/pre>\n<p>\u8fd8\u8bb0\u5f97\u8fd9\u4e2a\u5b57\u8282\u7801IO\u6d41\u76f8\u5173\u7684\u53c2\u6570\u4e48\uff0c\u5176\u5b9e\u5982\u679c\u628a\u8fd9\u4e2a<code>byte[]<\/code>\u901a\u8fc7IO\u6d41\u8f93\u51fa\u5230\u672c\u5730\u6587\u4ef6\uff0c\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a<code>.class<\/code>\u6587\u4ef6\uff0c\u53cd\u7f16\u8bd1\u51fa\u6765\uff0c\u5c31\u662f\u4e00\u4e2a<code>.java<\/code>\u6587\u4ef6\uff0c\u7c7b\u4f3c\u4e0b\u9762\u7684\uff1a<\/p>\n<pre><code class=\"language-java\">public final class $Proxy0\n  extends Proxy  \/\/\u6d3e\u751f\u81eaProxy\u7c7b\n  implements AaFactory  \/\/\u5b9e\u73b0\u4e86\u4e1a\u52a1\u63a5\u53e3\n{\n  private static Method m1;  \/\/\u5404\u79cd\u65b9\u6cd5\n  private static Method m8;\n  private static Method m3;\n  private static Method m2;\n  private static Method m6;\n  private static Method m5;\n  private static Method m7;\n  private static Method m9;\n  private static Method m0;\n  private static Method m4;\n\n  public final void saleManTools(String paramString)\n    throws\n  {\n    try\n    {\n      this.h.invoke(this, m3, new Object[] { paramString });  \/\/m3\u662f\u5176\u4e2d\u4e00\u4e2a\u4ee3\u7406\u65b9\u6cd5\n      return;\n    }<\/code><\/pre>\n<p>\u8fd9\u91cc\u4fdd\u5b58\u4e86\u6765\u81ea\u5916\u90e8\u63a5\u53e3\u7684\u5404\u79cd\u65b9\u6cd5(\u547d\u540d\u4e5f\u662f\u5f88\u803f\u76f4 m1,m2&#8230;..)\uff0c\u5e76\u4e14\u8fd9\u4e2a<code>saleManTools<\/code>\uff0c\u5c31\u662f\u6211\u4eecdemo\u4e2d\u7684\u63a5\u53e3\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u5b83\u53cd\u5c04\u6267\u884c\u4e86\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u8fd9\u91cc\u7684<code>h<\/code>\uff0c\u5176\u5b9e\u5c31\u662f\u6211\u4eec\u6700\u521d\u7ed9\u7684<code>invocationHandler<\/code>\u5bf9\u8c61\uff1a<\/p>\n<pre><code class=\"language-java\">public class DynamicProxyInstance implements InvocationHandler {\n\n    \/**\n     * \u83b7\u5f97\u52a8\u6001\u4ee3\u7406\n     * \n     * @return\n     *\/\n    public Object getProxyInstance() {\n        return Proxy.newProxyInstance(realInstance.getClass().getClassLoader(), realInstance.getClass().getInterfaces(),\n                this);  \/\/this\u5373InvocationHandler\u5bf9\u8c61\n\n    }\n\n    @Override\n    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {\n        doBefore();\n        Object res = method.invoke(realInstance, args);\n        doAfter();\n        return res;\n    }<\/code><\/pre>\n<p>\u5f53\u6211\u4eec\u8c03\u7528\u4ee3\u7406\u5bf9\u8c61\u7684\u4e1a\u52a1\u65b9\u6cd5\u65f6\uff0c\u5176\u5b9e\u4e5f\u6267\u884c\u4e86<code>invocationHandler<\/code>\u7684<code>invoke<\/code>\u65b9\u6cd5\uff0c\u90a3\u4e48\u5bf9\u88ab\u4ee3\u7406\u5bf9\u8c61\u7684\u65b9\u6cd5\u7684\u589e\u5f3a\u6216\u8005\u9650\u5236\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u91cd\u5199<code>invoke<\/code>\u65f6\u968f\u610f\u5904\u7406\u4e86\u3002<\/p>\n<h2>\u7ed3\u8bed<\/h2>\n<p>\u5c31\u5199\u5230\u8fd9\u91cc\u5427\uff0c\u505a\u4e2a\u603b\u7ed3\uff0c<code>java.lang.reflect.Proxy<\/code>\u662fJDK\u63d0\u4f9b\u7684\u52a8\u6001\u4ee3\u7406\u7c7b\uff0c\u6211\u4eec\u63a2\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u81f3\u5c11\u53d1\u73b0\u4e862\u5904\u4f7f\u7528\u53cd\u5c04\u7684\u5730\u65b9\uff0c\u4e00\u5904\u662f \u53cd\u5c04\u6784\u9020\u51fd\u6570\uff0c\u7136\u540e\u6267\u884c\u5b83\uff0c\u521b\u5efa\u4ee3\u7406\u7c7b\u5bf9\u8c61\u7684\u8fc7\u7a0b\uff0c\u4e00\u5904\u662f\u4f7f\u7528\u4ee3\u7406\u7c7b\u5bf9\u8c61\uff0c\u53cd\u5c04\u521b\u5efa\u4ee3\u7406\u5bf9\u8c61\u7684\u8fc7\u7a0b\uff08\u4ee3\u7406\u7c7b\u5bf9\u8c61\u548c\u4ee3\u7406\u5bf9\u8c61\u662f\u4e0d\u4e00\u6837\u7684\uff09\u3002<\/p>\n<p>\u4f7f\u7528\u4e86\u53cd\u5c04\uff0c\u6548\u7387\u81ea\u7136\u6bd4\u4e0d\u4e0a\u9759\u6001\u4ee3\u7406\uff0c\u4f46\u662f\u5b83\u80fd\u4fdd\u8bc1\u771f\u5b9e\u5bf9\u8c61\u65e0\u9650\u6269\u5c55\u7684\u65f6\u5019\uff0c\u4ee3\u7406\u7c7b\u4e0d\u7528\u505a\u4fee\u6539\uff0c\u53ea\u9700\u8981\u5728\u521b\u5efa\u4ee3\u7406\u7c7b\u7684\u65f6\u5019\uff0c\u4f20\u5165\u4e0d\u540c\u7684\u771f\u5b9e\u5bf9\u8c61\u5373\u53ef\uff0c\u8fd9\u79cd\u505a\u6cd5\u624d\u7b26\u5408\u7a0b\u5e8f\u8bbe\u8ba1\u7684\u5f00\u95ed\u539f\u5219\uff0c\u5bf9\u6269\u5c55\u5f00\u653e\uff0c\u5bf9\u4fee\u6539\u5173\u95ed\u3002<\/p>\n<p>\u6211\u4eec\u53bb\u4f7f\u7528\u4e00\u4e2a\u7c7b\uff0c\u4e00\u5b9a\u4f1a\u5148\u548c\u83b7\u5f97\u8fd9\u4e2a\u7c7b\u7684<code>class<\/code>\u5bf9\u8c61\uff0c\u5229\u7528<code>Proxy<\/code>\u53ef\u4ee5\u52a8\u6001\u53bb\u521b\u5efa<code>Proxy0<\/code>\uff0c<code>proxy1<\/code>\u8fd9\u6837\u5b50\u7684<code>class<\/code>\u5bf9\u8c61\u3002\u7136\u540e\u624d\u5229\u7528\u8fd9\u4e2a\u5bf9\u8c61\u53cd\u5c04\u6267\u884c\u6784\u9020\u51fd\u6570\u53bb\u521b\u5efa<code>proxy0<\/code>\u5bf9\u8c61\u3002<\/p>\n<p>\u672c\u6587\u8f6c\u8f7d\u81f3\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/www.jianshu.com\/p\/647b7cd0a7d9\">https:\/\/www.jianshu.com\/p\/647b7cd0a7d9<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Proxy\u4ecb\u7ecd \u4e4b\u524d\u5728\u7814\u7a76hook\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u5230\u4e86Proxy\u52a8\u6001\u4ee3\u7406\u3002\u5b83\u662f\u6765\u81eaJDK\u7684\u4e00\u4e2a\u7c7b\uff1a package [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[367],"class_list":["post-1493","post","type-post","status-publish","format-standard","hentry","category-java-basic","tag-367"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1493","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1493"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1493\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}