Spring IOC簡(jiǎn)單理解及創(chuàng)建對(duì)象的方式
spring框架
控制反轉(zhuǎn)(Inversion on Control)在spring框架里面,一般交給Spring容器,這叫控制反轉(zhuǎn)
什么是控制反轉(zhuǎn)呢?
先來(lái)說(shuō)一下控制正轉(zhuǎn),
class Demo{ Student student = new Student(); }
簡(jiǎn)單地來(lái)說(shuō)就是自己去創(chuàng)建對(duì)象,需要什么對(duì)象,就創(chuàng)建什么對(duì)象,實(shí)在當(dāng)前文件中創(chuàng)建出來(lái)的,自己new出來(lái)的,這就叫做“控制正轉(zhuǎn)”
那么控制反轉(zhuǎn)是什么:
就是跟控制正轉(zhuǎn)反著來(lái),就是我需要的對(duì)象,我不需要自己new創(chuàng)建出來(lái),我只需要到一個(gè)地方去取過(guò)來(lái)用,相當(dāng)于讓別人創(chuàng)建出來(lái)我們需要的對(duì)象。另外,讓其它人來(lái)創(chuàng)建對(duì)象有兩種方式:第一種是直接調(diào)用有參構(gòu)造方法,另一種方法是調(diào)用構(gòu)造方法,然后使用set方法實(shí)現(xiàn)。
第一種方式是在spring的配置文件中(applicationContext.xml)中寫(xiě)
applicationContext.xml
<!-- 其中scope是范圍的意思 singleton是單例模式,無(wú)論是否存在創(chuàng)建student這個(gè)操作,都會(huì)創(chuàng)建一個(gè)student對(duì)象,只創(chuàng)建一個(gè) prototype是多例模式,只要當(dāng)你進(jìn)行創(chuàng)建操作的時(shí)候才會(huì)進(jìn)行創(chuàng)建 舉個(gè)例子,單例模式就像一臺(tái)電腦,無(wú)論你用不用,它都在那里,也不會(huì)分裂多出一個(gè),也不會(huì)少一個(gè) 而多例模式就像擠牙膏,就是那種,你擠多少,出來(lái)多少,如果不擠就沒(méi)有 --> <bean scope="singleton" name="student" class="com.example.spring.entity.Student"> <constructor-arg name="id" value="1"/> <constructor-arg name="name" value="張三"/> <!-- 當(dāng)你創(chuàng)建的對(duì)像包括一個(gè)引用類型的時(shí)候,使用ref:reference:參考,引用來(lái)進(jìn)行構(gòu)造,調(diào)用的就是下面的course對(duì)象 --> <constructor-arg name="course" ref="com.example.spring.entity.Course"/> </bean>
Demo.class
public class Demo{ public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationCOntext("applicationContext.xml"); Student student = (Student) context.getBean("student"); // 根據(jù)你在applicationContext.xml的名字找到要?jiǎng)?chuàng)建的 } }
第二種方式是使用spring的配置文件中調(diào)用無(wú)參構(gòu)造方法,然后通過(guò)使用set方法將元素放進(jìn)去
applicationContext.xml
<bean scope="singleton" name="course" class="com.example.spring.entity.Course"> <property name="id" value="1"/> <property name="name" value="java"/> </bean>
該種方法是構(gòu)建一個(gè)無(wú)參構(gòu)造方法,然后將<property>
里面對(duì)應(yīng)的元素拿出來(lái),使用set方法放進(jìn)去,至于對(duì)應(yīng)的class文件也等用于是上面的Demo.class
對(duì)于第二種方法的優(yōu)化:
具體表現(xiàn)在三層架構(gòu)方面
StudentController.class
// 前面這個(gè)注釋等同于@Controller,在其他層次對(duì)應(yīng)的就是@service @Repository注釋是放在實(shí)現(xiàn)類上面的 // 相當(dāng)于 ApplicationContext.xml 文件中的 // <bean name="studentController" class="com.example.spring.controller"> // <property name="studentService" ref="studnetService"/> // </bean> @Controller(name="studentController") public class StudentController{ // @Resource(name="studnetService") 就相當(dāng)于調(diào)用setStudentService()方法將下面對(duì)應(yīng)的元素放進(jìn)Controller對(duì)象里 // <property name="studentService" ref="studnetService"/> // 然后 ref 在調(diào)用 // <bean name="studentService" class="com.example.spring.service.impl.StudentServiceImpl"><bean> @Resource(name="studentService") private StudentService studentService; public void selectAll(){ studentService.selectAll(); } public void setStudnetService(StudentService studnetService){ this.studnetService = studentService; } }
在次優(yōu)化變成
StudentController.class
@Controller public class StudnetController{ @Autowired // 這個(gè)會(huì)自動(dòng)進(jìn)行依賴注入,也不用特意寫(xiě)一個(gè)set方法了很方便,但是要注意配置文件,要進(jìn)行配置,掃描注釋 private StudentService studentService; public void selectAll(){ studentService.selectAll(); } }
applicationContext.xml
<!-- 掃描base-package對(duì)應(yīng)的包下面類中所有的注解--> <context:component-scan base-package="com.example.spring"/>
問(wèn)題來(lái)了,Autowried是根據(jù)類型進(jìn)行注入的,但是如果某個(gè)接口存在多個(gè)實(shí)現(xiàn)的子類,那么Autowried是注入哪一個(gè)?又或者說(shuō)會(huì)報(bào)錯(cuò)?
答案:Error create bean with ‘studnetController',原因并不是因?yàn)镾tudentController里面,而是因?yàn)镾tudentController里面使用了Autowired進(jìn)行注入,而存在多個(gè)實(shí)現(xiàn)的幾口expected single matching bean but found 2: banjiServiceImpl,banjiServiceImpl2
,那么解決方案是:
@Controller public class StudnetController{ @Autowired // 添加下面的注解,寫(xiě)明白是用那個(gè)注入 @Qualifier(value="studentServiceImpl2") private StudentService studentService; public void selectAll(){ studentService.selectAll(); } }
到此這篇關(guān)于Spring IOC簡(jiǎn)單理解的文章就介紹到這了,更多相關(guān)Spring IOC內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring應(yīng)用中使用acutator/refresh刷新屬性不生效的問(wèn)題分析及解決
在Spring應(yīng)用收到/actuator/refresh的POST請(qǐng)求后,標(biāo)注了@RefreshScope以及@ConfiguratioinProperties的bean會(huì)被Spring容器重新加載,但是,在實(shí)際應(yīng)用中,并沒(méi)有按照預(yù)期被Spring容器加載,本文將討論導(dǎo)致這種未按預(yù)期刷新的一種原因,感興趣的朋友可以參考下2024-01-01Java?Kryo,Protostuff,Hessian序列化方式對(duì)比
這篇文章主要介紹了Java?Kryo,Protostuff,Hessian序列化方式對(duì)比,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-07-07SpringBoot中Redisson延遲隊(duì)列的示例
延時(shí)隊(duì)列是一種常見(jiàn)的需求,延時(shí)隊(duì)列允許我們延遲處理某些任務(wù),本文主要介紹了Redisson延遲隊(duì)列的示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06Java?EE實(shí)現(xiàn)用戶后臺(tái)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java?EE實(shí)現(xiàn)用戶后臺(tái)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05關(guān)于springboot 中使用httpclient或RestTemplate做MultipartFile文件跨服務(wù)傳輸
這篇文章主要介紹了關(guān)于springboot 中使用httpclient或RestTemplate做MultipartFile文件跨服務(wù)傳輸?shù)膯?wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01JAVA簡(jiǎn)單分組的算法實(shí)現(xiàn)
本文介紹了“JAVA簡(jiǎn)單分組的算法實(shí)現(xiàn)”,需要的朋友可以參考一下2013-03-03SpringBoot頂層接口實(shí)現(xiàn)類注入項(xiàng)目的方法示例
本文主要介紹了SpringBoot頂層接口實(shí)現(xiàn)類注入項(xiàng)目的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06