最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Spring DI依賴注入的方式和類型

 更新時(shí)間:2023年05月08日 08:51:32   作者:會洗碗的CV工程師  
這篇文章主要介紹了詳解Spring DI依賴注入的方式和類型,DI是由容器動態(tài)的將某個依賴關(guān)系注入到組件之中。依賴注入的目的并非為軟件系統(tǒng)帶來更多功能,而是為了提升組件重用的頻率,并為系統(tǒng)搭建一個靈活、可擴(kuò)展的平臺,需要的朋友可以參考下

一、什么是依賴注入

依賴注入(Dependency Injection,簡稱DI),它是Spring控制反轉(zhuǎn)思想的具體實(shí)現(xiàn)。 控制反轉(zhuǎn)將對象的創(chuàng)建交給了Spring,但是對象中可能會依賴其他對象。比如service類中要有dao類的屬性,我們稱service依賴于dao。之前需要手動注入屬性值,代碼如下:

public interface StudentDao {
  Student findById(int id);
}
public class StudentDaoImpl implements StudentDao{
  @Override
  public Student findById(int id) {
    // 模擬根據(jù)id查詢學(xué)生
    return new Student(1,"程序員","北京");
 }
}
public class StudentService {
   // service依賴dao,手動注入屬性值,即手動維護(hù)依賴關(guān)系
  private StudentDao studentDao = new StudentDaoImpl();
  public Student findStudentById(int id){
    return studentDao.findById(id);
 }
}

此時(shí),當(dāng)StudentService的想要使用StudentDao的另一個實(shí)現(xiàn)類如StudentDaoImpl2時(shí),則需要修改Java源碼,造成代碼的可維護(hù)性降低。

而使用Spring框架后,Spring管理Service對象與Dao對象,此時(shí)它能夠?yàn)镾ervice對象注入依賴的Dao屬性值。這就是Spring的依賴注入。簡單來說,控制反轉(zhuǎn)是創(chuàng)建對象,依賴注入是為對象的屬性賦值

二、依賴注入方式

1. Setter注入

被注入類編寫屬性的setter方法

    public void setStudentDao(StudentDao studentDao){
        this.studentDao = studentDao;
    }

配置文件中,給需要注入屬性值的 <bean> 中設(shè)置 <property>

<bean id="studentDao" class="com.itbaizhan.dao.StudentDaoImpl"> </bean>
<bean id="studentService" class="com.itbaizhan.service.StudentService">
  <!--依賴注入-->
  <!--name:對象的屬性名 ref:容器中對象的id值-->
  <property name="studentDao" ref="studentDao"></property>
</bean>

測試

新增測試方法

    // 測試依賴注入
    @Test
    public void t6(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        StudentService service = (StudentService) ac.getBean("studentService");
        System.out.println(service.findStudentById(8));
    }

運(yùn)行結(jié)果

OK,確實(shí)成功測試到了

2. 構(gòu)造方法注入

被注入類編寫有參的構(gòu)造方法

    public StudentService(StudentDao studentDao){
        this.studentDao = studentDao;
    }

給需要注入屬性值的 <bean> 中設(shè)置 <constructor-arg>

<bean id="studentDao" class="com.itbaizhan.dao.StudentDaoImpl"></bean>
<bean id="studentService" class="com.itbaizhan.service.StudentService">
  <!-- 依賴注入 -->
  <!-- name:對象的屬性名 ref:配置文件中注入對象的id值 -->
  <constructor-arg name="studentDao" ref="studentDao"></constructor-arg>
</bean>

測試結(jié)果:

OK,確實(shí)也是可以使用的

3. 自動注入

自動注入不需要在 <bean> 標(biāo)簽中添加其他標(biāo)簽注入屬性值,而是自動從容器中找到相應(yīng)的bean對象設(shè)置為屬性值。

自動注入有兩種配置方式:

  • 全局配置:在 <beans> 中設(shè)置 default-autowire 屬性可以定義所有bean對象的自動注入策略。
  • 局部配置:在 <bean> 中設(shè)置 autowire 屬性可以定義當(dāng)前bean對象的自動注入策略。

autowire的取值如下:

  • no:不會進(jìn)行自動注入。
  • default:全局配置default相當(dāng)于no,局部配置default表示使用全局配置
  • byName:在Spring容器中查找id與屬性名相同的bean,并進(jìn)行注入。需要提供set方法。
  • byType:在Spring容器中查找類型與屬性類型相同的bean,并進(jìn)行注入。需要提供set方法。
  • constructor:在Spring容器中查找id與屬性名相同的bean,并進(jìn)行注入。需要提供構(gòu)造方法。

三、依賴注入類型

DI支持注入bean類型、基本數(shù)據(jù)類型和字符串、List集合、Set集合、Map集合、Properties對象類型等,他們的寫法如下:

準(zhǔn)備注入屬性的類

package com.example.service;
import com.example.dao.StudentDao;
import com.example.pojo.Student;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class StudentService {
    // service依賴dao,手動注入屬性值,即手動維護(hù)依賴關(guān)系
    //private StudentDao studentDao;
    // bean屬性
    private StudentDao studentDao;
    // 字符串類型
    private String name;
    // 基本數(shù)據(jù)類型
    private int count;
    // 字符串List集合
    private List<String> students1;
    // 對象類型List集合
    private List<Student> nameList;
    // 字符串類型Set集合
    private Set<String> students2;
    // 字符串類型Map集合
    private Map<String, String> students3;
    // 對象類型map集合
    private Map<String,Student> studentMap;
    // Properties類型
    private Properties properties;
    public StudentService(){}
    public StudentService(StudentDao studentDao){
        this.studentDao = studentDao;
    }
    public Student findStudentById(int id){
        return studentDao.findById(id);
    }
    public void setStudentDao(StudentDao studentDao){
        this.studentDao = studentDao;
    }
    public StudentDao getStudentDao() {
        return studentDao;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getCount() {
        return count;
    }
    public void setCount(int count) {
        this.count = count;
    }
    public List<String> getStudents1() {
        return students1;
    }
    public void setStudents1(List<String> students1) {
        this.students1 = students1;
    }
    public Set<String> getStudents2() {
        return students2;
    }
    public void setStudents2(Set<String> students2) {
        this.students2 = students2;
    }
    public Map<String, String> getNames2() {
        return students3;
    }
    public void setNames2(Map<String, Student> names2) {
        this.studentMap = names2;
    }
    public Map<String, String> getStudents3() {
        return students3;
    }
    public void setStudents3(Map<String, String> students3) {
        this.students3 = students3;
    }
    public Properties getProperties() {
        return properties;
    }
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
    public List<Student> getNameList() {
        return nameList;
    }
    public void setNameList(List<Student> nameList) {
        this.nameList = nameList;
    }
    @Override
    public String toString() {
        return "StudentService[ " +
                "studentDao=" + studentDao +
                ", name='" + name + '\'' +
                ", count=" + count +
                ", students1=" + students1 +
                ", nameList=" + nameList +
                ", students2=" + students2 +
                ", students3=" + students3 +
                ", studentMap=" + studentMap +
                ", properties=" + properties +
                " ]";
    }
}

準(zhǔn)備測試方法

    // 測試注入類型
    @Test
    public void t7(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        StudentService service = (StudentService) ac.getBean("studentService");
        System.out.println(service);
    }

1. 注入bean類型

    <!-- 注入bean類型 -->
    <bean id="studentDao" class="com.example.dao.StudentDaoImpl1"/>
    <!-- 寫法1 -->
    <bean id="studentService" class="com.example.service.StudentService">
        <property name="studentDao" ref="studentDao"/>
    </bean>
    <!-- 寫法2 -->
    <!--<bean id="studentService" class="com.example.service.StudentService">
        <property name="studentDao">
            <ref bean="studentDao"/>
        </property>
    </bean>-->

2. 注入基本數(shù)據(jù)類型

        <!-- 注入基本數(shù)據(jù)類型 -->
        <!-- 寫法一 name:屬性名 value:屬性值 -->
        <property name="name" value="程序員"/>
        <!-- 寫法二 name:屬性名 value:屬性值-->
        <property name="count">
            <value>10</value>
        </property>

3. 注入List集合

    <!-- 注入List集合 -->
        <!-- 簡單的數(shù)據(jù)類型List集合 name:屬性名 -->
        <property name="students1" >
            <list>
                <value>上海</value>
                <value>廣州</value>
            </list>
        </property>
        <!-- 對象類型的List集合 name:屬性名 -->
        <property name="nameList">
            <list>
                <bean class="com.example.pojo.Student">
                    <property name="id" value="1"/>
                    <property name="name" value="幾何心涼"/>
                    <property name="address" value="北京"/>
                </bean>
                <bean class="com.example.pojo.Student">
                    <property name="id" value="2"/>
                    <property name="name" value="哈士奇"/>
                    <property name="address" value="上海"/>
                </bean>
            </list>
        </property>

4. 注入Set集合

        <!-- 注入Set集合 -->
        <property name="students2">
            <set>
                <value>深圳</value>
                <value>北京</value>
            </set>
        </property>

5. 注入Map集合

        <!-- 注入Map集合 -->
        <property name="students3">
            <map>
                <entry key="哈士奇" value="上海"/>
                <entry key="幾何心涼" value="北京"/>
            </map>
        </property>
        <!-- 注入對象類型map類型 -->
        <property name="names2">
            <map>
                <entry key="student1" value-ref="s1"/>
                <entry key="student2" value-ref="s2"/>
            </map>
        </property>
    <bean id="s1" class="com.example.pojo.Student">
        <property name="id" value="1"/>
        <property name="name" value="幾何心涼"/>
        <property name="address" value="北京"/>
    </bean>
    <bean id="s2" class="com.example.pojo.Student">
        <property name="id" value="2"/>
        <property name="name" value="哈士奇"/>
        <property name="address" value="上海"/>
    </bean>

上面是用到的bean對象

6. 注入Properties對象

        <!-- 注入properties -->
        <property name="properties">
            <props>
                <prop key="配置1">值1</prop>
                <prop key="配置2">值2</prop>
            </props>
        </property>

運(yùn)行測試方法測試一下

OK ,可以看到都是插入的了。

到此這篇關(guān)于詳解Spring DI依賴注入的方式和類型的文章就介紹到這了,更多相關(guān)Spring DI依賴注入 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java中的ThreadLocal線程變量詳解

    Java中的ThreadLocal線程變量詳解

    這篇文章主要介紹了Java中的ThreadLocal線程變量詳解,ThreadLocal叫做線程變量,意思是在ThreadLocal中填充的變量屬于當(dāng)前線程,該變量對其他線程而言是隔離的,它是用來提供線程內(nèi)部的局部變量,需要的朋友可以參考下
    2024-01-01
  • Java?數(shù)據(jù)結(jié)構(gòu)與算法系列精講之棧

    Java?數(shù)據(jù)結(jié)構(gòu)與算法系列精講之棧

    棧(stack)又名堆棧,它是一種運(yùn)算受限的線性表。限定僅在表尾進(jìn)行插入和刪除操作的線性表。這一端被稱為棧頂,相對地,把另一端稱為棧底,棧是基礎(chǔ)中的基礎(chǔ),如果你還沒掌握透徹就來接著往下看吧
    2022-02-02
  • spring事務(wù)里面開啟線程插入報(bào)錯了是否會回滾

    spring事務(wù)里面開啟線程插入報(bào)錯了是否會回滾

    這篇文章主要介紹了spring事務(wù)里面開啟線程插入,報(bào)錯了是否會回滾?這是小編遇到一道面試題,題目大概是這個樣子,今天抽空通過示例代碼給大家分析下,需要的朋友可以參考下
    2023-04-04
  • SpringBoot中緩存注解的使用詳解

    SpringBoot中緩存注解的使用詳解

    為了實(shí)現(xiàn)緩存,Spring?Boot?提供了一些緩存注解,可以方便地實(shí)現(xiàn)緩存功能,這篇文章主要介紹了SpringBoot中常用的緩存注解的使用方法,需要的可以參考一下
    2023-06-06
  • 深入了解MyBatis參數(shù)

    深入了解MyBatis參數(shù)

    今天小編就為大家分享一篇關(guān)于深入了解MyBatis參數(shù),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Java實(shí)現(xiàn)的計(jì)算最大下標(biāo)距離算法示例

    Java實(shí)現(xiàn)的計(jì)算最大下標(biāo)距離算法示例

    這篇文章主要介紹了Java實(shí)現(xiàn)的計(jì)算最大下標(biāo)距離算法,涉及java針對數(shù)組的遍歷、運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下
    2018-02-02
  • 使用maven?shade插件解決項(xiàng)目版本沖突詳解

    使用maven?shade插件解決項(xiàng)目版本沖突詳解

    這篇文章主要為大家介紹了使用maven?shade插件解決項(xiàng)目版本沖突詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • java 利用反射機(jī)制,獲取實(shí)體所有屬性和方法,并對屬性賦值

    java 利用反射機(jī)制,獲取實(shí)體所有屬性和方法,并對屬性賦值

    這篇文章主要介紹了 java 利用反射機(jī)制,獲取實(shí)體所有屬性和方法,并對屬性賦值的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • Java AES加密解密的簡單實(shí)現(xiàn)方法

    Java AES加密解密的簡單實(shí)現(xiàn)方法

    下面小編就為大家?guī)硪黄狫ava AES加密解密的簡單實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • MyBatisPuls多數(shù)據(jù)源操作數(shù)據(jù)源偶爾報(bào)錯問題

    MyBatisPuls多數(shù)據(jù)源操作數(shù)據(jù)源偶爾報(bào)錯問題

    這篇文章主要介紹了MyBatisPuls多數(shù)據(jù)源操作數(shù)據(jù)源偶爾報(bào)錯問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06

最新評論

东辽县| 抚顺县| 巴青县| 昌邑市| 江都市| 行唐县| 天长市| 乡城县| 东方市| 慈溪市| 汕尾市| 彭山县| 丹寨县| 镇宁| 神池县| 辽源市| 岢岚县| 额敏县| 兰坪| 河东区| 富平县| 汕头市| 天门市| 香港 | 中江县| 威信县| 宁德市| 曲靖市| 卢湾区| 肃宁县| 永顺县| 腾冲县| 万荣县| 盐池县| 如皋市| 东兰县| 临泽县| 新源县| 黎平县| 嘉义市| 广丰县|