javascript
Spring学习(四)IOC详解
本文借鑒:Spring學(xué)習(xí)(特此感謝!)
一、簡(jiǎn)介
概念:控制反轉(zhuǎn)是一種通過(guò)描述(在 Java 中可以是 XML 或者注解)并通過(guò)第三方(Spring)去產(chǎn)生或獲取特定對(duì)象的方式。(被動(dòng)創(chuàng)建)
優(yōu)勢(shì):
①?降低對(duì)象之間的耦合
②?我們不需要理解一個(gè)類的具體實(shí)現(xiàn),只需要知道它有什么用就好了(直接向 IoC 容器拿)
小結(jié):主動(dòng)創(chuàng)建的模式中,責(zé)任歸于開發(fā)者,而在被動(dòng)的模式下,責(zé)任歸于 IoC 容器,基于這樣的被動(dòng)形式,我們就說(shuō)對(duì)象被控制反轉(zhuǎn)了。(也可以說(shuō)是反轉(zhuǎn)了控制)
二、IOC容器
Spring 會(huì)提供IoC 容器來(lái)管理和容納我們所開發(fā)的各種各樣的 Bean,并且我們可以從中獲取各種發(fā)布在 Spring IoC 容器里的 Bean,并且通過(guò)描述可以得到它。
?三、容器設(shè)計(jì)
主要基于BeanFactory和ApplicationContext 兩個(gè)接口。
BeanFactory 是 Spring IoC 容器所定義的最底層接口,而 ApplicationContext 是其最高級(jí)接口之一,并對(duì) BeanFactory 功能做了許多的擴(kuò)展,所以在絕大部分的工作場(chǎng)景下,都會(huì)使用 ApplicationContext 作為 Spring IoC 容器。
ApplicationContext的常用擴(kuò)展類:
ClassPathXmlApplicationContext:從類路徑下加載配置文件
FileSystemXmlApplication:從硬盤的絕對(duì)路徑下加載配置文件
BeanFactory
?常用方法
【getBean】對(duì)應(yīng)了多個(gè)方法來(lái)獲取配置給 Spring IoC 容器的 Bean。
① 按照類型拿 bean:
bean = (Bean) factory.getBean(Bean.class);PS:要求在 Spring 中只配置了一個(gè)這種類型的實(shí)例,否則報(bào)錯(cuò)。(如果有多個(gè)那 Spring 就懵了,不知道該獲取哪一個(gè))
② 按照 bean 的名字拿 bean:
bean = (Bean) factory.getBean("beanName");PS:這種方法不太安全,IDE 不會(huì)檢查其安全性(關(guān)聯(lián)性)
③ 按照名字和類型拿 bean:(推薦)
bean = (Bean) factory.getBean("beanName", Bean.class);【isSingleton】用于判斷是否單例,如果判斷為真,其意思是該 Bean 在容器中是作為一個(gè)唯一單例存在的。
【isPrototype】則相反,如果判斷為真,意思是當(dāng)你從容器中獲取 Bean,容器就為你生成一個(gè)新的實(shí)例。
PS:在默認(rèn)情況下,【isSingleton】為 ture,而【isPrototype】為 false
【getAliases】方法是獲取別名的方法
ApplicationContext
1、先在【src】目錄下創(chuàng)建一個(gè) 【bean.xml】 文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 通過(guò) xml 方式裝配 bean --><bean name="source" class="pojo.Source"><property name="fruit" value="橙子"/><property name="sugar" value="多糖"/><property name="size" value="超大杯"/></bean> </beans>PS:這里定義了一個(gè) bean ,這樣 Spring IoC 容器在初始化的時(shí)候就能找到它們,然后使用 ClassPathXmlApplicationContext 容器就可以將其初始化
2、使用 ClassPathXmlApplicationContext 容器將其初始化:
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); Source source = (Source) context.getBean("source", Source.class);System.out.println(source.getFruit()); System.out.println(source.getSugar()); System.out.println(source.getSize());四、ApplicationContext的常見實(shí)現(xiàn)類
ClassPathXmlApplicationCntext
讀取classpath中的資源
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");FileSystemXmlApplicationContext
讀取指定路徑的資源
ApplicationContext ac = new FileSystemXmlApplicationContext("c:/applicationContext.xml");XmlWebApplicationContext
需要在Web的環(huán)境下才可以運(yùn)行
XmlWebApplicationContext ac = new XmlWebApplicationContext(); // 這時(shí)并沒有初始化容器 ac.setServletContext(servletContext); // 需要指定ServletContext對(duì)象 ac.setConfigLocation("/WEB-INF/applicationContext.xml"); // 指定配置文件路徑,開頭的斜線表示W(wǎng)eb應(yīng)用的根目錄 ac.refresh(); // 初始化容器五、IOC初始化、注入的實(shí)現(xiàn)原理
Bean的定義
1、資源定位
Spring IoC 容器先根據(jù)開發(fā)者的配置,進(jìn)行資源的定位,在 Spring 的開發(fā)中,通過(guò) XML 或者注解都是十分常見的方式,定位的內(nèi)容是由開發(fā)者提供的。
2、獲取Bean
這個(gè)時(shí)候只是將定位到的資源信息,保存到 Bean 定義(BeanDefinition)中,此時(shí)并不會(huì)創(chuàng)建 Bean 的實(shí)例。
3、注冊(cè)Bean到IOC容器中
將定義好的Bean的信息發(fā)布到 Spring IoC 容器中
PS:做完了以上 3 步,Bean 就在 Spring IoC 容器中被定義了,但是沒有被初始化,更沒有完成依賴注入,此時(shí)它還不能完全使用。
Bean的初始化和注入
Spring IoC 默認(rèn)會(huì)自動(dòng)初始化 Bean。
Spring Bean 還有一個(gè)配置選項(xiàng)——【lazy-init】,其含義就是是否初始化 Spring Bean。
如果將其設(shè)置為 true,那么只有當(dāng)我們使用 Spring IoC 容器的 getBean 方法獲取它時(shí),它才會(huì)進(jìn)行 Bean 的初始化,完成依賴注入。
?
轉(zhuǎn)載于:https://www.cnblogs.com/riches/p/11510981.html
總結(jié)
以上是生活随笔為你收集整理的Spring学习(四)IOC详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 基本运算符与if while详解:
- 下一篇: Spring学习(六)bean装配详解之