DI依赖注入

1.构造器注入:已经了解过

2.Set方式注入:
依赖注入:Set注入
依赖:bean对象的创建依赖于容器
注入:bean对象中的所有属性,由容器来注入

环境搭建:
复杂类型

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

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

真实测试对象

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

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.dai.pojo.Student">
        <property name="name" value=""/>
    </bean>

</beans>

测试类:

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

    }
}

完善注入信息:

  <bean id="address" class="com.dai.pojo.Address"/>

    <bean id="student" class="com.dai.pojo.Student">
        <property name="name" value=""/>

<!--        第二种,Bean注入,ref-->
        <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>

<!--        Map注入-->
        <property name="card">
            <map>
                <entry key="身份证" value="1111"/>
                <entry key="银行卡" value="2222"/>
                <entry key="公交卡" value="3333"/>
            </map>
        </property>

<!--        set注入-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>CS</value>
            </set>
        </property>

<!--        null-->
        <property name="wife">
            <null></null>
        </property>

<!--        Properties-->
        <property name="info">
            <props>
                <prop key="学号">11</prop>
                <prop key="性别"></prop>
            </props>
        </property>

    </bean>

bean的作用域:
1.单例模式(Spring默认机制)

    <bean id="student" class="com.dai.pojo.Student" 
scope="singleton">

2.原型模式:每次从容器中get的时候,都会产生一个新对象

<bean id="student" class="com.dai.pojo.Student"
 scope="prototype">

3.其余的request、session、application这些只能在web开发中使用到

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值