欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring IOC簡單理解及創(chuàng)建對象的方式

 更新時間:2021年09月07日 11:53:50   作者:筆鋒Sharpen  
這篇文章主要介紹了Spring IOC簡單理解及創(chuàng)建對象的方式,本文通過兩種方式給大家介紹創(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)用中使用acutator/refresh刷新屬性不生效的問題分析及解決

    在Spring應(yīng)用收到/actuator/refresh的POST請求后,標注了@RefreshScope以及@ConfiguratioinProperties的bean會被Spring容器重新加載,但是,在實際應(yīng)用中,并沒有按照預(yù)期被Spring容器加載,本文將討論導致這種未按預(yù)期刷新的一種原因,感興趣的朋友可以參考下
    2024-01-01
  • Java?Kryo,Protostuff,Hessian序列化方式對比

    Java?Kryo,Protostuff,Hessian序列化方式對比

    這篇文章主要介紹了Java?Kryo,Protostuff,Hessian序列化方式對比,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-07-07
  • 詳解Spring中的AOP及AspectJ五大通知注解

    詳解Spring中的AOP及AspectJ五大通知注解

    這篇文章主要介紹了詳解Spring中的AOP及AspectJ五大通知注解,AOP面向切面編程是一種新的方法論,是對傳統(tǒng)OOP面向?qū)ο缶幊痰难a充,AOP?的主要編程對象是切面(aspect),切面模塊化橫切關(guān)注點,需要的朋友可以參考下
    2023-08-08
  • SpringBoot中Redisson延遲隊列的示例

    SpringBoot中Redisson延遲隊列的示例

    延時隊列是一種常見的需求,延時隊列允許我們延遲處理某些任務(wù),本文主要介紹了Redisson延遲隊列的示例,具有一定的參考價值,感興趣的可以了解一下
    2024-06-06
  • Java?EE實現(xiàn)用戶后臺管理系統(tǒng)

    Java?EE實現(xiàn)用戶后臺管理系統(tǒng)

    這篇文章主要為大家詳細介紹了Java?EE實現(xiàn)用戶后臺管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • 關(guān)于springboot 中使用httpclient或RestTemplate做MultipartFile文件跨服務(wù)傳輸?shù)膯栴}

    關(guān)于springboot 中使用httpclient或RestTemplate做MultipartFile文件跨服務(wù)傳輸

    這篇文章主要介紹了關(guān)于springboot 中使用httpclient或RestTemplate做MultipartFile文件跨服務(wù)傳輸?shù)膯栴},本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • springboot多模塊包掃描問題的解決方法

    springboot多模塊包掃描問題的解決方法

    這篇文章主要介紹了springboot多模塊包掃描問題的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • SpringBoot中緩存注解的使用詳解

    SpringBoot中緩存注解的使用詳解

    為了實現(xiàn)緩存,Spring?Boot?提供了一些緩存注解,可以方便地實現(xiàn)緩存功能,這篇文章主要介紹了SpringBoot中常用的緩存注解的使用方法,需要的可以參考一下
    2023-06-06
  • JAVA簡單分組的算法實現(xiàn)

    JAVA簡單分組的算法實現(xiàn)

    本文介紹了“JAVA簡單分組的算法實現(xiàn)”,需要的朋友可以參考一下
    2013-03-03
  • SpringBoot頂層接口實現(xiàn)類注入項目的方法示例

    SpringBoot頂層接口實現(xiàn)類注入項目的方法示例

    本文主要介紹了SpringBoot頂層接口實現(xiàn)類注入項目的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-06-06

最新評論