{"id":11773,"date":"2023-11-28T22:33:36","date_gmt":"2023-11-28T14:33:36","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=11773"},"modified":"2023-12-11T14:20:08","modified_gmt":"2023-12-11T06:20:08","slug":"spring-zhong-objectfactory-le-jie-ma","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/spring-zhong-objectfactory-le-jie-ma\/","title":{"rendered":"Spring \u4e2d ObjectFactory \u4e86\u89e3\u5417\uff1f"},"content":{"rendered":"<p>\u662f\u7684\uff0c<code>ObjectFactory<\/code>\u662fSpring\u6846\u67b6\u4e2d\u7684\u4e00\u4e2a\u63a5\u53e3\uff0c\u5b83\u7528\u4e8e\u521b\u5efa\u5bf9\u8c61\u7684\u5de5\u5382\u3002\u4e3b\u8981\u7528\u4e8e\u89e3\u51b3\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\uff0c\u7531\u4e8e\u5faa\u73af\u4f9d\u8d56\u7b49\u95ee\u9898\u5bfc\u81f4\u7684\u65e0\u6cd5\u901a\u8fc7\u6784\u9020\u51fd\u6570\u76f4\u63a5\u6ce8\u5165\u7684\u60c5\u51b5\u3002<code>ObjectFactory<\/code>\u7684\u5b9e\u73b0\u7c7b\u901a\u5e38\u7531Spring\u5bb9\u5668\u63d0\u4f9b\u3002<\/p>\n<p><code>ObjectFactory<\/code>\u63a5\u53e3\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">public interface ObjectFactory&lt;T&gt; {\n    T getObject() throws BeansException;\n}\n<\/code><\/pre>\n<p>\u5b9e\u73b0\u4e86<code>ObjectFactory<\/code>\u63a5\u53e3\u7684\u7c7b\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u7279\u5b9a\u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u901a\u5e38\uff0cSpring\u5728\u89e3\u51b3\u5faa\u73af\u4f9d\u8d56\u65f6\uff0c\u4f1a\u901a\u8fc7<code>ObjectFactory<\/code>\u6765\u5ef6\u8fdf\u521b\u5efa\u5bf9\u8c61\uff0c\u4ee5\u907f\u514d\u5faa\u73af\u5f15\u7528\u5bfc\u81f4\u7684\u95ee\u9898\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u5728Spring\u4e2d\u4f7f\u7528<code>ObjectFactory<\/code>\uff1a<\/p>\n<pre><code class=\"language-java\">import org.springframework.beans.factory.ObjectFactory;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Component;\n\n@Component\npublic class ClientBean {\n\n    private final ObjectFactory&lt;ServiceBean&gt; serviceBeanFactory;\n\n    @Autowired\n    public ClientBean(ObjectFactory&lt;ServiceBean&gt; serviceBeanFactory) {\n        this.serviceBeanFactory = serviceBeanFactory;\n    }\n\n    public void invokeService() {\n        \/\/ \u4f7f\u7528ObjectFactory\u83b7\u53d6ServiceBean\u5b9e\u4f8b\n        ServiceBean serviceBean = serviceBeanFactory.getObject();\n        \/\/ \u8c03\u7528ServiceBean\u7684\u65b9\u6cd5\n        serviceBean.doSomething();\n    }\n}\n\n@Component\npublic class ServiceBean {\n\n    private final ClientBean clientBean;\n\n    @Autowired\n    public ServiceBean(ClientBean clientBean) {\n        this.clientBean = clientBean;\n    }\n\n    public void doSomething() {\n        \/\/ \u4e1a\u52a1\u903b\u8f91\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\uff0c<code>ClientBean<\/code>\u548c<code>ServiceBean<\/code>\u4e4b\u95f4\u5b58\u5728\u5faa\u73af\u4f9d\u8d56\uff0c\u4f46\u901a\u8fc7\u4f7f\u7528<code>ObjectFactory<\/code>\uff0c\u5b83\u4eec\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u6b63\u786e\u5730\u83b7\u53d6\u5bf9\u65b9\u7684\u5b9e\u4f8b\uff0c\u907f\u514d\u4e86\u5faa\u73af\u4f9d\u8d56\u95ee\u9898\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>ObjectFactory<\/code>\u901a\u5e38\u5728\u8fd0\u884c\u65f6\u624d\u4f1a\u88ab\u5b9e\u9645\u8c03\u7528\uff0c\u56e0\u6b64\u5728\u4f7f\u7528\u65f6\u9700\u8981\u6ce8\u610f\u53ef\u80fd\u7684\u6027\u80fd\u5f71\u54cd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u662f\u7684\uff0cObjectFactory\u662fSpring\u6846\u67b6\u4e2d\u7684\u4e00\u4e2a\u63a5\u53e3\uff0c\u5b83\u7528\u4e8e\u521b\u5efa\u5bf9\u8c61\u7684\u5de5\u5382\u3002\u4e3b\u8981\u7528\u4e8e\u89e3\u51b3\u5728\u67d0\u4e9b\u573a\u666f\u4e0b [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[],"class_list":["post-11773","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11773","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=11773"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11773\/revisions"}],"predecessor-version":[{"id":37179,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11773\/revisions\/37179"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=11773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=11773"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=11773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}