{"id":6735,"date":"2025-04-12T11:11:57","date_gmt":"2025-04-12T03:11:57","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6735"},"modified":"2025-04-12T11:11:57","modified_gmt":"2025-04-12T03:11:57","slug":"springboot-%e7%9a%84-ioc-%e5%92%8c-autowired-%e6%b3%a8%e8%a7%a3%e7%bb%93%e5%90%88","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6735","title":{"rendered":"SpringBoot \u7684 IOC \u548c @Autowired \u6ce8\u89e3\u7ed3\u5408"},"content":{"rendered":"<h3>Spring Boot \u4e2d IOC \u8be6\u89e3<\/h3>\n<h4>1. IOC \u6982\u5ff5<\/h4>\n<p>IOC\uff08Inversion of Control\uff09\u5373\u63a7\u5236\u53cd\u8f6c\uff0c\u662f\u4e00\u79cd\u8bbe\u8ba1\u539f\u5219\uff0c\u5b83\u5c06\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u4f9d\u8d56\u5173\u7cfb\u7684\u7ba1\u7406\u4ece\u5e94\u7528\u7a0b\u5e8f\u4ee3\u7801\u4e2d\u8f6c\u79fb\u5230\u5916\u90e8\u5bb9\u5668\u4e2d\u3002\u5728\u4f20\u7edf\u7f16\u7a0b\u4e2d\uff0c\u5bf9\u8c61\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\u662f\u7531\u5bf9\u8c61\u81ea\u8eab\u8d1f\u8d23\u521b\u5efa\u548c\u7ba1\u7406\u7684\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u4ee3\u7801\u7684\u8026\u5408\u5ea6\u8f83\u9ad8\u3002\u800c\u5728 IOC \u6a21\u5f0f\u4e0b\uff0c\u5bf9\u8c61\u53ea\u9700\u8981\u58f0\u660e\u5b83\u6240\u4f9d\u8d56\u7684\u5bf9\u8c61\uff0c\u7531\u5bb9\u5668\u6765\u8d1f\u8d23\u521b\u5efa\u548c\u6ce8\u5165\u8fd9\u4e9b\u4f9d\u8d56\u5bf9\u8c61\uff0c\u4ece\u800c\u964d\u4f4e\u4e86\u7ec4\u4ef6\u4e4b\u95f4\u7684\u8026\u5408\u5ea6\u3002<\/p>\n<h4>2. Spring Boot \u4e2d\u7684 IOC \u5bb9\u5668<\/h4>\n<p>Spring Boot \u57fa\u4e8e Spring \u6846\u67b6\uff0c\u4f7f\u7528 Spring \u7684 IOC \u5bb9\u5668\u6765\u7ba1\u7406 Bean\u3002Spring \u7684 IOC \u5bb9\u5668\u6709\u4e24\u79cd\u4e3b\u8981\u5b9e\u73b0\uff1a<code>BeanFactory<\/code> \u548c <code>ApplicationContext<\/code>\u3002<code>ApplicationContext<\/code> \u662f <code>BeanFactory<\/code> \u7684\u5b50\u63a5\u53e3\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\uff0c\u5982\u56fd\u9645\u5316\u652f\u6301\u3001\u4e8b\u4ef6\u53d1\u5e03\u7b49\uff0cSpring Boot \u9ed8\u8ba4\u4f7f\u7528 <code>ApplicationContext<\/code> \u4f5c\u4e3a IOC \u5bb9\u5668\u3002<\/p>\n<h4>3. Bean \u7684\u5b9a\u4e49\u548c\u6ce8\u518c<\/h4>\n<p>\u5728 Spring Boot \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9a\u4e49\u548c\u6ce8\u518c Bean\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u6ce8\u89e3<\/strong>\uff1a\u4f8b\u5982 <code>@Component<\/code>\u3001<code>@Service<\/code>\u3001<code>@Repository<\/code>\u3001<code>@Controller<\/code> \u7b49\u6ce8\u89e3\uff0c\u8fd9\u4e9b\u6ce8\u89e3\u672c\u8d28\u4e0a\u90fd\u662f <code>@Component<\/code> \u7684\u6d3e\u751f\u6ce8\u89e3\uff0c\u7528\u4e8e\u5c06\u4e00\u4e2a\u7c7b\u6807\u8bb0\u4e3a Spring \u5bb9\u5668\u4e2d\u7684 Bean\u3002\n<pre><code class=\"language-java\">\nimport org.springframework.stereotype.Service;<\/code><\/pre>\n<\/li>\n<\/ul>\n<p>@Service<br \/>\npublic class UserService {<br \/>\npublic String getUserName() {<br \/>\nreturn &quot;John Doe&quot;;<br \/>\n}<br \/>\n}<\/p>\n<pre><code>- **\u4f7f\u7528 Java \u914d\u7f6e\u7c7b**\uff1a\u901a\u8fc7 <code>@Configuration<\/code> \u548c <code>@Bean<\/code> \u6ce8\u89e3\u6765\u5b9a\u4e49 Bean\u3002\n```java\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\n\n@Configuration\npublic class AppConfig {\n    @Bean\n    public UserService userService() {\n        return new UserService();\n    }\n}<\/code><\/pre>\n<h4>4. IOC \u5bb9\u5668\u7684\u5de5\u4f5c\u6d41\u7a0b<\/h4>\n<ul>\n<li><strong>\u542f\u52a8\u9636\u6bb5<\/strong>\uff1a\u5f53 Spring Boot \u5e94\u7528\u542f\u52a8\u65f6\uff0c\u4f1a\u521b\u5efa <code>ApplicationContext<\/code> \u5b9e\u4f8b\u3002\u5bb9\u5668\u4f1a\u626b\u63cf\u6307\u5b9a\u7684\u5305\u8def\u5f84\uff0c\u67e5\u627e\u5e26\u6709 <code>@Component<\/code> \u53ca\u5176\u6d3e\u751f\u6ce8\u89e3\u7684\u7c7b\uff0c\u4ee5\u53ca <code>@Configuration<\/code> \u6ce8\u89e3\u7684\u914d\u7f6e\u7c7b\u3002<\/li>\n<li><strong>Bean \u5b9a\u4e49\u9636\u6bb5<\/strong>\uff1a\u5bb9\u5668\u4f1a\u89e3\u6790\u8fd9\u4e9b\u7c7b\u548c\u914d\u7f6e\u7c7b\uff0c\u5c06\u5b83\u4eec\u8f6c\u6362\u4e3a Bean \u5b9a\u4e49\uff08<code>BeanDefinition<\/code>\uff09\uff0c\u5e76\u5b58\u50a8\u5728\u5bb9\u5668\u5185\u90e8\u7684\u6ce8\u518c\u8868\u4e2d\u3002<\/li>\n<li><strong>Bean \u521b\u5efa\u9636\u6bb5<\/strong>\uff1a\u6839\u636e Bean \u5b9a\u4e49\uff0c\u5bb9\u5668\u4f1a\u521b\u5efa Bean \u5b9e\u4f8b\u3002\u5bf9\u4e8e\u5355\u4f8b Bean\uff0c\u5bb9\u5668\u4f1a\u5728\u542f\u52a8\u65f6\u5c31\u521b\u5efa\u5b9e\u4f8b\uff1b\u5bf9\u4e8e\u539f\u578b Bean\uff0c\u4f1a\u5728\u6bcf\u6b21\u8bf7\u6c42\u65f6\u521b\u5efa\u65b0\u7684\u5b9e\u4f8b\u3002<\/li>\n<li><strong>\u4f9d\u8d56\u6ce8\u5165\u9636\u6bb5<\/strong>\uff1a\u5bb9\u5668\u4f1a\u6839\u636e Bean \u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u5c06\u4f9d\u8d56\u7684 Bean \u6ce8\u5165\u5230\u9700\u8981\u7684 Bean \u4e2d\u3002<\/li>\n<\/ul>\n<h3><code>@Autowired<\/code> \u6ce8\u89e3\u4e0e IOC \u7684\u7ed3\u5408<\/h3>\n<h4>1. <code>@Autowired<\/code> \u6ce8\u89e3\u7684\u4f5c\u7528<\/h4>\n<p><code>@Autowired<\/code> \u662f Spring \u6846\u67b6\u63d0\u4f9b\u7684\u4e00\u4e2a\u6ce8\u89e3\uff0c\u7528\u4e8e\u81ea\u52a8\u88c5\u914d\u4f9d\u8d56\u7684 Bean\u3002\u5b83\u53ef\u4ee5\u5e94\u7528\u5728\u6784\u9020\u51fd\u6570\u3001\u5b57\u6bb5\u3001\u65b9\u6cd5\u7b49\u4f4d\u7f6e\uff0cSpring \u5bb9\u5668\u4f1a\u6839\u636e\u7c7b\u578b\u81ea\u52a8\u67e5\u627e\u5339\u914d\u7684 Bean \u5e76\u6ce8\u5165\u5230\u76ee\u6807\u5bf9\u8c61\u4e2d\u3002<\/p>\n<h4>2. \u5728\u5b57\u6bb5\u4e0a\u4f7f\u7528 <code>@Autowired<\/code><\/h4>\n<pre><code class=\"language-java\">import org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Component;\n\n@Component\npublic class UserController {\n    @Autowired\n    private UserService userService;\n\n    public String getUserName() {\n        return userService.getUserName();\n    }\n}<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>UserController<\/code> \u7c7b\u4f9d\u8d56\u4e8e <code>UserService<\/code> \u7c7b\uff0c\u901a\u8fc7\u5728 <code>userService<\/code> \u5b57\u6bb5\u4e0a\u4f7f\u7528 <code>@Autowired<\/code> \u6ce8\u89e3\uff0cSpring \u5bb9\u5668\u4f1a\u81ea\u52a8\u67e5\u627e\u7c7b\u578b\u4e3a <code>UserService<\/code> \u7684 Bean \u5e76\u6ce8\u5165\u5230 <code>UserController<\/code> \u4e2d\u3002<\/p>\n<h4>3. \u5728\u6784\u9020\u51fd\u6570\u4e0a\u4f7f\u7528 <code>@Autowired<\/code><\/h4>\n<pre><code class=\"language-java\">import org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Component;\n\n@Component\npublic class UserController {\n    private final UserService userService;\n\n    @Autowired\n    public UserController(UserService userService) {\n        this.userService = userService;\n    }\n\n    public String getUserName() {\n        return userService.getUserName();\n    }\n}<\/code><\/pre>\n<p>\u4ece Spring 4.3 \u7248\u672c\u5f00\u59cb\uff0c\u5982\u679c\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u6784\u9020\u51fd\u6570\uff0c<code>@Autowired<\/code> \u6ce8\u89e3\u53ef\u4ee5\u7701\u7565\u3002\u4f7f\u7528\u6784\u9020\u51fd\u6570\u6ce8\u5165\u7684\u597d\u5904\u662f\u53ef\u4ee5\u4fdd\u8bc1\u4f9d\u8d56\u7684 Bean \u5728\u5bf9\u8c61\u521b\u5efa\u65f6\u5c31\u5df2\u7ecf\u88ab\u6ce8\u5165\uff0c\u907f\u514d\u4e86\u7a7a\u6307\u9488\u5f02\u5e38\u3002<\/p>\n<h4>4. \u5904\u7406\u591a\u4e2a\u5339\u914d\u7684 Bean<\/h4>\n<p>\u5f53\u5b58\u5728\u591a\u4e2a\u7c7b\u578b\u5339\u914d\u7684 Bean \u65f6\uff0c<code>@Autowired<\/code> \u4f1a\u6839\u636e Bean \u7684\u540d\u79f0\u8fdb\u884c\u5339\u914d\u3002\u53ef\u4ee5\u4f7f\u7528 <code>@Qualifier<\/code> \u6ce8\u89e3\u6765\u6307\u5b9a\u5177\u4f53\u8981\u6ce8\u5165\u7684 Bean \u7684\u540d\u79f0\u3002<\/p>\n<pre><code class=\"language-java\">import org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.beans.factory.annotation.Qualifier;\nimport org.springframework.stereotype.Component;\n\n@Component\npublic class UserController {\n    @Autowired\n    @Qualifier(&quot;userServiceV2&quot;)\n    private UserService userService;\n\n    public String getUserName() {\n        return userService.getUserName();\n    }\n}<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<p>Spring Boot \u7684 IOC \u5bb9\u5668\u8d1f\u8d23\u7ba1\u7406 Bean \u7684\u521b\u5efa\u548c\u4f9d\u8d56\u5173\u7cfb\uff0c\u800c <code>@Autowired<\/code> \u6ce8\u89e3\u662f\u5b9e\u73b0\u4f9d\u8d56\u6ce8\u5165\u7684\u4e00\u79cd\u4fbf\u6377\u65b9\u5f0f\uff0c\u5b83\u4e0e IOC \u5bb9\u5668\u7d27\u5bc6\u7ed3\u5408\uff0c\u4f7f\u5f97\u5f00\u53d1\u8005\u53ef\u4ee5\u66f4\u52a0\u65b9\u4fbf\u5730\u4f7f\u7528 Spring Boot \u7684\u4f9d\u8d56\u6ce8\u5165\u529f\u80fd\uff0c\u964d\u4f4e\u4ee3\u7801\u7684\u8026\u5408\u5ea6\u3002 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring Boot \u4e2d IOC \u8be6\u89e3 1. IOC \u6982\u5ff5 IOC\uff08Inversion of Control&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6735\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[123],"tags":[],"class_list":["post-6735","post","type-post","status-publish","format-standard","hentry","category-123"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6735","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6735"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6735\/revisions"}],"predecessor-version":[{"id":6736,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6735\/revisions\/6736"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}