依赖注入(DI)、p命名空间、c命名空间

305 阅读2分钟

依赖注入(Dependency Injection,DI)

  1. 依赖 : 指Bean对象的创建依赖于容器,Bean对象的依赖资源。
  2. 注入 : 指Bean对象所依赖的资源,由容器来设置和装配。

注入方式

  1. 构造器注入,可以看之前的Blog IOC创建对象方式和Spring配置
  2. Set方式注入:要求被注入的属性 , 必须有set方法

举个例子来看,我们创建两个实体类

Student:

package pojo;

import java.util.*;

public class Student {
    private String name;                  //基本类型注入
    private Address address;             //bean注入
    private String[] books;              //数组注入
    private List<String> hobbies;        //List注入
    private Map<String, String> card;    //Map注入
    private Set<String> games;           //Set注入
    private String wife;                 //null值注入
    private Properties info;             //Properties注入
    private String wifi;                 //空值注入


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

    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;
    }

    public String getWifi() {
        return wifi;
    }

    public void setWifi(String wifi) {
        this.wifi = wifi;
    }
}

Address:

package pojo;

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

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

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

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="pojo.Address">
        <property name="address" value="福州"/>
    </bean>

    <bean id="student" class="pojo.Student">

        <!--  基本类型注入    -->
        <property name="name" value="小米"/>

        <!--  bean注入    -->
        <property name="address" ref="address"/>

        <!--  数组注入    -->
        <property name="books">
            <array>
                <value>java</value>
                <value>php</value>
                <value>python</value>
            </array>
        </property>

        <!--  List注入    -->
        <property name="hobbies">
            <list>
                <value>code</value>
                <value>write</value>
                <value>music</value>
                <value>eat</value>
            </list>
        </property>

        <!--  Map注入  使用了entry,不一样!  -->
        <property name="card">
            <map>
                <entry key="学号" value="123456"/>
                <entry key="姓名" value="小米"/>
            </map>
        </property>

        <!--  Set注入    -->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>CSGO</value>
                <value>CF</value>
            </set>
        </property>

        <!--  null注入    -->
        <property name="wife">
            <null/>
        </property>

        <!--  空值注入      -->
        <property name="wifi" value=""/>

        <!--
            null表示对象的内容为空,即对象的内容是空白的。 ->没有wife,设置过的。
            空值表示对象的内容无法确定   ->有wifi,但是不知道是什么,没有设置。
         -->

        <!--  properties注入    -->
        <property name="info">
            <props>
                <prop key="学校">FAFU</prop>
                <prop key="学院">计信院</prop>
                <prop key="专业">软件工程</prop>
            </props>
        </property>


    </bean>

</beans>

测试类:

    @Test
    public void MyTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        Student student = (Student) context.getBean("student");

        System.out.println(student.toString());

    }

测试结果:

image.png

注意事项:

  1. Set注入和properties注入和其他注入有些区别,注意区别。
  2. null和空值的区别:
    • null表示对象的内容为空,即对象的内容是空白的。 ->没有wife,设置过的。
    • 空值表示对象的内容无法确定 ->有wifi,但是不知道是什么,没有设置。

p命名空间注入和c命名空间注入

  1. p命名空间注入:相当于properties注入,实体类需要无参构造 如果你要使用p命名空间,你需要引入p命名空间的约束文件:xmlns:p="www.springframework.org/schema/p"
<bean id="teacher" class="pojo.Teacher" p:name="小花" p:id="123456"/>
  1. c命名空间注入:相当于构造器注入,实体类需要有参构造 如果你要使用c命名空间,你需要引入c命名空间的约束文件:xmlns:c="www.springframework.org/schema/c"
<bean id="teacher1" class="pojo.Teacher" c:id="654321" c:name="小明"/>