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 name="phone">
<list>
<value>phone1</value>
<value>phone2</value>
</list>
<property name="email">
<set>
<value>emailA</value>
<value>emailB</value>
</set>
<property name="score">
<map>
<entry>
<key>
<value>Core Java</value>
</key>
<value>100</value>
</entry>
<value>Oracle</value>
<value>90</value>
</map>
<property name="friends">
<props>
<prop key="111">lisi</prop>
<prop key="111">wangwu</prop>
</props>
</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,如需轉載請自行聯系原作者