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