1.Spring基于XML配置的装配实验
(1)wrh_student.java类包含了Map、Set、List、数组、集合等这些容器,主要用于Spring基于XML配置的装配演示配置
import java.util.List;
import java.util.Map;
import java.util.Set;
public class wrh_Student {
private String username;
private List<String> hobbies;//list集合
private Map<String, String> address;//map集合
private Set<String> aliases;//set集合
private String[] array;//数组
/**
* 1.使用构造注入
* 1.1提供带有参数的有参构造方法。
*/
public wrh_Student(String username,List<String> hobbies,Map<String, String> address,Set<String> aliases,String[] array) {
super();
this.username=username;
this.hobbies=hobbies;
this.address=address;
this.aliases=aliases;
this.array=array;
}
/**
* 2.使用设置注入
* 2.1提供默认空参构造方法
* 2.2为所有属性提供setter方法
*/
public wrh_Student() {
super();
}
public void setUsername(String username) {
this.username=username;
}
public void setHobbies(List<String> hobbies) {
this.hobbies=hobbies;
}
public void setAddress(Map<String,String> address) {
this.address=address;
}
public void setAliases(Set <String> aliases) {
this.aliases=aliases;
}
public void setArray(String[] array) {
this.array=array;
}
public String toString() {
return "Student[username="+username+",hobbies="+hobbies+",address="+address+",aliases="+aliases+",array="+Array.toString(array)+"]";
}
}
(2)Spring配置文件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-4.3.xsd">
<!-- 使用构造注入方式装配Student实例 -->
<bean id="student1" class="wrh_homework.wrh_Student">
<constructor-arg index="0" value="xiaoming"/>
<!-- 注入list集合 -->
<constructor-arg index="1">
<list>
<value>"唱歌"</value>
<value>"跳舞"</value>
<value>"远足"</value>
</list>
</constructor-arg>
<!-- 注入map集合 -->
<constructor-arg index="2">
<map>
<entry key="chouzhou" value="潮州"/>
<entry key="guangdong" value="广东"/>
</map>
</constructor-arg>
<!-- 注入set集合 -->
<constructor-arg index="3">
<set>
<value>xiaoming1</value>
<value>xiaoming2</value>
</set>
</constructor-arg>
<!-- 注入array数组 -->
<constructor-arg index="4">
<array>
<value>数学</value>
<value>英语</value>
</array>
</constructor-arg>
</bean>
<!-- 使用设置注入方式装配Student实例 -->
<bean id="student2" class="wrh_homework.wrh_Student">
<property name="username" value="xiaohong"></property>
<!-- 注入list集合 -->
<property name="hobbies">
<list>
<value>"游泳"</value>
<value>"学习"</value>
<value>"开车"</value>
</list>
</property>
<!-- 注入map集合 -->
<property name="address">
<map>
<entry key="chouzhou" value="潮州"/>
<entry key="guangdong" value="广东"/>
</map>
</property>
<!-- 注入set集合 -->
<property name="aliases">
<set>
<value>xiaohong1,xiaohong2</value>
</set>
</property>
<!-- 注入array数组 -->
<property name="array">
<array>
<value>"语文"</value>
<value>"英语"</value>
<value>"历史"</value>
</array>
</property>
</bean>
</beans>
(3)编写测试类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class wrh_XmlBeanAssembleTest {
public static void main (String [] args) {
//定义配置文件路径
String xmlPath="wrh_homework/wrh_beans.xml";
//加载配置文件
ApplicationContext applicatContext=new ClassPathXmlApplicationContext(xmlPath);
//构造方式输出结果
System.out.println(applicatContext.getBean("student1"));
//设置方式输出结果
System.out.println(applicatContext.getBean("student2"));
}
}
输出结果:
实验中出现的问题:数组的注入打印出错,seter注入方法和构造注入方法同时打印array的地址
因为是刚入门Spring当看到这个问题时还以为是自己的标签属性使用错误了,接连修改了几次String[ ]array数组的配置文件。例如:将数组的属性标签改为,但是修改后调试结果仍然是错误(如下图)
经过一段盲改之后问题终究没有解决,冷静下来分析了问题
1.创建的两种输入方法都有输出且只有数组输出有误
2.数组的输出为数组地址
判断:数组的输出的数据类型配置错误
然后去检查了student类和xml配置文件,发现student类中的array的数据类型没有指明
修改前:
修改后:
结果正确显示: