{"id":1450,"date":"2016-12-07T22:31:50","date_gmt":"2016-12-07T14:31:50","guid":{"rendered":"http:\/\/coderbee.net\/?p=1450"},"modified":"2016-12-07T22:35:58","modified_gmt":"2016-12-07T14:35:58","slug":"%e6%8e%a5%e5%8f%a3%e4%b8%8espring%e8%87%aa%e5%8a%a8%e6%b3%a8%e5%85%a5","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/java\/20161207\/1450","title":{"rendered":"\u63a5\u53e3\u4e0eSpring\u81ea\u52a8\u6ce8\u5165"},"content":{"rendered":"<h2>\u63a5\u53e3\u4e0e\u81ea\u52a8\u6ce8\u5165<\/h2>\n<p>\u6709\u4e1a\u52a1\u63a5\u53e3 <code>IService<\/code> \u548c\u4e24\u79cd\u4e1a\u52a1\u903b\u8f91\u7684\u5b9e\u73b0 <code>Aservice<\/code>, <code>Bservice<\/code> \u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">public interface IService {}\n\n@Component\npublic class Aservice implements IService {}\n\n@Component\npublic class Bservice implements IService {}\n<\/code><\/pre>\n<p>\u91c7\u7528\u4e0b\u9762\u7684\u65b9\u5f0f\u81ea\u52a8\u6ce8\u5165\u65f6\uff0c\u4f1a\u62a5\u9519\uff1a<\/p>\n<pre><code class=\"java\">public class ManageService {\n    @Autowired\n    private Aservice aservice;\n\n    @Autowired\n    private Bservice bservice;\n\n    \/\/ ...\n}\n<\/code><\/pre>\n<p>\u5f02\u5e38\u4fe1\u606f\u7c7b\u4f3c\u4e3a\uff1a<code>nested exception is java.lang.IllegalArgumentException: Can not set xxx.ManageService field xxx.ManageService.aservice to com.sun.proxy.$Proxy48<\/code><\/p>\n<p><!--more--><\/p>\n<p>\u8fd9\u662f\u56e0\u4e3a\uff0cSpring \u7ba1\u7406\u7684 Bean\uff0c\u5982\u679c\u8fd9\u4e2a Bean \u5b9e\u73b0\u4e86\u67d0\u4e2a\u63a5\u53e3\uff0c\u90a3\u4e48\u5c31\u7528 JDK \u7684 Proxy \u52a8\u6001\u4ee3\u7406\u673a\u5236\u6765\u751f\u6210\u4ee3\u7406\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u901a\u8fc7 cglib \u5b50\u7c7b\u5316\u51fa\u4e00\u4e2a\u4ee3\u7406\u5bf9\u8c61\u3002<\/p>\n<p>\u8fd9\u65f6\uff0c\u5bf9\u4e8e IService\u3001 Aservice \u7c7b\u7684\u5c42\u6b21\u7ed3\u6784\u5982\u4e0b\uff0c<\/p>\n<pre><code>    IService\n    ^       ^\n    |       |\nAservice    $Proxy48\n<\/code><\/pre>\n<p>\u5373\u4e24\u4e2a\u4ee3\u7406\u5bf9\u8c61\u90fd\u5b9e\u73b0\u4e86\u63a5\u53e3\u7c7b\uff0c\u4f46\u5374\u662f\u4e0d\u540c\u7684\u7c7b\u578b\uff0c\u56e0\u6b64\u6ce8\u5165\u5931\u8d25\u3002<\/p>\n<p>\u53ef\u4ee5 Qualifer \u6765\u5b9e\u73b0\u81ea\u52a8\u6ce8\u5165\uff0c\u6ce8\u610f\u5c5e\u6027\u7684\u7c7b\u578b\u662f\u63a5\u53e3\u7c7b\uff1a<\/p>\n<pre><code class=\"java\">public class ManageService {\n    @Autowired\n    @Qualifier(\"aservice\")\n    private IService aservice;\n\n    @Autowired\n    @Qualifier(\"bservice\")\n    private ISservice bservice;\n\n    \/\/ ...\n}\n<\/code><\/pre>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a5\u53e3\u4e0e\u81ea\u52a8\u6ce8\u5165 \u6709\u4e1a\u52a1\u63a5\u53e3 IService \u548c\u4e24\u79cd\u4e1a\u52a1\u903b\u8f91\u7684\u5b9e\u73b0 Aservi &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/java\/20161207\/1450\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[172,263],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1450"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=1450"}],"version-history":[{"count":2,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1450\/revisions"}],"predecessor-version":[{"id":1452,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1450\/revisions\/1452"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}