DI依赖注入-Spring学习05

17 篇文章 0 订阅
6.DI依赖注入

6.1构造器注入

6.2set方式注入【重点】

.依赖注入:

。依赖:bean对象的创建依赖于set注入容器。

。注入:bean对象中所有属性,由容器来注入。

【环境搭建】

1.复杂类型

package com.kuang.pojo;

public class Address {
    private String address;
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }

}

2.真实测试对象

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;
     public String getName() {
        return name;
    }

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

3.测试类

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

4.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="student" class="com.kuang.pojo.Student">
        <!--第一种 普通值注入 直接使用value-->
        <property name="name" value="山"></property>
    </bean>
</beans>

完善注入信息

<?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.kuang.pojo.Address">
        <property name="address" value="北京"></property>
    </bean>
    <bean id="student" class="com.kuang.pojo.Student">
        <!--第一种 普通值注入 直接使用value-->
        <property name="name" value="张三"></property>
        <!-- 第二种  bean注入  是一个引用  用ref-->
        <property name="address" ref="address"></property>

        <!--数组注入-->
        <property name="books">
            <array>
                <value>西游记</value>
                <value>水浒传</value>
                <value>三国演义</value>
                <value>红楼梦</value>
            </array>
        </property>
        <!--集合注入-->
        <property name="hobbys">
            <list>
                <value>打球</value>
                <value>敲代码</value>
            </list>
            <!--map注入-->
        </property>
        <property name="card">
            <map>
                <entry key="身份证" value="1234567890098"></entry>
            </map>
        </property>
        <!--set注入-->
        <property name="games">
            <set>
                <value>王者</value>
                <value>吃鸡</value>
            </set>
        </property>
        <!--null注入-->
        <property name="wife">
            <null></null>
        </property>
        <!--Properties-->
        <property name="info">
            <props>
                <prop key="学号">183206102</prop>
                <prop key="性别"></prop>
            </props>
        </property>
    </bean>
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值