→阿童沐

財富==支撐一個人生存多長時間的能力!

导航

Spring配置文件中对于各种数据类型的支持-

spring的Ioc提供了对对象的动态创建,并且提供了对常见数据类型的处理功能。如:基本数据类型及其封装类,字符串,集合,Properties等。
 
Java类:
View Code
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
View Code
<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>


 
测试代码:
View Code
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 中遇到的所有情况。

本文出自 “夜狼” 博客,http://yangfei520.blog.51cto.com/1041581/244814

posted on 2011-11-30 12:15  阿童沐  阅读(591)  评论(0)    收藏  举报