Spring笔记:依赖注入 山上下了雪

依赖注入中“依赖”表示对象的创建依赖于Spring容器,“注入”表示对象中的属性值由Spring来进行注入,注入方式常用的有构造器注入和set方法注入两种方式,另外还可以使用命名空间进行注入。

1. 构造器注入

构造器注入方式就是使用构造器对属性进行注入赋值,即xml中使用 constructor-arg 标签对属性进行赋值,在 Spring笔记:Hello World 中有介绍和示例,这里就不多讲了。

2. set方法注入

set注入根据属性的类型不同,在xml中配置的方式也不同,详见下面的示例。

Address类:

package com.yun.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 + '\'' +
                '}';
    }
}

Student类:

package com.yun.pojo;

import java.util.*;

public class Student {
    private String name;
    private Address address;
    private String[] courses;
    private List<String> roommates;
    private Set<String> cards;
    private Map<String, String> courseTeacher;
    private String nullStr;
    private Properties props;

    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[] getCourses() {
        return courses;
    }

    public void setCourses(String[] courses) {
        this.courses = courses;
    }

    public List<String> getRoommates() {
        return roommates;
    }

    public void setRoommates(List<String> roommates) {
        this.roommates = roommates;
    }

    public Set<String> getCards() {
        return cards;
    }

    public void setCards(Set<String> cards) {
        this.cards = cards;
    }

    public Map<String, String> getCourseTeacher() {
        return courseTeacher;
    }

    public void setCourseTeacher(Map<String, String> courseTeacher) {
        this.courseTeacher = courseTeacher;
    }

    public String getNullStr() {
        return nullStr;
    }

    public void setNullStr(String nullStr) {
        this.nullStr = nullStr;
    }

    public Properties getProps() {
        return props;
    }

    public void setProps(Properties props) {
        this.props = props;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address.toString() +
                ", courses=" + Arrays.toString(courses) +
                ", roommates=" + roommates +
                ", cards=" + cards +
                ", courseTeacher=" + courseTeacher +
                ", nullStr='" + nullStr + '\'' +
                ", pro=" + props +
                '}';
    }
}

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.yun.pojo.Address">
        <property name="address" value="成都"/>
    </bean>
    <!-- set方式注入:给属性注入赋值 -->
    <bean id="student" class="com.yun.pojo.Student">
        <!-- 普通属性注入 -->
        <property name="name" value="zhangsan"/>
        <!-- bean注入,ref属性值是另一个bean的id值 -->
        <property name="address" ref="address"/>
        <!-- 数组类型注入 -->
        <property name="courses">
            <array>
                <value>语文</value>
                <value>数学</value>
                <value>英语</value>
            </array>
        </property>
        <!-- List类型注入 -->
        <property name="roommates">
            <list>
                <value>李四</value>
                <value>王五</value>
                <value>赵六</value>
            </list>
        </property>
        <!-- Set类型注入 -->
        <property name="cards">
            <set>
                <value>111</value>
                <value>222</value>
                <value>333</value>
            </set>
        </property>
        <!-- Map类型注入 -->
        <property name="courseTeacher">
            <map>
                <entry key="语文" value="李老师"/>
                <entry key="数学" value="王老师"/>
                <entry key="英语" value="赵老师"/>
            </map>
        </property>
        <!-- null类型注入,注意,如果直接使用value=""的方式,最终的值是空串,而不是null -->
        <property name="nullStr">
            <null/>
        </property>
        <!-- Properties类型注入 -->
        <property name="props">
            <props>
                <prop key="age">18</prop>
                <prop key="gender">男</prop>
                <prop key="email">zhangsan@qq.com</prop>
            </props>
        </property>
    </bean>
</beans>

测试类:

import com.yun.pojo.Student;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

3. 命名空间注入:p命名空间和c命名空间

p命名空间

其实对应的就是set方法注入,p就是property标签,看它给对象属性赋值的方式就知道了。使用p命名空间注入需要在xml文件中添加扩展 xmlns:p="http://www.springframework.org/schema/p" ,然后就可以直接在 beans 标签中使用 p: 方式对属性进行赋值注入了。

User类:

package com.yun.pojo;

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

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">
    <!-- p:后面除了直接跟属性名以外,如果name是一个对象类型的话,还可以使用p:name-ref指定其他的bean,
     其他用法可以参考set方法的注入方式,这些用法除了在IDEA中可以自动提示外,也可以去官网搜索一下-->
    <bean id="user" class="com.yun.pojo.User" p:name="zhangsan" p:age="18"/>
</beans>

c命名空间

c命名空间对应的就是构造器注入,c对应的就是constructor-args标签,使用c命名空间也是需要在xml配置中添加一个额外的扩展 xmlns:c="http://www.springframework.org/schema/c" ,然后使用 c: 的方式给对象属性进行赋值注入。

User类:必须要有无参和有参构造器

package com.yun.pojo;

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

    public User() {
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

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

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

    <!-- c命名空间注入,使用c:的方式进行属性注入赋值,其他用法可以参考构造器注入方式 -->
    <bean id="user" class="com.yun.pojo.User" c:name="zhangsan" c:age="18"/>
</beans>
原文地址:https://www.cnblogs.com/guyuyun/p/15631469.html