Sping-依賴注入

前进的蜗小牛發表於2024-04-26

6、依賴注入

6.1 構造器注入(參考第三節)

6.2 Set注入

  • 依賴注入 :set注入
  • 依賴注入
    • 依賴 :bean物件的建立依賴於容器
    • 注入 :bean物件中的所有屬性 由容器注入
    • 編寫實體類
//實體類一
package pojo;

public class Address {
    private String address;

    public Address(){

    }
    public Address(String address) {
        this.address = address;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }
}
//實體類二
package pojo;

import java.util.*;

public class Student {
    private Address address;
    private String name ;
    private String[] books;
    private List<String> hobby;
    private Set<String> games;
    private Map<String,Integer > score;
    private Properties info;

    /*建構函式*/
    public Student(){

    }
    public Student(Address address, String name, String[] books, List<String> hobby, Set<String> games, Map<String, Integer> score, Properties info) {
        this.address = address;
        this.name = name;
        this.books = books;
        this.hobby = hobby;
        this.games = games;
        this.score = score;
        this.info = info;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List<String> getHobby() {
        return hobby;
    }

    public void setHobby(List<String> hobby) {
        this.hobby = hobby;
    }

    public Set<String> getGames() {
        return games;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

    public Map<String, Integer> getScore() {
        return score;
    }

    public void setScore(Map<String, Integer> score) {
        this.score = score;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "Student{" +
                "address=" + address +
                ", name='" + name + '\'' +
                ", books=" + Arrays.toString(books) +
                ", hobby=" + hobby +
                ", games=" + games +
                ", score=" + score +
                ", info=" + info +
                '}';
    }
}

  • 編寫beans.xml檔案 注意一下map和props
<?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-->
    <bean id="address" class="pojo.Address">
        <property name="address" value="小李"/>
    </bean>
    <bean id="student" class="pojo.Student">
      <property name="address" ref="address"/>
        <property name="name" value="12"/>
        <property name="books">
            <array>
                <value>三國</value>
                <value>三國</value>
                <value>三國</value>
            </array>
        </property>
        <property name="games">
            <list>
                <value>112</value>
                <value>112</value>
            </list>
        </property>
        <property name="hobby">
            <list>
                <value>1</value>
                <value>2</value>
            </list>
        </property>
        <property name="score">
            <map>
                <entry key="語文" value="90"/>
                <entry key="數學" value="90"/>
            </map>
        </property>
        <property name="info">
            <props>
                <prop key="1">2</prop>
            </props>
        </property>
    </bean>
</beans>
  • 測試
package pojo;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test(){
        /*獲取容器*/
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        /*從容器裡面取出物件*/
        Student student = context.getBean("student", Student.class);
        /*輸出*/
        System.out.println(student);
    }
}

6.3 擴充

  • p名稱空間注入 set注入
  • 第一步 編寫實體類
package pojo;

public class User {
    private String name ;
    private String password;
    
    public User(){
        
    }
    public User(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

  • 第二步 編寫xml配置檔案
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
        <!--需要匯入約束-->
        <!--註冊bean-->
        <bean id="user" class="pojo.User" p:name="lsl" p:password="123456"/>
        </beans>
  • 測試
package pojo;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {

    @Test
    public void test1(){
       ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
         User user = context.getBean("user", User.class);
        System.out.println(user);
    }
}

  • c名稱空間注入 構造器注入 程式碼與上面的p名稱空間注入相似

  • 總結 :這兩種方式都不能直接使用 需要匯入約束

6.4 bean的作用域

6.4.1 Singleton Scope
  • 只有一個單例 Bean 的共享例項被管理,所有對具有符合該Bean定義的ID的Bean的請求都會被Spring容器返回該特定的Bean例項
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--註冊bean-->
    <bean id="use" class="pojo.User" c:_0="1" c:_1="2" scope="singleton"/>
</beans>
6.4.2 Prototype Scope
  • Bean 部署的非 singleton prototype scope 導致每次對該特定Bean的請求都會建立一個新的Bean例項。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--註冊bean-->
    <bean id="use" class="pojo.User" c:_0="1" c:_1="2" scope="prototype"/>
</beans>

相關文章