6、DI依赖注入

6.1、构造器注入

我们要使用有参构造创建对象

  1. 下标赋值

    <!--    下标赋值-->
        <bean id="user" class="com.xiao.pojo.User">
            <constructor-arg index="0" value="彩虹java"/>
        </bean>
    
  2. 类型

    <!--    第二种方式,通过类型创建,不建议使用-->
        <bean id="user" class="com.xiao.pojo.User">
            <constructor-arg type="java.lang.String" value="caihong"/>
        </bean>
    
  3. 参数名

    <!--直接通过参数名来设置-->
        <bean id="user" class="com.xiao.pojo.User">
            <constructor-arg name="name" value="caihong"/>
        </bean>
    

6.2、通过set方式注入

  • 依赖注入:set注入

    • 依赖:bean对象的创建依赖于容器

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

【环境搭建】

  1. 复杂类型

    package com.xiao.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;
    }
    
  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
               http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="student" class="com.xiao.pojo.Student">
            <!--第一种,不同的注入,value-->
            <property name="name" value="彩虹"/>
        </bean>
    </beans>
    
  4. 测试类

    public class MyTest {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            Student student = context.getBean("student", Student.class);
            System.out.println(student.getName());
        }
    }
    

完善注入信息

<?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.xiao.pojo.Address">
        <property name="address" value="西安"/>
    </bean>
    <bean id="student" class="com.xiao.pojo.Student">
        <!--第一种,不同的注入,value-->
        <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>

        <property name="hobbys">
            <list>
                <value>唱歌</value>
                <value>跳舞</value>
            </list>
        </property>
        <property name="card">
            <map>
                <entry key="身份证" value="123123"/>
                <entry key="银行卡" value="232323"/>
            </map>
        </property>
        <property name="games">
            <set>
                <value>LOL</value>
                <value>COC</value>
            </set>
        </property>
        <property name="wife">
            <null/>
        </property>
        <property name="info">
            <props>
                <prop key="学号">123123</prop>
            </props>
        </property>
    </bean>

</beans>

6.3、扩展方式

我们可以使用p命名空间和c命名空间进行注入

<!--p命名空间注入,可以直接注入属性的值:property-->
<bean id="user" class="com.xiao.pojo.User" p:name="彩虹" p:age="12"/>
<!--p命名空间注入,通过构造器注入:construct-arges-->
<bean id="user2" class="com.xiao.pojo.User" c:age="12" c:name="彩虹"/>
</beans>

测试:

    <!--p命名空间注入,可以直接注入属性的值:property-->
    <bean id="user" class="com.xiao.pojo.User" p:name="彩虹" p:age="12"/>
    <!--p命名空间注入,通过构造器注入:construct-arges-->
    <bean id="user2" class="com.xiao.pojo.User" c:age="12" c:name="彩虹"/>
</beans>

注意点:p命名和c命名空间不能直接使用需要导入xml约束

xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"

6.4、bean的作用域

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Xj6Vfh3d-1628176142080)(/Users/yanxiao/Library/Application Support/typora-user-images/image-20210805221741983.png)]

  1. 代理模式(spring默认机制)

    <bean id="user2" class="com.xiao.pojo.User" c:age="12" c:name="彩虹" scope="singleton"/>
    
  2. 原型模式:每次从容器中get的时候,都会产生一个新对象

    <bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>
    
  3. 其余的request、session、application,这些只能在web开发中使用到!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

期待aaaa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值