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