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

Spring bean為什么需要依賴注入

 更新時間:2021年07月16日 10:22:34   作者:寧在春  
本篇文章主要介紹了Spring依賴注入的三種方式小結,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

具體步驟:

1.創(chuàng)建一個maven項目 spring-day1-constructor

2.導入依賴

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!--這里是java 版本號-->
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <!--這里是方便版本控制-->
        <spring.version>5.3.1</spring.version>
        <lombok.version>1.18.20</lombok.version>
        <junit.version>4.12</junit.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
        </dependency>
    </dependencies>

3.工程項目結構

在這里插入圖片描述

樣例1:

1.創(chuàng)建一個Student類

public class Student {
    private Long number;
    private String name;
    private String school;
    public void setNumber(Long number) {
        this.number = number;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setSchool(String school) {
        this.school = school;
    }
    public Student() {
    }
    public Student(Long number, String name, String school) {
        this.number = number;
        this.name = name;
        this.school = school;
    }
    @Override
    public String toString() {
        return "Student{" +
                "number=" + number +
                ", name='" + name + '\'' +
                ", school='" + school + '\'' +
                '}';
    }
}

寫一個配置文件

<?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">
    <!--這里是根據構造函數內的順序往里面注入-->
    <bean id="s1" class="com.crush.pojo.Student">
       <constructor-arg index="0" value="12"/>
        <constructor-arg index="1" value="wyh"/>
        <constructor-arg index="2" value="北大"/>
    </bean>
    <!--這里是根據構造函數中的 類型來進行注入 -->
    <bean id="s2" class="com.crush.pojo.Student">
        <constructor-arg type="java.lang.Long" value="123"/>
        <constructor-arg type="java.lang.String" value="crush"/>
        <constructor-arg type="java.lang.String" value="浙江大學"/>
    </bean>
</beans>

3.測試

   @org.junit.Test
    public void testStudent(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        Student student = applicationContext.getBean("s2", Student.class);
        System.out.println(student);
    }

樣例2:

1.創(chuàng)建Teacher類

public class Teacher {
    private String name;
    private String school;
    private List<Student> studentList;
    private Map<String,String> map;
    private Set<String> set;
    public Teacher(String name, String school, List<Student> studentList, Map<String, String> map, Set<String> set) {
        this.name = name;
        this.school = school;
        this.studentList = studentList;
        this.map = map;
        this.set = set;
    }
    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", school='" + school + '\'' +
                ", studentList=" + studentList +
                ", map=" + map +
                ", set=" + set +
                '}';
    }
}public class Teacher {
    private String name;
    private String school;
    private List<Student> studentList;
    private Map<String,String> map;
    private Set<String> set;
    public Teacher(String name, String school, List<Student> studentList, Map<String, String> map, Set<String> set) {
        this.name = name;
        this.school = school;
        this.studentList = studentList;
        this.map = map;
        this.set = set;
    }
    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", school='" + school + '\'' +
                ", studentList=" + studentList +
                ", map=" + map +
                ", set=" + set +
                '}';
    }
}

2.beans.xml

<bean id="teacher" class="com.crush.pojo.Teacher">
    <constructor-arg index="0" value="xxx"/>
    <constructor-arg index="1" value="北京大學"/>
    <constructor-arg index="2" >
        <list>
            <ref bean="s1"/>
            <ref bean="s2"/>
        </list>
    </constructor-arg>
    <constructor-arg index="3">
        <map>
            <entry key="k1" value="xiaowang"/>
        </map>
    </constructor-arg>
    <constructor-arg index="4">
        <set>
            <value>1</value>
            <value>2</value>
        </set>
    </constructor-arg>
</bean>

3.測試

    @org.junit.Test
    public void testTeacher(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        Teacher teacher  = applicationContext.getBean("teacher", Teacher.class);
        System.out.println(teacher);
    }

Spring單例模式和原型模式

一、單例模式

Spring默認是單例模式的。

以Student的那個樣例1 為例。 scope=“singleton” 加上這么一個設置 當然默認也是它。

bean id="s1" class="com.crush.pojo.Student" scope="singleton">
    <constructor-arg index="0" value="12"/>
    <constructor-arg index="1" value="wyh"/>
    <constructor-arg index="2" value="北大"/>
</bean>

這個時候我們來進行測試

    @org.junit.Test
    public void testStudent(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        Student student1 = applicationContext.getBean("s1", Student.class);
        Student student2 = applicationContext.getBean("s1", Student.class);
        // 并且如果我們對其中一個做了修改 ,其余也會跟著一起被修改
        // 可以看到我們只修改了一個
        student1.setSchool("夢中的學校");
        System.out.println(student1);
        System.out.println(student2);
        System.out.println(student1==student2);
    }

在這里插入圖片描述

二、原型模式

我們還是以**Student來做例子講解 ** 注意:我們把原來設置改成了 scope=“prototype” 也就是原型模式

<!--這里是根據構造函數中的 類型來進行注入 -->
<bean id="s2" class="com.crush.pojo.Student" scope="prototype">
    <constructor-arg type="java.lang.Long" value="123"/>
    <constructor-arg type="java.lang.String" value="crush"/>
    <constructor-arg type="java.lang.String" value="浙江大學"/>
</bean>

接著測試

    @org.junit.Test
    public void testStudent(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        Student student1 = applicationContext.getBean("s2", Student.class);
        Student student2 = applicationContext.getBean("s2", Student.class);
        // 并且如果我們對其中一個做了修改 ,其余也會跟著一起被修改
        // 可以看到我們只修改了一個
        student1.setSchool("夢中的學校");
        System.out.println(student1);
        System.out.println(student2);
        System.out.println(student1==student2);
    }

在這里插入圖片描述

思考 為什么需要依賴注入

為什么我們以前用一個對象 new一下就好了,但用了Spring 之后,反而還需要寫

這樣一段代碼再去獲取勒?明明感覺更麻煩啦丫?用這個又有什么樣的好處呢?

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Student student1 = applicationContext.getBean("s2", Student.class);

總結

本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關注腳本之家的更多內容!

相關文章

  • 淺談MyBatisPlus中LocalDateTime引發(fā)的一些問題和解決辦法

    淺談MyBatisPlus中LocalDateTime引發(fā)的一些問題和解決辦法

    MyBatisPlus進行數據庫操作時,我們經常會遇到處理日期時間類型的需求,本文主要介紹了淺談MyBatisPlus中LocalDateTime引發(fā)的一些問題和解決辦法,具有一定的參考價值,感興趣的可以了解一下
    2024-07-07
  • 最有價值的50道java面試題 適用于準入職Java程序員

    最有價值的50道java面試題 適用于準入職Java程序員

    這篇文章主要為大家分享了最有價值的50道java面試題,涵蓋內容全面,包括數據結構和算法相關的題目、經典面試編程題等,對hashCode方法的設計、垃圾收集的堆和代進行剖析,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Spring實戰(zhàn)之抽象Bean和子Bean定義與用法示例

    Spring實戰(zhàn)之抽象Bean和子Bean定義與用法示例

    這篇文章主要介紹了Spring實戰(zhàn)之抽象Bean和子Bean定義與用法,結合實例形式分析了Spring抽象Bean和子Bean相關配置、定義與使用操作技巧,需要的朋友可以參考下
    2019-11-11
  • 圖文詳解Java中class的初始化順序

    圖文詳解Java中class的初始化順序

    網上有很多關于Java中class的初始化順序文章,但是本文通過圖文更加詳細的介紹了Java中class的初始化順序,并對class的裝載順序進行了講解,下面一起來看看。
    2016-08-08
  • 關于bigDecimal類的精度保留方法

    關于bigDecimal類的精度保留方法

    這篇文章主要介紹了關于bigDecimal類的精度保留方法,計算機存儲的浮點數受存儲bit位數影響,只能保證一定范圍內精準,超過bit范圍的只能取近似值,Java使用java.math.BigDecimal專門處理小數精度,需要的朋友可以參考下
    2023-07-07
  • Java使用utf8格式保存文本文件的方法

    Java使用utf8格式保存文本文件的方法

    這篇文章主要介紹了Java使用utf8格式保存文本文件的方法,涉及Java針對字符流編碼操作的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • SpringBoot3集成Thymeleaf的過程詳解

    SpringBoot3集成Thymeleaf的過程詳解

    在現(xiàn)代的Web開發(fā)中,構建靈活、動態(tài)的用戶界面是至關重要的,Spring Boot和Thymeleaf的結合為開發(fā)者提供了一種簡單而強大的方式來創(chuàng)建動態(tài)的Web應用,本文將介紹如何在Spring Boot項目中集成Thymeleaf,并展示一些基本的使用方法,需要的朋友可以參考下
    2024-01-01
  • Java對象數組定義與用法詳解

    Java對象數組定義與用法詳解

    這篇文章主要介紹了Java對象數組定義與用法,結合實例形式分析了java對象數組的概念、功能、定義與使用方法,需要的朋友可以參考下
    2019-08-08
  • java中計算集合的交差并集示例代碼

    java中計算集合的交差并集示例代碼

    今天突然想Java如何計算集合的交差并集,主要是看Python語言的時候想起來的。下面這篇文章主要給大家介紹了關于java中計算集合的交差并集的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-08-08
  • linux下java的安裝全過程

    linux下java的安裝全過程

    在Linux系統(tǒng)下安裝Java需要先檢查是否已有Java版本,如果有則先卸載,之后可以從華為云官網下載Java JDK的tar包,解壓到/usr/local目錄,并配置環(huán)境變量,最后通過命令行測試Java是否安裝成功
    2024-09-09

最新評論