spring ioc和di的区别 Spring DI 也就是IoC的原理是什么 ?
\u9762\u8bd5\u9898\uff1aSpring\u4e2dIOC\u548cDI\u7684\u542b\u4e49\u3002\u5e94\u8be5\u600e\u4e48\u7b54ioc\u5c31\u662f\u63a7\u5236\u7ffb\u8f6c\u6216\u662f\u4f9d\u8d56\u6ce8\u5165\u3002\u901a\u4fd7\u7684\u8bb2\u5c31\u662f\u5982\u679c\u5728\u4ec0\u4e48\u5730\u65b9\u9700\u8981\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4f60\u81ea\u5df1\u4e0d\u7528\u53bb\u901a\u8fc7new \u751f\u6210\u4f60\u9700\u8981\u7684\u5bf9\u8c61\uff0c\u800c\u662f\u901a\u8fc7spring\u7684bean\u5de5\u5382\u4e3a\u4f60\u957f\u751f\u8fd9\u6837\u4e00\u4e2a\u5bf9\u8c61\u3002
aop\u5c31\u662f\u9762\u5411\u5207\u9762\u7684\u7f16\u7a0b\u3002\u6bd4\u5982\u8bf4\u4f60\u6bcf\u505a\u4e00\u6b21\u5bf9\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u90fd\u8981\u751f\u6210\u4e00\u53e5\u65e5\u5fd7\u3002\u5982\u679c\uff0c\u4f60\u5bf9\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u6709\u5f88\u591a\u7c7b\uff0c\u90a3\u4f60\u6bcf\u4e00\u7c7b\u4e2d\u90fd\u8981\u5199\u5173\u4e8e\u65e5\u5fd7\u7684\u65b9\u6cd5\u3002\u4f46\u662f\u5982\u679c\u4f60\u7528aop\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u5199\u4e00\u4e2a\u65b9\u6cd5\uff0c\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\u6709\u5173\u4e8e\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u65b9\u6cd5\uff0c\u6bcf\u4e00\u6b21\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u7684\u65f6\u5019\uff0c\u5c31\u52a0\u4e0a\u751f\u6210\u65e5\u5fd7\u7684\u64cd\u4f5c\u3002
\u8bf4\u7684\u4e0d\u662f\u5f88\u6e05\u6670\uff0c\u591a\u5199\u591a\u770b\u5c31\u80fd\u660e\u767d\u4e86\u3002
\u901a\u8fc7DI\uff08Dependency Injection\uff0c\u4f9d\u8d56\u6ce8\u5165\uff09\u6765\u5b9e\u73b0\u7684\u3002\u6bd4\u5982\u5bf9\u8c61A\u9700\u8981\u64cd\u4f5c\u6570\u636e\u5e93\uff0c\u4ee5\u524d\u6211\u4eec\u603b\u662f\u8981\u5728A\u4e2d\u81ea\u5df1\u7f16\u5199\u4ee3\u7801\u6765\u83b7\u5f97\u4e00\u4e2aConnection\u5bf9\u8c61\uff0c\u6709\u4e86 spring\u6211\u4eec\u5c31\u53ea\u9700\u8981\u544a\u8bc9spring\uff0cA\u4e2d\u9700\u8981\u4e00\u4e2aConnection\uff0c\u81f3\u4e8e\u8fd9\u4e2aConnection\u600e\u4e48\u6784\u9020\uff0c\u4f55\u65f6\u6784\u9020\uff0cA\u4e0d\u9700\u8981\u77e5\u9053\u3002\u5728\u7cfb\u7edf\u8fd0\u884c\u65f6\uff0cspring\u4f1a\u5728\u9002\u5f53\u7684\u65f6\u5019\u5236\u9020\u4e00\u4e2aConnection\uff0c\u7136\u540e\u50cf\u6253\u9488\u4e00\u6837\uff0c\u6ce8\u5c04\u5230A\u5f53\u4e2d\uff0c\u8fd9\u6837\u5c31\u5b8c\u6210\u4e86\u5bf9\u5404\u4e2a\u5bf9\u8c61\u4e4b\u95f4\u5173\u7cfb\u7684\u63a7\u5236\u3002A\u9700\u8981\u4f9d\u8d56 Connection\u624d\u80fd\u6b63\u5e38\u8fd0\u884c\uff0c\u800c\u8fd9\u4e2aConnection\u662f\u7531spring\u6ce8\u5165\u5230A\u4e2d\u7684\uff0c\u4f9d\u8d56\u6ce8\u5165\u7684\u540d\u5b57\u5c31\u8fd9\u4e48\u6765\u7684\u3002
IOC\u662f\u63a7\u5236\u53cd\u8f6c,\u6240\u8c13\u53cd\u8f6c\u662f\u6307\u5e94\u7528\u672c\u8eab\u5e76\u4e0d\u8d1f\u8d23\u4f9d\u8d56\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u7ef4\u62a4,\u800c\u628a\u8fd9\u4e2a\u4efb\u52a1\u4ea4\u7ed9\u7b2c\u4e09\u65b9\u5373Spring\u53bb\u5904\u7406,\u8fd9\u662f\u5c06\u521b\u5efa\u5bf9\u8c61\u7684\u6743\u5229\u4ea4\u7ed9\u7b2c\u4e09\u65b9,\u63a7\u5236\u53cd\u8f6c\u5c31\u662f\u63a7\u5236\u6743\u7684\u8f6c\u79fb
DI\u662f\u4f9d\u8d56\u6ce8\u5165.\u4f9d\u8d56\u6ce8\u5165,\u662f\u6307\u8fd0\u884c\u671f\u95f4,Spring\u52a8\u6001\u7684\u5c06\u4f9d\u8d56\u5bf9\u8c61\u6ce8\u5165\u5230\u7ec4\u4ef6\u4e2d\u3002
\u5f53IOC\u5bb9\u5668\u542f\u52a8\u65f6\uff0c\u5bb9\u5668\u8d1f\u8d23\u521b\u5efa\u5bb9\u5668\u5185\u7684\u6240\u6709\u5bf9\u8c61;
\u6839\u636e\u914d\u7f6e\u4fe1\u606f\u5f62\u6210\u5bf9\u8c61\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\uff1b
\u4e5f\u5c31\u662f\u8bf4\u8ba9\u522b\u4eba(ioc)\u4e3a\u4f60\u670d\u52a1\u3002\u8fd9\u662f\u6838\u5fc3\u3002
Dependency Injection
原来,它叫IoC。
Martin Flower发话了,是个框架都有IoC,这不足以新生容器反转的“如何定位插件的具体实现”,于是,它有了个新名字,Dependency Injection。
其实,它就是一种将调用者与被调用者分离的思想,Uncle Bob管它叫DIP(Dependency Inversion Principle),并把它归入OO设计原则。
同Spring相比,它更早进入我的大脑。一切都是那么朦胧,直至Spring出现。
慢
慢的,我知道了它还分为Interface Injection(type 1),Setter Injection(type
2),Constructor Injection(type 3)。Martin Flower那篇为它更名的大作让我心目关于它的一切趋于完整。
在Spring中,它是一切的基础。Spring的种种优势随之而来。
于我而言,它为我带来更多的是思维方式的转变,恐怕以后我再也无法写出那种一大块的全功能程序了。
扩展阅读:springboot ioc aop ... spring cloud gateway ... spring ioc和aop原理 面试 ... spring ioc源码 ... springcloud alibaba ... spring ioc和aop面试回答 ... spring ioc理解 ... spring ioc容器的作用 ... spring aop实现原理 ...