06-DI依赖注入环境

DI依赖注入环境

控制反转loC(Inversion of Control),是一种设计思想,DI(依赖注入)是实现loC的一种方法,也有人认为DI只是loC的另一种说法。

构造器注入

  • 前面已经说过了

Set方式注入【重点】

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

【环境搭建】

1.复杂类型

package com.tian.pojo;

public class Address {
    private String address;

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

    public String getAddress() {
        return 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 wif;
    private Properties info;
}

3.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--第一种 普通值注入 -->
    <bean id="student" class="com.tian.pojo.Student">
        <property name="name" value="张三"/>
    </bean>
</beans>

4.测试类

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.getAddress());//null
    }
}

完善注入信息

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.tian.pojo.Address">
    <property name="address" value="北京"></property>
</bean>

    <bean id="student" class="com.tian.pojo.Student">
        <!--第一种 普通值注入 -->
        <property name="name" value="张三"/>
        <!--  第二种注入 bean注入 ref -->
        <property name="address" ref="address"></property>

        <!--   数组注入: ref-->
        <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>
        <!--   Map-->
        <property name="card">
            <map>
                <entry key="身份证" value="12345678931"/>
                <entry key="银行卡" value="12364897545"/>
            </map>
        </property>
        <!--   Set-->
        <property name="games">
            <set>
                <value>王者</value>
                <value>吃鸡</value>
                <value>飞车</value>
            </set>
        </property>
        <!--   null-->
        <property name="wif">
            <null/>
        </property>
        <!--   properties-->
        <property name="info">
            <props>
                <prop key="学号">123</prop>
                <prop key="姓名">张三</prop>
            </props>
        </property>
    </bean>


</beans>

测试

import com.tian.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.getAddress());//null
        System.out.println(student.toString());
        /*
        * Student
        * {name='张三',
        * address=Address{address='北京'},
        * books=[红楼梦, 三国演义, 水浒传, 西游记],
        *  hobbys=[吃饭, 睡觉, 打豆豆],
        * card={身份证=12345678931, 银行卡=12364897545},
        * games=[王者, 吃鸡, 飞车],
        * wif='null',
        * info={学号=123, 姓名=张三}}
         */
    }
}

扩展方式注入

  • c命名
  • p命名
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值