Spring-06-DI依赖注入

本文详细介绍了Spring框架中的依赖注入,重点讲解了set注入的使用,包括普通值、引用类型、数组、列表、映射、集合、null值和Properties的注入方式,并提供了详细的bean.xml配置示例。此外,还探讨了p命名空间和c命名空间注入的两种替代方法,通过实例展示了如何通过不同方式设置bean属性。
摘要由CSDN通过智能技术生成

Spring-06-DI依赖注入

构造器注入

set注入(重要)

在Spring中如何给属性(复杂的)赋值的问题

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

模拟复杂属性

实体类

public class Student {
    private String name; //普通值
    private Address address;//引用类型
    private String[] books;//数组
    private List<String> hobbys;//列表
    private Map<String,String> card;//map
    private Set<String> games;//集合
    private String wife;//null
    private Properties info;//Properties类
    ...get,set,toString...
}

bean.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="address" class="com.cmy.pojo.Address"/>

    <bean id="student" class="com.cmy.pojo.Student">
        <!--1.普通值注入,直接使用value-->
        <property name="name" value="cmy"/>
        <!--2.引用类型注入,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>
            </list>
        </property>
        <!--map注入-->
        <property name="card">
            <map>
                <entry key="建设银行" value="123456"/>
                <entry key="农业银行" value="654321"/>
            </map>
        </property>
        <!--set注入-->
        <property name="games">
            <set>
                <value>魂斗罗</value>
                <value>超级玛丽</value>
                <value>LOL</value>
            </set>
        </property>
        <!--null注入-->
        <!--空值符串注入和null注入不同-->
        <property name="wife">
            <null/>
        </property>

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

    </bean>


</beans>

测试

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student s = (Student) context.getBean("student");
        System.out.println(s);
        //Student{name='cmy', address=com.cmy.pojo.Address@1d16f93d,
        //books=[红楼梦, 西游记, 三国演义, 水浒传], hobbys=[看电影, 看书],
        //card={建设银行=123456, 农业银行=654321}, games=[魂斗罗, 超级玛丽, LOL],
        //wife='null', info={学号=11111111, 性别=男}}
    }
}

其他方式注入

p命名空间注入

<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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

        <!--p命名空间注入,可以直接注入属性的值-->
        <!--要+ xmlns:p="http://www.springframework.org/schema/p"-->
        <bean id="user" class="com.cmy.pojo.User" p:name="void" p:gender=""/>

</beans>

测试

@Test
public void test(){
    ApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml");
    User user = (User) context.getBean("user");
    System.out.println(user);
    //User{name='void', gender='男'} 
}

c命名空间注入

<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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

        <!--c命名空间注入,必须有有参构造器,通过构造器注入-->
        <!--必须导入xmlns:c="http://www.springframework.org/schema/c"-->
        <bean id="user2" class="com.cmy.pojo.User" c:name="xiaoming" c:gender=""/>

</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值