{"id":94591,"date":"2020-05-09T16:25:02","date_gmt":"2020-05-09T08:25:02","guid":{"rendered":"http:\/\/4563.org\/?p=94591"},"modified":"2020-05-09T16:25:02","modified_gmt":"2020-05-09T08:25:02","slug":"%e5%ae%9e%e7%8e%b0%e4%b8%80%e4%b8%aa%e7%ae%80%e6%98%93%e7%9a%84-spring-ioc-%e5%ae%b9%e5%99%a8","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=94591","title":{"rendered":"\u5b9e\u73b0\u4e00\u4e2a\u7b80\u6613\u7684 Spring IoC \u5bb9\u5668"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5b9e\u73b0\u4e00\u4e2a\u7b80\u6613\u7684 Spring IoC \u5bb9\u5668               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : deppwxq <\/span>  <span><i><\/i> 26<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u4f5c\u8005\uff1aDeppWang\u3001\u539f\u6587\u5730\u5740<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/05\/20200519_5ec469ef4adc5.jpg\" alt=\"\u5b9e\u73b0\u4e00\u4e2a\u7b80\u6613\u7684 Spring IoC \u5bb9\u5668\" \/><\/p>\n<p>\u6211\u901a\u8fc7\u5b9e\u73b0\u4e00\u4e2a\u7b80\u6613\u7684 Spring IoC \u5bb9\u5668\uff0c\u7b97\u662f\u5165\u95e8\u4e86 Spring \u6846\u67b6\u3002\u672c\u6587\u662f\u5bf9\u5b9e\u73b0\u8fc7\u7a0b\u7684\u4e00\u4e2a\u603b\u7ed3\u63d0\u70bc\uff0c<strong>\u9700\u8981\u914d\u5408\u6e90\u7801\u9605\u8bfb<\/strong>\uff0c\u6e90\u7801\u5730\u5740\u3002<\/p>\n<p>\u7ed3\u5408\u672c\u6587\u548c\u6e90\u7801\uff0c\u4f60\u5e94\u8be5\u53ef\u4ee5\u5b66\u5230\uff1aSpring \u7684\u539f\u7406\u548c Spring Boot \u7684\u539f\u7406\u3002<\/p>\n<p>Spring \u6846\u67b6\u662f Java \u5f00\u53d1\u7684\uff0cJava \u662f\u9762\u5411\u5bf9\u8c61\u7684\u8bed\u8a00\uff0c\u6240\u4ee5 Spring \u6846\u67b6\u672c\u8eab\u6709\u5927\u91cf\u7684\u62bd\u8c61\u3001\u7ee7\u627f\u3001\u591a\u6001\u3002\u5bf9\u4e8e\u521d\u5b66\u8005\u6765\u8bf4\uff0c\u5149\u662f\u7406\u6e05\u4ed6\u4eec\u7684\u903b\u8f91\u5c31\u5f88\u9ebb\u70e6\uff0c\u6211\u6452\u5f03\u4e86\u90a3\u4e9b\u5305\u88c5\uff0c\u53ea\u5b9e\u73b0\u4e86\u6700\u672c\u8d28\u7684\u529f\u80fd\u3002\u4ee3\u7801\u4e0d\u662f\u5f88\u4e25\u8c28\uff0c\u4f46\u53ea\u4e3a\u4e86\u7406\u89e3 Spring \u601d\u60f3\u5374\u591f\u4e86\u3002<\/p>\n<p>\u4e0b\u9762\u6b63\u6587\u5f00\u59cb\u3002<\/p>\n<h2>\u96f6\u3001\u524d\u8a00<\/h2>\n<p>\u5728\u6ca1\u6709 Spring \u6846\u67b6\u7684\u8fdc\u53e4\u65f6\u4ee3\uff0c\u6211\u4eec\u4e1a\u52a1\u903b\u8f91\u957f\u8fd9\u6837\uff1a<\/p>\n<pre><code>public class PetStoreService {     AccountDao accountDao = new AccountDao(); }  public class AccountDao { }  PetStoreService petStoreService = new PetStoreService(); <\/code><\/pre>\n<p>\u5230\u5904\u90fd\u662f <strong>new<\/strong> \u5173\u952e\u5b57\uff0c\u9700\u8981\u5f00\u53d1\u4eba\u5458\u663e\u5f0f\u7684\u4f7f\u7528 new \u5173\u952e\u5b57\u6765\u521b\u5efa<strong>\u4e1a\u52a1\u7c7b<\/strong>\u5bf9\u8c61\uff08\u5b9e\u4f8b\uff09\u3002\u8fd9\u6837\u6709\u5f88\u591a\u5f0a\u7aef\uff0c\u5982\uff0c\u521b\u5efa\u7684\u5bf9\u8c61\u592a\u591a\uff0c\u8026\u5408\u6027\u592a\u5f3a\uff0c\u7b49\u7b49\u3002<\/p>\n<p>\u6709\u4e2a\u53eb Rod Johnson \u8001\u54e5\u5bf9\u6b64\u5f88\u4e0d\u723d\uff0c\u5c31\u5f00\u53d1\u4e86\u4e00\u4e2a\u53eb Spring \u7684\u6846\u67b6\uff0c\u5c31\u662f\u4e3a\u4e86\u5e72\u6389 new \u5173\u952e\u5b57\uff08\u54c8\u54c8\uff0c\u6211\u675c\u64b0\u7684\uff0c\u53ea\u662f\u4e3a\u4e86\u8bf4\u660e Spring \u7684\u4f5c\u7528\uff09\u3002<\/p>\n<p>\u6709\u4e86 Spring \u6846\u67b6\uff0c<strong>\u7531\u6846\u67b6\u6765\u65b0\u5efa\u5bf9\u8c61\uff0c\u7ba1\u7406\u5bf9\u8c61\uff0c\u5e76\u5904\u7406\u5bf9\u8c61\u4e4b\u95f4\u7684\u4f9d\u8d56<\/strong>\uff0c\u6211\u4eec\u7a0b\u5e8f\u5458\u518d\u4e5f\u4e0d\u7528 new \u4e1a\u52a1\u7c7b\u5bf9\u8c61\u4e86\u3002\u6211\u4eec\u6765\u770b\u770b Spring \u6846\u67b6\u662f\u5982\u4f55\u5b9e\u73b0\u7684\u5427\u3002<\/p>\n<p>\u6ce8\uff1a\u4ee5\u4e0b Spring \u6846\u67b6\u7b80\u5199\u4e3a Spring<\/p>\n<p>\u672c\u8282\u6e90\u7801\u5bf9\u5e94\uff1a<strong>v0<\/strong><\/p>\n<h2>\u4e00\u3001\u5b9e\u73b0\u300c\u5b9e\u4f8b\u5316 Bean \u300d<\/h2>\n<p>\u9996\u5148\uff0cSpring \u9700\u8981\u5b9e\u4f8b\u5316\u7c7b\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a\u5bf9\u8c61\u3002\u5728 Spring \u4e2d\uff0c\u6211\u4eec\u7ba1\uff08\u4e1a\u52a1\uff09\u7c7b\u53eb Bean\uff0c\u6240\u4ee5\u5b9e\u4f8b\u5316\u7c7b\u4e5f\u53ef\u4ee5\u79f0\u4e3a\u5b9e\u4f8b\u5316 Bean \u3002<\/p>\n<p>\u65e9\u671f Spring \u9700\u8981\u501f\u52a9 xml \u914d\u7f6e\u6587\u4ef6\u6765\u5b9e\u73b0\u5b9e\u4f8b\u5316 Bean\uff0c\u53ef\u4ee5\u5206\u4e3a\u4e09\u6b65\uff08\u914d\u5408\u6e90\u7801 <strong>v1<\/strong> \u9605\u8bfb\uff09\uff1a<\/p>\n<ol>\n<li>\u4ece xml \u914d\u7f6e\u6587\u4ef6\u83b7\u53d6 Bean \u4fe1\u606f\uff0c\u5982\u5168\u9650\u5b9a\u540d\u7b49\uff0c\u5c06\u5176\u4f5c\u4e3a BeanDefinition \uff08 Bean \u5b9a\u4e49\u7c7b\uff09\u7684\u5c5e\u6027<\/li>\n<li>\u4f7f\u7528\u4e00\u4e2a Map \u5b58\u653e\u6240\u6709 BeanDefinition\uff0c\u6b64\u65f6 Spring \u672c\u8d28\u4e0a\u662f\u4e00\u4e2a Map\uff0c\u5b58\u653e BeanDefinition<\/li>\n<li>\u5f53\u83b7\u53d6 Bean \u5b9e\u4f8b\u65f6\uff0c\u901a\u8fc7\u7c7b\u52a0\u8f7d\u5668\uff0c\u6839\u636e\u5168\u9650\u5b9a\u540d\uff0c\u5f97\u5230\u5176\u7c7b\u5bf9\u8c61\uff0c\u901a\u8fc7\u7c7b\u5bf9\u8c61\u5229\u7528<strong>\u53cd\u5c04<\/strong>\u521b\u5efa Bean \u5b9e\u4f8b<\/li>\n<\/ol>\n<p>\u5173\u4e8e\u7c7b\u52a0\u8f7d\u548c\u53cd\u5c04\uff0c\u524d\u8005\u53ef\u4ee5\u770b\u770b\u300a\u6df1\u5165\u7406\u89e3 Java \u865a\u62df\u673a\u300b\u7b2c 7 \u7ae0\uff0c\u540e\u8005\u53ef\u4ee5\u770b\u770b\u300a\u5ed6\u96ea\u5cf0 Java \u6559\u7a0b\u300b\u53cd\u5c04 \u90e8\u5206\u3002\u672c\u6587\u53ea\u5b66\u4e60 Spring\uff0c\u8fd9\u4e24\u4e2a\u77e5\u8bc6\u70b9\u4e0d\u505a\u6df1\u5165\u8ba8\u8bba\u3002<\/p>\n<p>\u540d\u8bcd\u89e3\u91ca\uff1a<\/p>\n<ul>\n<li>\u5168\u9650\u5b9a\u540d\uff1a\u6307\u7f16\u8bd1\u540e\u7684 class \u6587\u4ef6\u5728 jar \u5305\u4e2d\u7684\u8def\u5f84<\/li>\n<\/ul>\n<p>\u672c\u8282\u6e90\u7801\u5bf9\u5e94\uff1a<strong>v1<\/strong><\/p>\n<h2>\u4e8c\u3001\u5b9e\u73b0\u300c\u586b\u5145\u5c5e\u6027\uff08\u4f9d\u8d56\u6ce8\u5165\uff09\u300d<\/h2>\n<p>\u5b9e\u73b0\u5b9e\u4f8b\u5316 Bean \u540e\uff0c\u6b64\u65f6\u6210\u5458\u53d8\u91cf\uff08\u5f15\u7528\uff09\u8fd8\u4e3a null\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/05\/20200519_5ec469f035e0a.png\" alt=\"\u5b9e\u73b0\u4e00\u4e2a\u7b80\u6613\u7684 Spring IoC \u5bb9\u5668\" \/><\/p>\n<p>\u6b64\u65f6\u9700\u8981\u901a\u8fc7\u4e00\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u8ba9\u5f15\u7528\u6307\u5411\u5b9e\u4f8b\uff0c\u6211\u4eec\u7ba1\u8fd9\u4e00\u6b65\u53eb\u586b\u5145\u5c5e\u6027\u3002<\/p>\n<p>\u5f53\u4e00\u4e2a Bean \u7684\u6210\u5458\u53d8\u91cf\u7c7b\u578b\u662f\u53e6\u4e00\u4e2a Bean \u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u8bf4\u4e00\u4e2a Bean \u4f9d\u8d56\u4e8e\u53e6\u4e00\u4e2a Bean \u3002\u6240\u4ee5\u586b\u5145\u5c5e\u6027\uff0c\u4e5f\u53ef\u4ee5\u79f0\u4e3a\u4f9d\u8d56\u6ce8\u5165\uff08<strong>D<\/strong>ependency <strong>I<\/strong>njection\uff0c\u7b80\u79f0 DI \uff09\u3002<\/p>\n<p>\u629b\u5f00 Spring \u4e0d\u8c08\uff0c\u5728\u6b63\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u6709\u4e24\u79cd\u65b9\u5f0f\u5b9e\u73b0\u4f9d\u8d56\u6ce8\u5165\uff0c1 \u3001\u4f7f\u7528 Setter() \u65b9\u6cd5\uff0c2 \u3001\u4f7f\u7528\u6784\u9020\u65b9\u6cd5\u3002\u4f7f\u7528 Setter() \u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>public class PetStoreService {     private AccountDao accountDao;     public void setAccountDao(AccountDao accountDao) {         this.accountDao = accountDao;     } }  public class AccountDao { }  PetStoreService petStore = new PetStoreService(); petStore.setAccountDao(new AccountDao()); \/\/ \u5c06\u4f9d\u8d56 new AccountDao() \u6ce8\u5165 petStore <\/code><\/pre>\n<p>\u5176\u5b9e\u65e9\u671f Spring \u4e5f\u662f\u901a\u8fc7\u8fd9\u4e24\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u4f9d\u8d56\u6ce8\u5165\u7684\u3002\u4e0b\u9762\u662f Spring \u901a\u8fc7 xml \u6587\u4ef6 + Setter() \u6765\u5b9e\u73b0\u4f9d\u8d56\u6ce8\u5165\u7684\u6b65\u9aa4\uff08\u914d\u5408\u6e90\u7801 <strong>v2<\/strong> \u9605\u8bfb\uff09\uff1a<\/p>\n<ol>\n<li>\u7ed9 PetStoreService \u6dfb\u52a0 Setter() \u65b9\u6cd5\uff0c\u5e76\u7a0d\u5fae\u4fee\u6539\u4e00\u4e0b xml \u914d\u7f6e\u6587\u4ef6\uff0c\u6dfb\u52a0 <code>&lt;property&gt;<\/code>\uff0c\u4ee3\u8868\u5bf9\u5e94 Setter() \u65b9\u6cd5\u3002<\/li>\n<li>\u4ece xml \u914d\u7f6e\u6587\u4ef6\u83b7\u53d6 Bean \u7684\u5c5e\u6027 <code>&lt;property&gt;<\/code>\uff0c\u5b58\u653e\u5230 BeanDefinition \u7684 propertyNames \u4e2d\u3002<\/li>\n<li>\u901a\u8fc7 propertyName \u83b7\u53d6\u5c5e\u6027\u5b9e\u4f8b\uff0c\u5229\u7528\u53cd\u5c04\uff0c\u901a\u8fc7 Setter() \u65b9\u6cd5\u5b9e\u73b0\u586b\u5145\u5c5e\u6027\uff08\u4f9d\u8d56\u6ce8\u5165\uff09<\/li>\n<\/ol>\n<p>\u57fa\u4e8e\u6784\u9020\u51fd\u6570\u5b9e\u73b0\u4f9d\u8d56\u6ce8\u5165\u7684\u65b9\u5f0f\u8ddf Setter() \u65b9\u6cd5\u5dee\u4e0d\u591a\uff0c\u611f\u5174\u8da3\u53ef\u4ee5 Google \u641c\u7d22\u67e5\u770b\u3002<\/p>\n<p>\u56e0\u4e3a Spring \u5b9e\u73b0\u4e86\u4f9d\u8d56\u6ce8\u5165\uff0c\u6240\u4ee5\u6211\u4eec\u7a0b\u5e8f\u5458\u6ca1\u6709\u4e86\u521b\u5efa\u5bf9\u8c61\u7684\u63a7\u5236\u6743\uff0c\u6240\u4ee5\u4e5f\u88ab\u79f0\u4e3a\u63a7\u5236\u53cd\u8f6c\uff08<strong>I<\/strong>nversion <strong>o<\/strong>f <strong>C<\/strong>ontrol\uff0c\u7b80\u79f0 IoC \uff09\u3002\u56e0\u4e3a Spring \u4f7f\u7528 Map \u7ba1\u7406 BeanDefinition\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5c06 Spring \u79f0\u4e3a IoC \u5bb9\u5668\u3002<\/p>\n<p>\u672c\u8282\u6e90\u7801\u5bf9\u5e94\uff1a<strong>v2<\/strong><\/p>\n<h2>\u4e09\u3001\u4f7f\u7528\u300c\u5355\u4f8b\u6a21\u5f0f\u3001\u5de5\u5382\u65b9\u6cd5\u6a21\u5f0f\u300d<\/h2>\n<p>\u524d\u9762\u4e24\u6b65\u5b9e\u73b0\u4e86\u83b7\u53d6 Bean \u5b9e\u4f8b\u65f6\u521b\u5efa Bean \u5b9e\u4f8b\uff0c\u4f46 Bean \u5b9e\u4f8b\u7ecf\u5e38\u4f7f\u7528\uff0c\u4e0d\u80fd\u6bcf\u6b21\u90fd\u65b0\u521b\u5efa\u3002\u5176\u5b9e\u5728 Spring \u4e2d\uff0c\u4e00\u4e2a\u4e1a\u52a1\u7c7b\u53ea\u5bf9\u5e94\u4e00\u4e2a Bean \u5b9e\u4f8b\uff0c\u8fd9\u9700\u8981\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u3002<\/p>\n<blockquote>\n<p>\u5355\u4f8b\u6a21\u5f0f\uff1a\u4e00\u4e2a\u7c7b\u6709\u4e14\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b<\/p>\n<\/blockquote>\n<p>Spring \u4f7f\u7528\u7c7b\u5bf9\u8c61\u521b\u5efa Bean \u5b9e\u4f8b\uff0c\u662f\u5982\u4f55\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f\u7684\uff1f<\/p>\n<p>Spring \u5176\u5b9e\u4f7f\u7528\u4e00\u4e2a Map \u5b58\u653e\u6240\u6709 Bean \u5b9e\u4f8b\u3002\u521b\u5efa\u65f6\uff0c\u5148\u770b Map \u4e2d\u662f\u5426\u6709 Bean \u5b9e\u4f8b\uff0c\u6ca1\u6709\u5c31\u521b\u5efa\uff1b\u83b7\u53d6\u65f6\uff0c\u76f4\u63a5\u4ece Map \u4e2d\u83b7\u53d6\u3002\u8fd9\u79cd\u65b9\u5f0f\u80fd\u4fdd\u8bc1\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a Bean \u5b9e\u4f8b\u3002<\/p>\n<pre><code>private final Map&lt;String, Object&gt; singletonObjects = new ConcurrentHashMap&lt;&gt;(64); <\/code><\/pre>\n<p>\u65e9\u671f Spring \u4f7f\u7528 Bean \u7684\u7b56\u7565\u662f\u7528\u5230\u65f6\u518d\u5b9e\u4f8b\u5316\u6240\u7528 Bean\uff0c\u6770\u51fa\u4ee3\u8868\u662f XmlBeanFactory\uff0c\u540e\u671f\u4e3a\u4e86\u5b9e\u73b0\u66f4\u591a\u7684\u529f\u80fd\uff0c\u65b0\u589e\u4e86 ApplicationContext\uff0c\u4e24\u8005\u90fd\u7ee7\u627f\u4e8e BeanFactory \u63a5\u53e3\u3002\u8fd9\u4f7f\u7528\u4e86\u5de5\u5382\u65b9\u6cd5\u6a21\u5f0f\u3002<\/p>\n<blockquote>\n<p>\u5de5\u5382\u65b9\u6cd5\u6a21\u5f0f\uff1a\u5b9a\u4e49\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u5bf9\u8c61\u7684\u63a5\u53e3\uff0c\u8ba9\u5b50\u7c7b\u51b3\u5b9a\u5b9e\u4f8b\u5316\u54ea\u4e00\u4e2a\u7c7b\u3002Factory Method \u4f7f\u4e00\u4e2a\u7c7b\u7684\u5b9e\u4f8b\u5316\u5ef6\u8fdf\u5230\u5176\u5b50\u7c7b\u3002<\/p>\n<\/blockquote>\n<p>\u6211\u4eec\u5c06 BeanIocContainer \u4fee\u6539\u4e3a BeanFactory \u63a5\u53e3\uff0c\u53ea\u63d0\u4f9b getBean() \u65b9\u6cd5\u3002\u521b\u5efa\uff08 IoC \uff09\u5bb9\u5668\u7531\u5176\u5b50\u7c7b\u81ea\u5df1\u5b9e\u73b0\u3002<\/p>\n<p>ApplicationContext \u548c BeanFactory \u7684\u533a\u522b\uff1aApplicationContext \u521d\u59cb\u5316\u65f6\u5c31\u5b9e\u4f8b\u5316\u6240\u6709 Bean\uff0cBeanFactory \u7528\u5230\u65f6\u518d\u5b9e\u4f8b\u5316\u6240\u7528 Bean \u3002<\/p>\n<p>\u672c\u8282\u6e90\u7801\u5bf9\u5e94\uff1a<strong>v3<\/strong><\/p>\n<h2>\u4e09\u3001\u5b9e\u73b0\u300c\u6ce8\u89e3\u300d<\/h2>\n<p>\u524d\u9762\u4f7f\u7528 xml \u914d\u7f6e\u6587\u4ef6\u7684\u65b9\u5f0f\uff0c\u5b9e\u73b0\u4e86\u5b9e\u4f8b\u5316 Bean \u548c\u4f9d\u8d56\u6ce8\u5165\u3002\u8fd9\u79cd\u65b9\u5f0f\u6bd4\u8f83\u9ebb\u70e6\uff0c\u8fd8\u5bb9\u6613\u51fa\u9519\u3002Spring \u4ece 2.5&lt;sup&gt;ref&lt;\/sup&gt; \u5f00\u59cb\u53ef\u4f7f\u7528\u6ce8\u89e3\u66ff\u4ee3 xml \u914d\u7f6e\u6587\u4ef6\u3002\u6bd4\u5982\uff1a<\/p>\n<ol>\n<li>\u4f7f\u7528 @Component \u6ce8\u89e3\u4ee3\u66ff <code>&lt;bean&gt;<\/code><\/li>\n<li>\u4f7f\u7528 @Autowired \u6ce8\u89e3\u4ee3\u66ff <code>&lt;property&gt;<\/code><\/li>\n<\/ol>\n<p>@Component \u7528\u4e8e\u751f\u6210 BeanDefinition\uff0c\u539f\u7406\uff08\u914d\u5408\u6e90\u7801 <strong>v4<\/strong> \u9605\u8bfb\uff09\uff1a<\/p>\n<ul>\n<li>\u6839\u636e component-scan \u6307\u5b9a\u8def\u5f84\uff0c\u627e\u5230\u8def\u5f84\u4e0b\u6240\u6709\u5305\u542b @Component \u6ce8\u89e3\u7684 Class \u6587\u4ef6\uff0c\u4f5c\u4e3a BeanDefinition<\/li>\n<li>\u5982\u4f55\u5224\u65ad Class \u662f\u5426\u6709 @Component\uff1a\u5229\u7528\u5b57\u8282\u7801\u6280\u672f\uff0c\u83b7\u53d6 Class \u6587\u4ef6\u4e2d\u7684\u5143\u6570\u636e\uff08\u6ce8\u89e3\u7b49\uff09\uff0c\u5224\u65ad\u5143\u6570\u636e\u4e2d\u662f\u5426\u6709 @Componet<\/li>\n<\/ul>\n<p>@Autowired \u7528\u4e8e\u4f9d\u8d56\u6ce8\u5165\uff0c\u539f\u7406\uff08\u914d\u5408\u6e90\u7801 <strong>v4<\/strong> \u9605\u8bfb\uff09\uff1a<\/p>\n<ul>\n<li>\u901a\u8fc7\u53cd\u5c04\uff0c\u67e5\u770b Field \u4e2d\u662f\u5426\u6709 @Autowired \u7c7b\u578b\u7684\u6ce8\u89e3\uff0c\u6709\uff0c\u5219\u4f7f\u7528\u53cd\u5c04\u5b9e\u73b0\u4f9d\u8d56\u6ce8\u5165<\/li>\n<\/ul>\n<p>\u81f3\u6b64\uff0c\u6211\u4eec\u8fd8\u662f\u5728\u9700\u8981\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u6765\u5b9e\u73b0\u7ec4\u4ef6\u626b\u63cf\u3002\u6709\u6ca1\u6709\u5b8c\u5168\u4e0d\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u7684\u65b9\u5f0f\uff1f\u6709\uff01<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 @Configuration \u66ff\u4ee3\u914d\u7f6e\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528 @ComponentScan \u6765\u66ff\u4ee3\u914d\u7f6e\u6587\u4ef6\u7684 <code>&lt;context:component-scan&gt;<\/code> \u3002<\/p>\n<pre><code>@Configuration \/\/ \u5c06\u7c7b\u6807\u8bb0\u4e3a @Configuration\uff0c\u4ee3\u8868\u8fd9\u4e2a\u7c7b\u662f\u76f8\u5f53\u4e8e\u4e00\u4e2a\u914d\u7f6e\u6587\u4ef6 @ComponentScan \/\/ ComponentScan \u626b\u63cf PetStoreConfig.class \u6240\u5728\u8def\u5f84\u53ca\u5176\u6240\u5728\u8def\u5f84\u6240\u6709\u5b50\u8def\u5f84\u7684\u6587\u4ef6 public class PetStoreConfig {     public static void main(String[] args) {         ApplicationContext context = new AnnotationConfigApplicationContext(PetStoreConfig.class);         PetStoreService userService = context.getBean(PetStoreService.class);         userService.getAccountDao();     } } <\/code><\/pre>\n<p>\u4f7f\u7528\u6ce8\u89e3\u5176\u5b9e\u8ddf\u4f7f\u7528 xml \u914d\u7f6e\u6587\u4ef6\u4e00\u6837\uff0c\u76ee\u7684\u662f\u5c06\u914d\u7f6e\u7c7b\u4f5c\u4e3a\u5165\u53e3\uff0c\u5b9e\u73b0\u626b\u63cf\u7ec4\u4ef6\uff0c\u5c06\u5176\u52a0\u8f7d\u8fdb IoC \u5bb9\u5668\u4e2d\u7684\u529f\u80fd\u3002<\/p>\n<p>AnnotationConfigApplicationContext \u662f\u4e13\u4e3a\u9488\u5bf9\u914d\u7f6e\u7c7b\u7684\u542f\u52a8\u7c7b\u3002\u5176\u5b9e\u73b0\u673a\u5236\uff0c\u53ef\u4ee5 Google \u67e5\u9605\u3002<\/p>\n<p>\u540d\u8bcd\u89e3\u91ca\uff1a<\/p>\n<ul>\n<li>Component\uff1a\u7ec4\u4ef6<\/li>\n<li>Autowired\uff1a\u81ea\u52a8\u88c5\u914d<\/li>\n<\/ul>\n<p>\u672c\u8282\u6e90\u7801\u5bf9\u5e94\uff1a<strong>v4<\/strong><\/p>\n<h2>\u56db\u3001Spring Boot \u539f\u7406<\/h2>\n<p>\u8bf4\u5230\u4e86 @Configuration \u548c @ComponentScan\uff0c\u5c31\u4e0d\u5f97\u4e0d\u63d0 Spring Boot \u3002\u56e0\u4e3a Spring Boot \u5c31\u4f7f\u7528\u4e86 @Configuration \u548c @ComponentScan\uff0c\u4f60\u53ef\u4ee5\u70b9\u5f00 @SpringBootApplication \u770b\u5230\u3002<\/p>\n<p>\u6211\u4eec\u53d1\u73b0\uff0cSpring Boot \u542f\u52a8\u65f6\uff0c\u5e76\u6ca1\u6709\u4f7f\u7528 AnnotationConfigApplicationContext \u6765\u6307\u5b9a\u542f\u52a8\u67d0\u67d0 Config \u7c7b\u3002\u8fd9\u662f\u56e0\u4e3a\u5b83\u4f7f\u7528\u4e86 @EnableAutoConfiguration \u6ce8\u89e3\u3002<\/p>\n<p>Spring Boot \u5229\u7528\u4e86 @EnableAutoConfiguration \u6765\u81ea\u52a8\u52a0\u8f7d\u6807\u8bc6\u4e3a @Configuration \u7684\u914d\u7f6e\u7c7b\u5230\u5bb9\u5668\u4e2d\u3002Spring Boot \u8fd8\u53ef\u4ee5\u5c06\u9700\u8981\u81ea\u52a8\u52a0\u8f7d\u7684\u914d\u7f6e\u7c7b\u653e\u5728 spring.factories \u4e2d\uff0cSpring Boot \u5c06\u81ea\u52a8\u52a0\u8f7d spring.factories \u4e2d\u7684\u914d\u7f6e\u7c7b\u3002spring.factories \u9700\u653e\u7f6e\u4e8e META-INF \u4e0b\u3002<\/p>\n<p>\u5982 Spring Boot \u9879\u76ee\u542f\u52a8\u65f6\uff0cautocofigure \u5305\u4e2d\u5c06\u81ea\u52a8\u52a0\u8f7d\u5230\u5bb9\u5668\u7684\uff08\u90e8\u5206\uff09\u914d\u7f6e\u7c7b\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/05\/20200519_5ec469f17a3c9.png\" alt=\"\u5b9e\u73b0\u4e00\u4e2a\u7b80\u6613\u7684 Spring IoC \u5bb9\u5668\" \/><\/p>\n<p>\u4ee5\u4e0a\u4e5f\u662f Spring Boot \u7684\u539f\u7406\u3002<\/p>\n<p>\u5728 Spring Boot \u4e2d\uff0c\u6211\u4eec\u5f15\u5165\u7684 jar \u5305\u90fd\u6709\u4e00\u4e2a\u5b57\u6bb5\uff0c<strong>starter<\/strong>\uff0c\u6211\u4eec\u53eb starter \u5305\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/05\/20200519_5ec469f2e0ce9.png\" alt=\"\u5b9e\u73b0\u4e00\u4e2a\u7b80\u6613\u7684 Spring IoC \u5bb9\u5668\" \/><\/p>\n<p>\u6807\u8bc6\u4e3a starter \uff08\u542f\u52a8\u5668\uff09\u662f\u56e0\u4e3a\u5f15\u5165\u8fd9\u4e9b\u5305\u65f6\uff0c\u6211\u4eec\u4e0d\u7528\u8bbe\u7f6e\u989d\u5916\u64cd\u4f5c\uff0c\u5b83\u80fd\u88ab\u81ea\u52a8\u88c5\u914d\uff0cstarter \u5305\u4e00\u822c\u90fd\u5305\u542b\u81ea\u5df1\u7684 spring.factories \u3002\u5982 spring-cloud-starter-eureka-server\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/05\/20200519_5ec469f42eebd.png\" alt=\"\u5b9e\u73b0\u4e00\u4e2a\u7b80\u6613\u7684 Spring IoC \u5bb9\u5668\" \/><\/p>\n<p>\u5982 druid-spring-boot-starter\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/05\/20200519_5ec469f56a389.png\" alt=\"\u5b9e\u73b0\u4e00\u4e2a\u7b80\u6613\u7684 Spring IoC \u5bb9\u5668\" \/><\/p>\n<p>\u6709\u65f6\u5019\u6211\u4eec\u8fd8\u9700\u8981\u81ea\u5b9a\u4e49 starter \u5305\uff0c\u6bd4\u5982\u5728 Spring Cloud \u4e2d\uff0c\u5f53\u67d0\u4e2a\u5e94\u7528\u8981\u8c03\u7528\u53e6\u4e00\u4e2a\u5e94\u7528\u7684\u4ee3\u7801\u65f6\uff0c\u8981\u4e48\u8c03\u7528\u65b9\u4f7f\u7528 Feign \uff08 HTTP \uff09\uff0c\u8981\u4e48\u5c06\u88ab\u8c03\u7528\u65b9\u81ea\u5b9a\u4e49\u4e3a starter \u5305\uff0c\u8ba9\u8c03\u7528\u65b9\u4f9d\u8d56\u5f15\u7528\uff0c\u518d @Autowired \u4f7f\u7528\u3002\u6b64\u65f6\u9700\u8981\u5728\u88ab\u8c03\u7528\u65b9\u8bbe\u7f6e\u914d\u7f6e\u7c7b\u548c spring.factories\uff1a<\/p>\n<pre><code>@Configuration @ComponentScan public class ProviderAppConfiguration { }  \/\/ spring.factories # Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration= com.amy.cloud.amycloudact.ProviderAppConfiguration <\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/05\/20200519_5ec469f64d4b8.png\" alt=\"\u5b9e\u73b0\u4e00\u4e2a\u7b80\u6613\u7684 Spring IoC \u5bb9\u5668\" \/><\/p>\n<p>\u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u628a\u8fd9\u4e24\u4e2a\u6587\u4ef6\u653e\u5728\u8c03\u7528\u65b9\uff08\u6b64\u65f6\u8981\u6307\u5b9a\u626b\u63cf\u8def\u5f84\uff09\uff0c\u4f46\u4e00\u822c\u653e\u5728\u88ab\u8c03\u7528\u65b9\u3002ps\uff1a\u5982\u679c\u4f60\u4e24\u4e2a\u5e94\u7528\u7684 base-package \u8def\u5f84\u4e00\u6837\uff0c\u90a3\u4e48\u53ef\u4ee5\u4e0d\u7528\u8fd9\u4e00\u6b65\u3002<\/p>\n<p>\u8bf4\u4e86 Spring Boot\uff0c\u90a3\u4e48\u5728 Spring MVC\uff0c\u5982\u4f55\u5c06\u5f15\u5165 jar \u5305\u7684\u7ec4\u4ef6\u6ce8\u5165\u5bb9\u5668\uff1f<\/p>\n<ul>\n<li>\u8ddf\u626b\u63cf\u672c\u9879\u76ee\u5305\u4e00\u6837\uff0c\u5728 xml\uff0c\u589e\u52a0\u5f15\u5165 jar \u5305\u7684\u626b\u63cf\u8def\u5f84\uff1a<\/li>\n<\/ul>\n<pre><code>&lt;context:component-scan base-package=\"\u5f15\u5165 jar \u5305\u7684 base-package\" \/&gt; ... <\/code><\/pre>\n<p>\u55ef\uff0c\u672c\u8282\u6ca1\u6709\u6e90\u7801<\/p>\n<h2>\u4e94\u3001\u7ed3\u8bed<\/h2>\n<p>\u4ee5\u4e0a\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u6613\u7684 Spring IoC \u5bb9\u5668\uff0c\u987a\u4fbf\u8bf4\u4e86\u4e00\u4e0b Spring Boot \u539f\u7406\u3002Spring \u8fd8\u6709\u5f88\u591a\u91cd\u8981\u529f\u80fd\uff0c\u5982\uff1a\u7ba1\u7406 Bean \u751f\u547d\u5468\u671f\u3001AOP \u7684\u5b9e\u73b0\uff0c\u7b49\u7b49\u3002\u540e\u7eed\u6709\u673a\u4f1a\u518d\u505a\u4e00\u6b21\u5206\u4eab\u3002<\/p>\n<p>\u6765\u4e2a\u6ce8\u89e3\u5c0f\u7ed3\uff1a<\/p>\n<ul>\n<li>Spring \u53ea\u5b9e\u4f8b\u5316\u6807\u8bc6\u4e3a @Component \u7684\u7ec4\u4ef6\uff08\u5373\u4e1a\u52a1\u7c7b\u5bf9\u8c61\uff09<\/li>\n<li>@Component \u4f5c\u4e3a\u7ec4\u4ef6\u6807\u8bc6<\/li>\n<li>@Autowired \u7528\u4e8e\u5224\u65ad\u662f\u5426\u9700\u8981\u4f9d\u8d56\u6ce8\u5165<\/li>\n<li>@ComponentScan \u6307\u5b9a\u7ec4\u4ef6\u626b\u63cf\u8def\u5f84\uff0c\u4e0d\u6307\u5b9a\u5373\u4e3a\u5f53\u524d\u8def\u5f84<\/li>\n<li>@Configuration \u4ee3\u8868\u914d\u7f6e\u7c7b\uff0c\u4f5c\u4e3a\u5165\u53e3<\/li>\n<li>@EnableAutoConfiguration \u5b9e\u73b0\u52a0\u8f7d\u914d\u7f6e\u7c7b<\/li>\n<\/ul>\n<p>\u6709\u7684\u7ae5\u978b\u53ef\u80fd\u8fd8\u4f1a\u6709\u8fd9\u6837\u7684\u7591\u95ee\uff1a<\/p>\n<blockquote>\n<p>jdk jar \u5305\u3001\u5de5\u5177 jar \u5305\u7684\u7c7b\u662f\u5426\u9700\u8981\u6ce8\u5165\u5bb9\u5668\uff1f<\/p>\n<\/blockquote>\n<ul>\n<li>\u56de\u7b54\u662f\u4e0d\u9700\u8981\uff0c\u56e0\u4e3a\u5bb9\u5668\u53ea\u7ba1\u7406\u4e1a\u52a1\u7c7b\uff0c\u6ce8\u5165\u5bb9\u5668\u7684\u7c7b\u90fd\u6709 @Component \u6ce8\u89e3\u3002<\/li>\n<\/ul>\n<p>\u5168\u6587\u5b8c\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>4<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1218445\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : guyeu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             1. @Component \u548c @Bean \u6ce8\u89e3\uff1b<br \/>2. \u63a8\u8350\u4f7f\u7528\u6784\u9020\u65b9\u6cd5\u7684\u65b9\u5f0f\u8868\u793a\u4f9d\u8d56\u6ce8\u5165\uff1b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1218446\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : guyeu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5e76\u4e0d\u662f\u8bf4 jdk jar \u5305\u3001\u5de5\u5177 jar \u5305\u7684\u7c7b\u4e0d\u9700\u8981\u6ce8\u5165\u5bb9\u5668\uff0c\u4e00\u4e2a\u5e38\u89c1\u7684\u4f8b\u5b50\u662f\u4f7f\u7528 spring \u7ba1\u7406\u7ebf\u7a0b\u6c60\u3002\u4e00\u5207\u9700\u8981\u5b9e\u4f8b\u5316\u5e76\u88ab\u4f9d\u8d56\u7684\u5bf9\u8c61\u90fd\u53ef\u4ee5\u6258\u7ba1\u7ed9 spring\uff0c\u5e76\u901a\u8fc7\u4f9d\u8d56\u6ce8\u5165\u7684\u65b9\u5f0f\u63d0\u4f9b\u7ed9\u4f7f\u7528\u8005\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1218447\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : 460881773 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u8bf4\u4e86\u5148\u6807\u8bb0\u4e00\u4e0b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1218448\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : deppwxq <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @guyeu \u771f\u77e5\u707c\u89c1\uff0c\u611f\u8c22\uff0c\u6211\u4fee\u6539\u4e00\u4e0b\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5b9e\u73b0\u4e00\u4e2a\u7b80\u6613\u7684 Spring Io&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/94591"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=94591"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/94591\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=94591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=94591"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=94591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}