Spring----Set方式注入

本文详细介绍了Spring框架中的依赖注入(DI)概念,通过一个Student类的实例展示了包括普通值注入、bean注入、数组、List、Map、Set、null值以及Properties注入在内的多种注入方式。在提供的XML配置文件中,展示了如何为Student对象的各个属性设置值,包括地址、书籍、爱好、证件信息等。依赖注入使得对象间的依赖关系由容器管理,提高了代码的灵活性和可测试性。
摘要由CSDN通过智能技术生成

本文讲解了Set方式常见的注入方式:

1. 普通值注入:value

2.bean注入:ref

3.数组注入

4.List注入

5.Map注入

6.Set注入

7.null值注入

8.Properties注入:key=value


什么是依赖注入? 

依赖注入
  ·依赖:bean对象的创建依赖于容器!
  ·注入:bean对象中的所有属性,由容器来注入

学生类 ↓ 

package cn.di.spring;

import java.util.*;

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobby;
    private Map<String,String> card;
    private Set<String> games;
    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> getHobby() {
        return hobby;
    }

    public void setHobby(List<String> hobby) {
        this.hobby = hobby;
    }

    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 Properties getInfo() {
        return info;
    }

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

    public String getWife() {
        return wife;
    }

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

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

 地址类↓

package cn.di.spring;

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

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="cn.di.spring.Address">
        <property name="address" value="中国"/>
    </bean>

    <bean id="student" class="cn.di.spring.Student">
        <!--第1种:普通值注入:value-->
        <property name="name" value="张三"/>
        <!--第2种:bean注入:ref-->
        <property name="address" ref="address"/>
        <!--第3种:数组注入:-->
        <property name="books">
            <array>
                <value>西游记</value>
                <value>水浒传</value>
                <value>红楼梦</value>
            </array>
        </property>
        <!--第4种:list注入:-->
        <property name="hobby">
            <list>
               <value>打球</value>
               <value>唱歌</value>
               <value>代码</value>
            </list>
        </property>
        <!--第5种:map注入:-->
        <property name="card">
            <map>
               <entry key="身份证" value="000545"/>
               <entry key="银行卡" value="111454"/>
            </map>
        </property>
        <!--第6种:Set注入:-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>COC</value>
                <value>BOB</value>
            </set>
        </property>
        <!--第7种:null值注入:-->
        <property name="wife">
            <null/>
        </property>
        <!--第8种:Properties注入:key=value -->
        <property name="info">
          <props>
              <prop key="学号">193102008</prop>
              <prop key="性别">男</prop>
              <prop key="姓名">张三</prop>
          </props>
        </property>



    </bean>
</beans>

测试类 ↓ 

import cn.di.spring.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 s = (Student) context.getBean("student");
        System.out.println(s.toString());
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值