Spring使用Setter完成依赖注入

2023-04-08 12:09:52

对依赖注入的理解

依赖:实体间的所有依赖由容器创建
注入:容器负责完成实体间依赖互相注入的任务

使用Setter完成不同类型属性的注入

实体类Student

package indi.stitch.pojo;

import java.util.*;

public class Student {

    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Set<String> games;
    private Map<String, String> card;
    private Properties info;
    private String wife;

    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> getHobbys() {
        return hobbys;
    }

    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }

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

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

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

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

    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 + '\'' + "\n" +
                ", address=" + address.toString() + "\n" +
                ", books=" + Arrays.toString(books) + "\n" +
                ", hobbys=" + hobbys + "\n" +
                ", games=" + games + "\n" +
                ", card=" + card + "\n" +
                ", info=" + info + "\n" +
                ", wife='" + wife + '\'' +
                '}';
    }
}

实体类引用的复杂类型Address

package indi.stitch.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 + '\'' +
                '}';
    }
}
  • String字符串类型注入
<property name="name" value = "stitch" />
  • 复杂VO类型注入

配置文件中增加复杂类型bean(Address)的依赖配置

<bean id = "address" class="indi.stitch.pojo.Address">
        <property name="address" value="北京" />
    </bean>
<bean id = "student" class = "indi.stitch.pojo.Student">

实体类Student的bean属性依赖对其进行引用

<property name="address" ref="address"/>
  • 数组类型注入
<property name="books">
            <array>
                <value>西游记</value>
                <value>三国演义</value>
                <value>红楼梦</value>
                <value>水浒传</value>
            </array>
</property>
  • List集合类型注入
<property name="hobbys">
            <list>
                <value>唱歌</value>
                <value>跳舞</value>
                <value>打篮球</value>
            </list>
</property>
  • Set集合类型注入
 <property name="games">
            <set>
                <value>英雄联盟</value>
                <value>穿越火线</value>
                <value>刺激战场</value>
            </set>
</property>
  • Map键值对类型注入
<property name="card">
            <map>
                <entry key="学生卡" value="123456"/>
                <entry key="身份证" value="111111222222223333" />
            </map>
</property>
  • Properties类型注入
<property name="info">
            <props>
                <prop key="sex"></prop>
                <prop key="age">18</prop>
            </props>
</property>
  • null类型注入
<property name="wife">
            <null />
        </property>

整体配置文件

<?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="indi.stitch.pojo.Address">
        <property name="address" value="北京" />
    </bean>
    <bean id = "student" class = "indi.stitch.pojo.Student">

        <!-- String字符串类型注入-->
        <property name="name" value = "stitch" />

        <!--复杂VO类型注入-->
        <property name="address" ref="address"/>

        <!--数组类型注入-->
        <property name="books">
            <array>
                <value>西游记</value>
                <value>三国演义</value>
                <value>红楼梦</value>
                <value>水浒传</value>
            </array>
        </property>

        <!--List集合类型注入-->
        <property name="hobbys">
            <list>
                <value>唱歌</value>
                <value>跳舞</value>
                <value>打篮球</value>
            </list>
        </property>

        <!--Set集合类型注入-->
        <property name="games">
            <set>
                <value>英雄联盟</value>
                <value>穿越火线</value>
                <value>刺激战场</value>
            </set>
        </property>

        <!--Map键值对类型注入-->
        <property name="card">
            <map>
                <entry key="学生卡" value="123456"/>
                <entry key="身份证" value="111111222222223333" />
            </map>
        </property>

        <!--Properties类型注入-->
        <property name="info">
            <props>
                <prop key="sex"></prop>
                <prop key="age">18</prop>
            </props>
        </property>

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

    </bean>
</beans>

测试类

import indi.stitch.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

输出结果:
在这里插入图片描述

  • 作者:想飞的鱼Stitch
  • 原文链接:https://blog.csdn.net/qq_39209361/article/details/114047114
    更新时间:2023-04-08 12:09:52