{"id":25397,"date":"2023-04-29T17:23:11","date_gmt":"2023-04-29T08:23:11","guid":{"rendered":"https:\/\/m9js.shop\/blog\/?p=25397"},"modified":"2023-04-29T18:17:05","modified_gmt":"2023-04-29T09:17:05","slug":"implementing-dynamic-proxies-using-java-interfaces-and-reflection","status":"publish","type":"post","link":"https:\/\/m9js.shop\/blog\/development\/implementing-dynamic-proxies-using-java-interfaces-and-reflection","title":{"rendered":"\uc790\ubc14 \uc778\ud130\ud398\uc774\uc2a4\uc640 \ub9ac\ud50c\ub809\uc158\uc744 \uc0ac\uc6a9\ud55c \ub3d9\uc801 \ud504\ub85d\uc2dc \uad6c\ud604\ud558\uae30"},"content":{"rendered":"

\uc790\ubc14 \uc778\ud130\ud398\uc774\uc2a4\uc640 \ub9ac\ud50c\ub809\uc158\uc744 \uc0ac\uc6a9\ud55c \ub3d9\uc801 \ud504\ub85d\uc2dc \uad6c\ud604\ud558\uae30===
\n\uc790\ubc14\uc5d0\uc11c\ub294 \uc778\ud130\ud398\uc774\uc2a4\uc640 \ub9ac\ud50c\ub809\uc158\uc744 \uc0ac\uc6a9\ud558\uc5ec \ub3d9\uc801 \ud504\ub85d\uc2dc\ub97c \uad6c\ud604\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \ub3d9\uc801 \ud504\ub85d\uc2dc\ub780 \ub7f0\ud0c0\uc784 \uc2dc\uc5d0 \uc778\ud130\ud398\uc774\uc2a4\ub97c \uad6c\ud604\ud558\ub294 \ud074\ub798\uc2a4\ub97c \ub3d9\uc801\uc73c\ub85c \uc0dd\uc131\ud558\uc5ec \uc778\ud130\ud398\uc774\uc2a4\uc758 \uba54\uc11c\ub4dc \ud638\ucd9c\uc744 \uac00\ub85c\ucc44\uc11c \uc6d0\ud558\ub294 \ub85c\uc9c1\uc744 \uc218\ud589\ud558\ub294 \uac83\uc744 \ub9d0\ud569\ub2c8\ub2e4. \uc774 \uae30\uc220\uc740 AOP(Aspect Oriented Programming)\uc5d0\uc11c \ub9ce\uc774 \uc0ac\uc6a9\ub418\uba70, \ubcf4\uc548, \ud2b8\ub79c\uc7ad\uc158, \ub85c\uae45 \ub4f1\uc758 \uae30\ub2a5\uc744 \uad6c\ud604\ud560 \ub54c \uc720\uc6a9\ud569\ub2c8\ub2e4.<\/p>\n

\uc790\ubc14 \uc778\ud130\ud398\uc774\uc2a4\uc640 \ub9ac\ud50c\ub809\uc158\uc758 \uac1c\ub150 \uc774\ud574<\/h2>\n

\uc790\ubc14 \uc778\ud130\ud398\uc774\uc2a4\ub294 \uc77c\uc885\uc758 \uacc4\uc57d(contract)\uc73c\ub85c, \uc778\ud130\ud398\uc774\uc2a4\ub97c \uad6c\ud604\ud558\ub294 \ud074\ub798\uc2a4\ub294 \ubc18\ub4dc\uc2dc \uc778\ud130\ud398\uc774\uc2a4\uc5d0\uc11c \uc815\uc758\ud55c \uba54\uc11c\ub4dc\ub97c \uad6c\ud604\ud574\uc57c \ud569\ub2c8\ub2e4. \uc774\ub97c \ud1b5\ud574 \ud074\ub798\uc2a4 \uac04\uc758 \uacb0\ud569\ub3c4\ub97c \ub0ae\ucd94\uace0, \uc720\uc5f0\uc131\uc744 \ud655\ubcf4\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \ub9ac\ud50c\ub809\uc158\uc740 \ub7f0\ud0c0\uc784 \uc2dc\uc5d0 \ud074\ub798\uc2a4\uc758 \uc815\ubcf4\ub97c \uc5bb\uc5b4\uc624\ub294 \uae30\uc220\ub85c, \ud074\ub798\uc2a4\uc758 \uc0dd\uc131\uc790, \ud544\ub4dc, \uba54\uc11c\ub4dc \ub4f1\uc744 \ub3d9\uc801\uc73c\ub85c \uc870\uc791\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n

\ub3d9\uc801 \ud504\ub85d\uc2dc\uc758 \uc5ed\ud560\uacfc \uad6c\ud604 \ubc29\ubc95<\/h2>\n

\ub3d9\uc801 \ud504\ub85d\uc2dc\ub294 \uc778\ud130\ud398\uc774\uc2a4\ub97c \uad6c\ud604\ud558\ub294 \ud074\ub798\uc2a4\ub97c \ub3d9\uc801\uc73c\ub85c \uc0dd\uc131\ud558\uc5ec, \uc778\ud130\ud398\uc774\uc2a4\uc758 \uba54\uc11c\ub4dc \ud638\ucd9c\uc744 \uac00\ub85c\ucc44\uc11c \ucd94\uac00\uc801\uc778 \ub85c\uc9c1\uc744 \uc218\ud589\ud569\ub2c8\ub2e4. \uc774\ub97c \uc704\ud574\uc11c\ub294 \uc790\ubc14\uc5d0\uc11c \uc81c\uacf5\ud558\ub294 Proxy \ud074\ub798\uc2a4\ub97c \uc0ac\uc6a9\ud558\uba74 \ub429\ub2c8\ub2e4. Proxy \ud074\ub798\uc2a4\ub294 \uc778\ud130\ud398\uc774\uc2a4\uc640 InvocationHandler \uac1d\uccb4\ub97c \uc0dd\uc131\uc790 \uc778\uc790\ub85c \ubc1b\uc544, \uc9c0\uc815\ub41c \uc778\ud130\ud398\uc774\uc2a4\ub97c \uad6c\ud604\ud558\ub294 \ud074\ub798\uc2a4\ub97c \ub3d9\uc801\uc73c\ub85c \uc0dd\uc131\ud569\ub2c8\ub2e4. InvocationHandler \uac1d\uccb4\ub294 invoke() \uba54\uc11c\ub4dc\ub97c \uad6c\ud604\ud558\uc5ec, \uc778\ud130\ud398\uc774\uc2a4\uc758 \uba54\uc11c\ub4dc \ud638\ucd9c\uc744 \uac00\ub85c\ucc44\uc11c \ud544\uc694\ud55c \ub85c\uc9c1\uc744 \uc218\ud589\ud569\ub2c8\ub2e4.<\/p>\n

\uc790\ubc14 \uc778\ud130\ud398\uc774\uc2a4\uc640 \ub9ac\ud50c\ub809\uc158\uc744 \ud65c\uc6a9\ud55c \ub3d9\uc801 \ud504\ub85d\uc2dc \uad6c\ud604\ud558\uae30<\/h2>\n

\ub3d9\uc801 \ud504\ub85d\uc2dc\ub97c \uad6c\ud604\ud558\uae30 \uc704\ud574\uc11c\ub294 \uba3c\uc800 \uc778\ud130\ud398\uc774\uc2a4\uc640 InvocationHandler \uac1d\uccb4\ub97c \uc815\uc758\ud574\uc57c \ud569\ub2c8\ub2e4. \ub2e4\uc74c\uc740 SampleInterface\uc640 SampleInvocationHandler \ud074\ub798\uc2a4\uc758 \uc608\uc2dc\uc785\ub2c8\ub2e4.<\/p>\n

public interface SampleInterface {\n    void doSomething();\n}\n\npublic class SampleInvocationHandler implements InvocationHandler {\n    private Object target;\n\n    public SampleInvocationHandler(Object target) {\n        this.target = target;\n    }\n\n    @Override\n    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {\n        \/\/ \ucd94\uac00\uc801\uc778 \ub85c\uc9c1\uc744 \uc218\ud589\n        System.out.println(\"Before method call\");\n\n        Object result = method.invoke(target, args);\n\n        System.out.println(\"After method call\");\n\n        return result;\n    }\n}<\/code><\/pre>\n

\uc774\uc81c Proxy \ud074\ub798\uc2a4\ub97c \uc0ac\uc6a9\ud558\uc5ec \ub3d9\uc801 \ud504\ub85d\uc2dc \uac1d\uccb4\ub97c \uc0dd\uc131\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n

SampleInterface target = new SampleInterfaceImpl();\nSampleInvocationHandler invocationHandler = new SampleInvocationHandler(target);\nSampleInterface proxy = (SampleInterface) Proxy.newProxyInstance(\n        target.getClass().getClassLoader(),\n        new Class[] { SampleInterface.class },\n        invocationHandler);<\/code><\/pre>\n

\uc704\uc758 \ucf54\ub4dc\uc5d0\uc11c\ub294 SampleInterfaceImpl \ud074\ub798\uc2a4\uac00 SampleInterface\ub97c \uad6c\ud604\ud558\ub294 \ud074\ub798\uc2a4\uc785\ub2c8\ub2e4. Proxy.newProxyInstance() \uba54\uc11c\ub4dc\ub294 \uc778\ud130\ud398\uc774\uc2a4\uc758 \ud074\ub798\uc2a4 \ub85c\ub354\uc640 \uc778\ud130\ud398\uc774\uc2a4 \ubc30\uc5f4, InvocationHandler \uac1d\uccb4\ub97c \uc778\uc790\ub85c \ubc1b\uc544, \ub3d9\uc801 \ud504\ub85d\uc2dc \uac1d\uccb4\ub97c \uc0dd\uc131\ud569\ub2c8\ub2e4. \uc0dd\uc131\ub41c \ud504\ub85d\uc2dc \uac1d\uccb4\ub294 SampleInterface \ud0c0\uc785\uc73c\ub85c \uce90\uc2a4\ud305\ud558\uc5ec \uc0ac\uc6a9\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n

\uc790\ubc14 \uc778\ud130\ud398\uc774\uc2a4\uc640 \ub9ac\ud50c\ub809\uc158\uc744 \uc0ac\uc6a9\ud55c \ub3d9\uc801 \ud504\ub85d\uc2dc \uad6c\ud604\ud558\uae30===
\n\uc790\ubc14\uc5d0\uc11c\ub294 \uc778\ud130\ud398\uc774\uc2a4\uc640 \ub9ac\ud50c\ub809\uc158\uc744 \uc0ac\uc6a9\ud558\uc5ec \ub3d9\uc801 \ud504\ub85d\uc2dc\ub97c \uad6c\ud604\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \ub3d9\uc801 \ud504\ub85d\uc2dc\ub294 \uc778\ud130\ud398\uc774\uc2a4\uc758 \uba54\uc11c\ub4dc \ud638\ucd9c\uc744 \uac00\ub85c\ucc44\uc11c \ucd94\uac00\uc801\uc778 \ub85c\uc9c1\uc744 \uc218\ud589\ud558\ub294\ub370, \uc774\ub97c \ud1b5\ud574 AOP\uc640 \uac19\uc740 \uae30\ub2a5\uc744 \uad6c\ud604\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. Proxy \ud074\ub798\uc2a4\ub97c \uc0ac\uc6a9\ud558\uc5ec \ub3d9\uc801 \ud504\ub85d\uc2dc \uac1d\uccb4\ub97c \uc0dd\uc131\ud560 \uc218 \uc788\uc73c\uba70, InvocationHandler \uac1d\uccb4\ub97c \uad6c\ud604\ud558\uc5ec \ud504\ub85d\uc2dc \uac1d\uccb4\uc5d0\uc11c \uc218\ud589\ud560 \ub85c\uc9c1\uc744 \uc815\uc758\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \uc774\ub97c \ud1b5\ud574 \uc790\ubc14 \uc560\ud50c\ub9ac\ucf00\uc774\uc158\uc5d0\uc11c \uc720\uc5f0\ud558\uace0 \ud655\uc7a5\uc131 \uc788\ub294 \ucf54\ub4dc\ub97c \uc791\uc131\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n","protected":false},"excerpt":{"rendered":"

\uc790\ubc14 \uc778\ud130\ud398\uc774\uc2a4\uc640 \ub9ac\ud50c\ub809\uc158\uc744 \uc774\uc6a9\ud55c \ub3d9\uc801 \ud504\ub85d\uc2dc \uad6c\ud604\ud558\uae30<\/p>\n","protected":false},"author":1,"featured_media":12633,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1957],"tags":[2500,45,2637,2188,2584],"class_list":["post-25397","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","tag-method","tag-new","tag-proxy","tag-public","tag-system"],"acf":[],"_links":{"self":[{"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/posts\/25397","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/comments?post=25397"}],"version-history":[{"count":0,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/posts\/25397\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/media\/12633"}],"wp:attachment":[{"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/media?parent=25397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/categories?post=25397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/tags?post=25397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}