Spring框架(四)——IOC的注入方式

1.構造器注入

applicationContext.xml中的配置文件爲:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">
    <bean name="user2" class="com.zj.pojo.User">
        <constructor-arg name="name" value="哈哈"/>
    </bean>-->
</beans>

2、Set方式注入 【重點】

  • 依賴注入:Set注入!
    • 依賴:bean對象的創建依賴於容器!
    • 注入: bean對象中的所有屬性,由容器來注入!

【環境搭建】

  1. 複雜類型
package com.zj.pojo;

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
  1. 真實測試對象
public class Student {
    private String name;
    private Address address;
    private Dog dog;
    
    public Student() {
    }

    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 Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address.getAddress() +
                ", dog=" + dog.getName() +
                '}';
    }
}

3.applicationContext.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

   <bean id="user" class="com.zj.pojo.User">
     <property name="name" value="哈哈"/>

</beans>
  1. 測試類

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

4.完善注入信息

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="addr" class="com.zj.pojo.Address">
    <property name="address" value="秦皇島"/>
</bean>
    <bean id="student" class="com.zj.pojo.Student" scope="prototype">
       <property name="name" value="張三"/>
        <property name="address" ref="addr"/>
        <property name="books">
            <array>
                <value>西遊記</value>
                <value>水滸傳</value>
                <value>紅樓夢</value>
            </array>
        </property>
        <property name="list">
            <list>
                <value>list1</value>

                <value>list2</value>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="k1" value="v1"/>
                <entry key="k2" value="v2"/>
            </map>
        </property>
        <property name="set">
            <set>
                <value>s1</value>
                <value>s2</value>
                <value>s3</value>
            </set>
        </property>
        <property name="wife">
            <null/>
        </property>
        <property name="info">
            <props>
                <prop key="id">1</prop>
                <prop key="gender"></prop>
            </props>
        </property>
    </bean>
</beans>

在這裏插入圖片描述

3、拓展方式注入

我們可以使用 p命令空間和c命令空間進行注入
使用!

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

    <!--p命名空間注入,可以直接注入屬性的值:property-->
    <bean id="user" class="com.kuang.zj.User" p:name="haha" p:age="18"/>

    <!--c命名空間注入,通過構造器注入:construct-args-->
    <bean id="user2" class="com.zj.pojo.User" c:age="18" c:name="haha2"/>

</beans>

測試:

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

注意:p命名和c命名空間不能直接使用,需要導入xml約束!

xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章