Spring5(7) —— 依赖注入之Set注入/对象复杂属性赋值

在这里插入图片描述

1.完成student对象中所有属性的依赖注入

<?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.thhh.pojo.Student">
        <!--1、name:string-->
        <property name="name" value="张三"/>

        <!--2、address:对象属性-->
        <property name="address" ref="address"/>

        <!--3、books:数组属性-->
        <property name="books">
            <array>
                <value>三国</value>
                <value>水浒</value>
                <value>红楼</value>
                <value>西游</value>
            </array>
        </property>

        <!--4、hobbys:list属性-->
        <property name="hobbys">
            <list>
                <value>coding</value>
                <value>sleeping</value>
                <value>movie</value>
            </list>
        </property>

        <!--5、card:map属性-->
        <property name="card">
            <map>
                <entry key="学生证" value="1234242"/>
                <entry key="身份证" value="2132131"/>
                <entry key="银行卡" value="3213121"/>
            </map>
        </property>

        <!--6、games:set属性-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>BOB</value>
                <value>COC</value>
            </set>
        </property>

        <!--7、wife:NULL值-->
        <property name="wife">
            <null/>
        </property>

        <!--8、properties:properties配置文件-->
        <property name="properties">
            <props>
                <prop key="driver">com.mysql.jdbc.DRIVER</prop>
                <prop key="url">jdbc:mysql://localhost:3306/smbms</prop>
                <prop key="username">root</prop>
                <prop key="password">123</prop>
            </props>
        </property>
    </bean>

    <bean id="address" class="com.thhh.pojo.Address">
        <property name="address" value="chunxi road"/>
    </bean>
</beans>

在这里插入图片描述

2.测试

package com.thhh.pojo;

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

输出
在这里插入图片描述

Student{
	name='张三', 
	
	address=Address{address='chunxi road'}, 
	
	books=[三国, 水浒, 红楼, 西游], 
	
	hobbys=[coding, sleeping, movie], 
	
	card={
		学生证=1234242, 
		身份证=2132131, 
		银行卡=3213121
	}, 
	
	games=[LOL, BOB, COC], 
	
	wife='null', 
	
	properties={
		password=123, 
		driver=com.mysql.jdbc.DRIVER, 
		url=jdbc:mysql://localhost:3306/smbms, 
		username=root
	}
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值