Spring-- DI 依赖注入(可直接赋值运行,有效)

Spring-- DI 依赖注入(可直接赋值运行,有效)

依赖注入DI 通俗的来讲就是将属性值注入到spring的配置文件里面去,交给spring,要用直接取就行

1、实体类

地址类,作为 学生类 的属性注入

package springdi1;

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

学生类,实体类

package springdi1;

import java.util.*;

public class Student {
    private String name;
    private Address address;
    private String[] bokks;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private Properties info;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddress() {
        return address;
    }

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

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

    public String[] getBokks() {
        return bokks;
    }

    public void setBokks(String[] bokks) {
        this.bokks = bokks;
    }

    public List<String> getHobbys() {
        return hobbys;
    }

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

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

2、测试类

package springdi1;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestDi {
    @Test
    public void Test(){
        ApplicationContext context=new ClassPathXmlApplicationContext("springdi1.xml");
        Student student=(Student) context.getBean("student1");
        System.out.println(student.toString());
    }
}

3、spring 配置文件–核心部分

<?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="student1" class="springdi1.Student">
         <property name="name" value="小明"/>
        <!--对象注入-->
        <property name="address" ref="address"/>
        <!--数组注入-->
        <property name="bokks">
            <array>
                <value>斗破苍穹</value>
                <value>斗罗大陆</value>
                <value>星辰变</value>
            </array>
        </property>
        <!--list集合注入-->
        <property name="hobbys">
            <list>
                <value>狗</value>
                <value>猪</value>
                <value>牛</value>
            </list>
        </property>
        <!--map 集合注入-->
        <property name="card">
            <map>
                <entry key="学历" value="本科"></entry>
                <entry key="专业" value="信息与计算科学"></entry>
            </map>
        </property>
        <!--set集合注入-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>DNF</value>
            </set>
        </property>
        <!--proper-->
        <property name="info">
            <props>
                <prop key="学号">12132</prop>
                <prop key="性别">男</prop>
            </props>
        </property>
    </bean>
    <bean id="address" class="springdi1.Address">
        <property name="address" value="重庆市"/>
    </bean>

</beans>```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值