{"id":12096,"date":"2023-11-28T22:43:17","date_gmt":"2023-11-28T14:43:17","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=12096"},"modified":"2023-12-11T15:24:05","modified_gmt":"2023-12-11T07:24:05","slug":"spring-zhong-bu-tong-fang-shi-de-zi-dong-zhuang-pe-2","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/spring-zhong-bu-tong-fang-shi-de-zi-dong-zhuang-pe-2\/","title":{"rendered":"Spring\u4e2d\u4e0d\u540c\u65b9\u5f0f\u7684\u81ea\u52a8\u88c5\u914d\u4e86\u89e3\u5417\uff1f"},"content":{"rendered":"<p>\u5728Spring\u4e2d\uff0c\u81ea\u52a8\u88c5\u914d\u662f\u4e00\u79cd\u901a\u8fc7Spring\u5bb9\u5668\u81ea\u52a8\u5c06Bean\u7684\u4f9d\u8d56\u5173\u7cfb\u5efa\u7acb\u8d77\u6765\u7684\u673a\u5236\u3002Spring\u63d0\u4f9b\u4e86\u51e0\u79cd\u4e0d\u540c\u7684\u81ea\u52a8\u88c5\u914d\u65b9\u5f0f\uff0c\u8ba9\u5f00\u53d1\u8005\u80fd\u591f\u6839\u636e\u5177\u4f53\u7684\u60c5\u51b5\u9009\u62e9\u9002\u5f53\u7684\u65b9\u5f0f\u3002<\/p>\n<p>\u4ee5\u4e0b\u662fSpring\u4e2d\u4e0d\u540c\u65b9\u5f0f\u7684\u81ea\u52a8\u88c5\u914d\uff1a<\/p>\n<ol>\n<li>\n<strong>\u6309\u7c7b\u578b\u81ea\u52a8\u88c5\u914d\uff08byType\uff09\uff1a<\/strong><\/p>\n<ul>\n<li>Spring\u5bb9\u5668\u4f1a\u81ea\u52a8\u5c06\u5c5e\u6027\u7684\u7c7b\u578b\u4e0e\u5bb9\u5668\u4e2d\u7684Bean\u8fdb\u884c\u5339\u914d\u3002\u5982\u679c\u5bb9\u5668\u4e2d\u5b58\u5728\u4e00\u4e2a\u4e0e\u5c5e\u6027\u7c7b\u578b\u5339\u914d\u7684Bean\uff0c\u5c31\u5c06\u8be5Bean\u6ce8\u5165\u5230\u5c5e\u6027\u4e2d\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-xml\">&lt;bean id=&quot;myBean&quot; class=&quot;com.example.MyBean&quot; autowire=&quot;byType&quot;&gt;\n    &lt;!-- properties and configurations --&gt;\n&lt;\/bean&gt;\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u6309\u540d\u79f0\u81ea\u52a8\u88c5\u914d\uff08byName\uff09\uff1a<\/strong><\/p>\n<ul>\n<li>Spring\u5bb9\u5668\u4f1a\u81ea\u52a8\u5c06\u5c5e\u6027\u7684\u540d\u79f0\u4e0e\u5bb9\u5668\u4e2d\u7684Bean\u8fdb\u884c\u5339\u914d\u3002\u5982\u679c\u5bb9\u5668\u4e2d\u5b58\u5728\u4e00\u4e2a\u4e0e\u5c5e\u6027\u540d\u5339\u914d\u7684Bean\uff0c\u5c31\u5c06\u8be5Bean\u6ce8\u5165\u5230\u5c5e\u6027\u4e2d\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-xml\">&lt;bean id=&quot;myBean&quot; class=&quot;com.example.MyBean&quot; autowire=&quot;byName&quot;&gt;\n    &lt;!-- properties and configurations --&gt;\n&lt;\/bean&gt;\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u6784\u9020\u51fd\u6570\u81ea\u52a8\u88c5\u914d\uff08constructor\uff09\uff1a<\/strong><\/p>\n<ul>\n<li>Spring\u5bb9\u5668\u4f1a\u81ea\u52a8\u901a\u8fc7\u6784\u9020\u51fd\u6570\u6ce8\u5165Bean\u7684\u4f9d\u8d56\u5173\u7cfb\u3002\u5728\u4f7f\u7528\u6784\u9020\u51fd\u6570\u81ea\u52a8\u88c5\u914d\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>@Autowired<\/code>\u6ce8\u89e3\u6216XML\u914d\u7f6e\u8fdb\u884c\u6807\u8bc6\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">public class MyClass {\n    private AnotherClass another;\n\n    @Autowired\n    public MyClass(AnotherClass another) {\n        this.another = another;\n    }\n<\/code><\/pre>\n<pre><code class=\"language-xml\">&lt;bean id=&quot;myClass&quot; class=&quot;com.example.MyClass&quot; autowire=&quot;constructor&quot;&gt;\n    &lt;!-- configurations --&gt;\n&lt;\/bean&gt;\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u65e0\u81ea\u52a8\u88c5\u914d\uff08no\uff09\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528<code>autowire=&quot;no&quot;<\/code>\u8868\u793a\u5173\u95ed\u81ea\u52a8\u88c5\u914d\uff0c\u9700\u8981\u624b\u52a8\u901a\u8fc7<code>&lt;property&gt;<\/code>\u6807\u7b7e\u6216<code>@Autowired<\/code>\u6ce8\u89e3\u8fdb\u884c\u663e\u5f0f\u914d\u7f6e\u4f9d\u8d56\u5173\u7cfb\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-xml\">&lt;bean id=&quot;myBean&quot; class=&quot;com.example.MyBean&quot; autowire=&quot;no&quot;&gt;\n    &lt;property name=&quot;dependency&quot; ref=&quot;anotherBean&quot; \/&gt;\n&lt;\/bean&gt;\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u901a\u8fc7\u6ce8\u89e3\u8fdb\u884c\u81ea\u52a8\u88c5\u914d\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528<code>@Autowired<\/code>\u3001<code>@Resource<\/code>\u7b49\u6ce8\u89e3\uff0c\u901a\u8fc7\u6ce8\u89e3\u7684\u65b9\u5f0f\u5b9e\u73b0\u81ea\u52a8\u88c5\u914d\u3002\u8fd9\u79cd\u65b9\u5f0f\u901a\u5e38\u9700\u8981\u5728\u914d\u7f6e\u7c7b\u4e2d\u542f\u7528\u6ce8\u89e3\u626b\u63cf\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Component\npublic class MyClass {\n    @Autowired\n    private AnotherClass another;\n}\n<\/code><\/pre>\n<pre><code class=\"language-java\">@Configuration\n@ComponentScan(basePackages = &quot;com.example&quot;)\npublic class AppConfig {\n    \/\/ Configuration settings\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u8fd9\u4e9b\u81ea\u52a8\u88c5\u914d\u65b9\u5f0f\u63d0\u4f9b\u4e86\u4e0d\u540c\u7684\u9009\u62e9\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u60c5\u51b5\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u5f0f\u3002\u5728\u5b9e\u8df5\u4e2d\uff0c\u6839\u636e\u9879\u76ee\u7684\u9700\u6c42\u548c\u56e2\u961f\u7684\u7ea6\u5b9a\uff0c\u9009\u62e9\u5408\u9002\u7684\u81ea\u52a8\u88c5\u914d\u65b9\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Spring\u4e2d\uff0c\u81ea\u52a8\u88c5\u914d\u662f\u4e00\u79cd\u901a\u8fc7Spring\u5bb9\u5668\u81ea\u52a8\u5c06Bean\u7684\u4f9d\u8d56\u5173\u7cfb\u5efa\u7acb\u8d77\u6765\u7684\u673a\u5236\u3002Spring\u63d0\u4f9b\u4e86 [&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-12096","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12096","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=12096"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12096\/revisions"}],"predecessor-version":[{"id":47851,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12096\/revisions\/47851"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=12096"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=12096"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=12096"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}