spring的Ioc提供了对对象的动态创建,并且提供了对常见数据类型的处理功能。如:基本数据类型及其封装类,字符串,集合,Properties等。
Java类:
public class Test {
private Integer id;
private String name;
private List<String> phone;
private Set<String> email;
private Map<String, Double> score;
private Properties friends;
//一系列的setter.getter方法
}
配置文件 test.xml
<beans>
<bean id="student" class="com.cernet.spring.first.entity.Test">
<property name="id">
<value>1</value>
</property>
<property name="name">
<value>yangfei</value>
</property>
<property name="phone">
<list>
<value>phone1</value>
<value>phone2</value>
</list>
</property>
<property name="email">
<set>
<value>emailA</value>
<value>emailB</value>
</set>
</property>
<property name="score">
<map>
<entry>
<key>
<value>Core Java</value>
</key>
<value>100</value>
</entry>
<entry>
<key>
<value>Oracle</value>
</key>
<value>90</value>
</entry>
</map>
</property>
<property name="friends">
<props>
<prop key="111">lisi</prop>
<prop key="111">wangwu</prop>
</props>
</property>
</bean>
</beans>
测试代码:
public static void main(String[] args) {
BeanFactory factory=new XmlBeanFactory(new ClassPathResource("test.xml"));
Student stu=(Student)factory.getBean("student");
System.out.println("Id========"+stu.getId());
System.out.println(stu.getName());
List<String> l=stu.getPhone();
Iterator it=stu.getEmail().iterator();
Map m=stu.getScore();
}
对于一些更复杂的或者不便处理的数据类型,spring提供了属性编辑器功能由用户自己进行处理。总之,它会尽力处理Java 中遇到的所有情况。
本文转自NightWolves 51CTO博客,原文链接:http://blog.51cto.com/yangfei520/244814,如需转载请自行联系原作者