Spring從入門到入土——依賴注入(DI)


相關文章

跟着官網學spring—快速入門指南

跟着官網學Spring—構建RESTful Web服務

Spring從入門到入土——概述以及IOC理論推導

Spring從入門到入土——快速上手Spring

Spring從入門到入土——依賴注入(DI)

Spring從入門到入土——Bean的作用域

Dependency Injection

概念

  • 依賴注入(DI)
  • 依賴:指Bean對象的創建依賴於容器。Bean對象的依賴資源
  • 注入:指Bean對象

注入方式

一共有三種:分別是構造器注入;Set注入;P命名和C命名注入

構造器注入

Spring從入門到入土——快速上手Spring中Beans.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

<!--   第一種根據參數名字來設置-->
    <bean id="userT" class="com.zhonghu.pojo.UserT">
<!--        name 指參數名-->
        <constructor-arg name="name"value="zhonghu"/>
        
    </bean>
    
    
<!--    第二種根骨index參數下標來設置-->
    <bean id="userT" class="com.zhonghu.pojo.UserT">
<!--     index值構造方法,下標從0開始   -->
        <constructor-arg index="0" value="zhonghu"/>
    </bean>

    
    
<!--   第三種 根據參數類型設置 十分不推薦-->
    <bean id="useT"class="com.zhonghu.pojo.UserT">
        <constructor-arg type="java.lang.String" value="zhonghu"/>
    </bean>
</beans>

Set注入

​ 要求被注入的屬性必須有set方法,set方法的方法名由set + 屬性首字母大寫 , 如果屬性是boolean類型 , 沒有set方法 , 是 is

測試pojo類:

Address.java

public class Address {
    private String Address;

    public String getAddress() {
        return Address;
    }

    public void setAddress(String address) {
        Address = address;
    }

    @Override
    public String toString() {
        return "Address{" +
                "Address='" + Address + '\'' +
                '}';
    }
}

Student.java

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbies;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;

    public String getName() {
        return name;
    }

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

    public Address getAddress() {
        return address;
    }

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

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

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

    public List<String> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    public Map<String, String> getCard() {
        return card;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

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

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

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo() {
        return info;
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address.toString() +
                ", books=" + Arrays.toString(books) +
                ", hobbies=" + hobbies +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}
Bean注入:

beans.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="address" class="com.zhonghu.pojo.Address">
        <property name="address" value="China"/>
    </bean>
    
    <bean id="student" class="com.zhonghu.pojo.Student">
        <!--        普通值注入-->
        <property name="name" value="冢狐"/>


        <!--        Bean注入-->
        <property name="address" ref="address"/>
        <!--       數組 -->
        <property name="books">
            <array>
                <value>紅樓夢</value>
                <value>三國演義</value>
                <value>水滸傳</value>
                <value>西遊記</value>
            </array>
        </property>
        <!--        list-->
        <property name="hobbies">
            <list>
                <value>打遊戲</value>
                <value>看電影</value>
                <value>碼代碼</value>
            </list>
        </property>
        <!--        map-->
        <property name="card">
            <map>
                <entry key="學號" value="123456789"/>
                <entry key="性別" value=""/>
                <entry key="姓名" value="冢狐"/>
            </map>
        </property>
        <!--        set-->
        <property name="games">
            <set>
                <value>CSGO</value>
                <value>COC</value>
            </set>
        </property>
        <!--        Null-->
        <property name="wife">
            <null/>
        </property>
        <!--        Properties-->
        <property name="info">
            <props>
                <prop key="url">https://blog.csdn.net/issunmingzhi</prop>
                <prop key="用戶名">root</prop>
                <prop key="密碼">123456</prop>
            </props>
        </property>
    </bean>

</beans>

測試:

public class MyTest {
    public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());
    }
}
結果:

p命名和c命名注入

user.java:【注意,這裏沒有有參構造器】

public class User {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
p命名空間注入:

​ 就是set注入、需要在頭文件中加入約束條件

 導入約束 : xmlns:p="http://www.springframework.org/schema/p"
 
 <!--P(屬性: properties)命名空間 , 屬性依然要設置set方法-->
 <bean id="user" class="com.zhonghu.pojo.User" p:name="冢狐" p:age="18"/>
c命名空間注入

​ 就是構造器注入、需要在頭文件下加入約束文件

 導入約束 : xmlns:c="http://www.springframework.org/schema/c"
 <!--C(構造: Constructor)命名空間 , 屬性依然要設置set方法-->
 <bean id="user" class="com.kuang.zhonghu.User" c:name="冢狐" c:age="18"/>

此時會發現出問題:因爲沒有寫有參構造函數

C命名空間注入就是所謂的構造器注入

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章