<<SSM——Spring 学习总结(一) Spring IOC容器
Spring 学习总结(二)Spring IOC容器
1、通过配置文件向 Map、List、Set、Properties、String数组中注入数据
- 创建Map、List、Set、Properties、String数组元素,并创建set,get,toString方法;
ackage qiao.student;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Student {
private Map<String,String> map;
private Set<String> set;
private String[] str;
private List<String> list;
private Properties props;
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Set<String> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
public String[] getStr() {
return str;
}
public void setStr(String[] str) {
this.str = str;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Properties getProps() {
return props;
}
public void setProps(Properties props) {
this.props = props;
}
public String toString() {
String s = "";
for(String sc:str) {
s += sc+",";
}
return "list:"+this.list+"\nMap:"+this.map+"\nSet:"+this.set+"\nProperties"+this.props+"\nString:"+s;
}
}
- 编写配置文件
<?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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<bean id = "student1" class = "qiao.student.Student">
<property name="list"> <!-- 向list容器中注入值 -->
<list>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</list>
</property>
<property name="set"> <!-- 向set容器中注入值 -->
<set>
<value>ddd</value>
<value>eee</value>
<value>fff</value>
</set>
</property>
<property name = "map"> <!-- 向map容器中注入值 -->
<map>
<entry>
<key> <!-- Map 是k-v对所以复制时需要注意 -->
<value>ggg</value>
</key>
<value>hhh</value>
</entry>
<entry>
<key>
<value>111</value>
</key>
<value>222</value>
</entry>
</map>
</property>
<property name="str"> <!-- 向String[]容器中注入值 -->
<array>
<value>iii</value>
<value>jjj</value>
<value>kkk</value>
</array>
</property>
<property name="props"> <!-- 向property中注入值 -->
<props>
<prop key="ccc">ccc</prop>
<prop key="ddd">ddd</prop>
<prop key="eee">eee</prop>
</props>
</property>
</bean>
<!-- 该复制方法不唯一这只是其中一种-->
</beans>
- 编写测试类
package qiao.text;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import qiao.student.Student;
public class Test {
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student)context.getBean("student1");
System.out.println(student);
}
}
- 运行结果
如有问题还请指出,谢谢。江湖最高道义 “抱拳了”