依赖注入

本文详细介绍了Spring框架中的依赖注入,包括构造器注入和set方式注入(主流)。通过实例展示了如何配置XML来注入不同类型的属性,如字符串、数组、列表、集合、Map和Properties等。同时,还探讨了p命名空间和c命名空间的使用,以简化XML配置,分别对应set注入和构造器注入。这些内容有助于理解Spring中Bean的装配和管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.构造器注入

2.set方式注入【主流】

依赖注入:set注入!(依赖是指:bean对象的创建依赖于容器
注入是指:bean对象中的所有属性,由容器来注入!)
测试:
复杂类型
pojo类文件

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

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

真实环境

public class Student {
    private String name;
    private Address address;//通过ref赋值
    private String[] books;
    private List<String> hobbies;
    private Map<String,String> card;
    private Set<String> game;
    private String wife;
    private Properties info;
    }

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="pojo.Student">
        <!--普通值注入-->
        <property name="name" value="hzc"/>
    </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="pojo.Address">

    </bean>


    <bean id="student" class="pojo.Student">
        <!--第一种,常量注入-->
        <property name="name" value="hzc"/>
        <!--第二种,引用注入,ref-->
        <property name="address" ref="address"/>
        <!--数组注入-->
        <property name="books">
            <array>
                <value>《红楼梦》</value>
                <value>《西游记》</value>
                <value>《水浒传》</value>
                <value>《三国演义》</value>
            </array>
        </property>
        <!--链表注入-->
        <property name="hobbies">
            <list>
                <value>打游戏</value>
                <value>打代码</value>
                <value>学习</value>
            </list>
        </property>
        <!--set注入-->
        <property name="game">
            <set>
                <value>王者荣耀</value>
                <value>英雄联盟</value>
            </set>
        </property>
        <!--null值注入-->
        <property name="wife">
            <null></null>
        </property>
        <!--图注入-->
        <property name="card">
            <map>
                <entry key="身份证" value="123456123456781234"/>
                <entry key="银河卡" value="123123123123"/>
            </map>
        </property>
        <!--Properties注入
        key=value
        -->
        <property name="info">
            <props>
                <prop key="driver">2</prop>
                <prop key="url"></prop>
                <prop key="username"></prop>
                <prop key="password"></prop>
            </props>
        </property>
    </bean>
</beans>

3.拓展方式注入

可以使用p命名空间和c命名空间注入
p-namespace对应set注入
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"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--p命名空间注入,可以直接注入属性的值,p=property-->
    <bean id="user" class="pojo.User" p:name="cz" p:age="18"/>
</beans>


c-namespace对应构造器注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--c命名空间注入,通过构造器注入:c=construct-args-->
    <bean id="user2" class="pojo.User" c:age="21" c:name="zc"/>

</beans>

注意:p命名空间和c命名空间不能直接使用,需要导入xml约束
在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值