Spring 源码学习 02:关于 Spring IoC 和 Bean 的概念

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"前言","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在前一篇文章中介绍了如何构建源码阅读环境,既然构建好了源码环境,本地也可以正常运行,那就开始阅读源码吧!","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在阅读源码时,会参考","attrs":{}},{"type":"link","attrs":{"href":"https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core","title":""},"content":[{"type":"text","text":"官方文档","attrs":{}}]},{"type":"text","text":",很多概念在官网都可以得到答案,有兴趣的小伙伴们可以继续阅读,当做复习,写的不足之处,希望多多指导。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"IoC 和 DI","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c5/c58cb4a04b9b9bee9043d610d1919fbc.png","alt":"IoC 概念","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"IoC","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"IoC(Inversion of Control),即控制反转。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"之前是在对象内部 new 创建其他对象,然后使用。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"而现在 Spring 中有一个容器可以在创建管理这些对象,并且将对象依赖的其他对象注入到这个对象中,这些对象的创建、销毁都由 Spring 进行管理。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"相比以前来说,不再由自己控制其他对象的生命周期,这个过程就叫做控制反转。而负责统一管理这些类的容器就叫做 IoC 容器。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"DI","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"IoC is also known as dependency injection (DI).","attrs":{}}],"attrs":{}},{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"是不是感觉奇奇怪怪的,为什么说:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"IoC 也称为 DI","attrs":{}}],"attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其实 IoC 和 DI 是同一个概念的不同角度描述。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"依赖注入是指组件之间的依赖关系由容器在运行期决定,形象的说,即由容器动态的将某个依赖关系注入到组件之中。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通过依赖注入机制,我们只需要通过简单的配置,而无需任何代码就可指定目标需要的资源,完成自身的业务逻辑,而不需要关心具体的资源来自何处,由谁实现。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Spring 是通过 DI 实现 IoC 的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"Container 和 Bean","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/54/54f076ef8a460d1d31e83759cfec26f1.png","alt":"Spring IoC container and Bean","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Bean 是一个由 Spring IoC 容器实例化,组装和管理的对象。","attrs":{}}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"相信大家都写过或者见过下面的代码:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"/**\n * 从容器中获取对象\n * @author liuzhihang\n * @date 2020/4/6 19:02\n */\n@Component\npublic class CustomBeanFactory implements ApplicationContextAware {\n\n private static ApplicationContext ctx;\n\n @Override\n public void setApplicationContext(ApplicationContext ac) throws BeansException {\n\n ctx = ac;\n }\n\n public static Object getBean(String beanName) {\n\n return ctx.getBean(beanName);\n }\n}","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"代码逻辑很简单,就是从容器中获取到指定名称的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Bean","attrs":{}}],"attrs":{}},{"type":"text","text":",而其中 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"ApplicationContext","attrs":{}}],"attrs":{}},{"type":"text","text":" 接口其实就是 Spring IoC 容器。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当然 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"ApplicationContext","attrs":{}}],"attrs":{}},{"type":"text","text":" 是一个接口,它有很多实现,而它也继承了 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"BeanFactory","attrs":{}}],"attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/62/6258133dff67e25067c08b89b9d78771.png","alt":"BeanFactory or ApplicationContext","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"虽然 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"BeanFactory","attrs":{}}],"attrs":{}},{"type":"text","text":" 是 IoC 容器的最基本的形式,但是 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"ApplicationContext","attrs":{}}],"attrs":{}},{"type":"text","text":" 对其进行了很多扩展,并具有 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"BeanFactory","attrs":{}}],"attrs":{}},{"type":"text","text":" 的所有功能,通常建议优先使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"ApplicationContext","attrs":{}}],"attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"总结","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在通过","attrs":{}},{"type":"link","attrs":{"href":"https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core","title":""},"content":[{"type":"text","text":"Spring 官网","attrs":{}}]},{"type":"text","text":" 了解了 IoC 、DI 、容器和 Bean 的概念后,再结合平常的使用基本上可以有个大概流程。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/67/67f7791b516be90f015124cb1c616ab9.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当然,这只是一个很粗略的猜想,是否正确,还有待后面继续阅读源码,然后去验证。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"相关推荐","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://mp.weixin.qq.com/s/gDhJMSPSX2vz68p5X3juow","title":""},"content":[{"type":"text","text":"Spring 源码学习 01:源码阅读环境的搭建","attrs":{}}]}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://mp.weixin.qq.com/s/BORxKatt9qs4rDgmAHbv_A","title":""},"content":[{"type":"text","text":"Spring 自调用事务失效,你是怎么解决的?","attrs":{}}]}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://mp.weixin.qq.com/s/ZJB-2WWeJFG7RQxaksNBuA","title":""},"content":[{"type":"text","text":"APP 莫名崩溃,开始以为是 Header 中 name 大小写的锅,最后发现原来是容器的错","attrs":{}}]}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章