import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Person {
private String name;
private int age;
private String[] arr;
private List<String> list;
private Set<String> set;
private Map<String, String> map;
private Properties properties;
private Date birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String[] getArr() {
return arr;
}
public void setArr(String[] arr) {
this.arr = arr;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Set<String> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="person" class="com.spring.Person">
<property name="name" value="张三"></property>
<property name="age" value="28"></property>
<property name="arr">
<list>
<value>西施</value>
<value>貂蝉</value>
<value>王昭君</value>
<value>杨玉环</value>
</list>
</property>
<property name="set">
<set>
<value>红色</value>
<value>蓝色</value>
</set>
</property>
<property name="list">
<list>
<value>刘备</value>
<value>曹操</value>
<value>孙权</value>
</list>
</property>
<property name="map">
<map>
<entry key="001" value="华为"></entry>
<entry key="002" value="腾讯"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="0">男</prop>
<prop key="1">女</prop>
</props>
</property>
<property name="birthday">
<bean factory-bean="dateFormat" factory-method="parse">
<constructor-arg value="2018-06-05"></constructor-arg>
</bean>
</property>
</bean>
<bean id="dateFormat" class="java.text.SimpleDateFormat">
<constructor-arg value="yyyy-MM-dd"></constructor-arg>
</bean>
</beans>
package com.spring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) factory.getBean("person");
System.out.println(person.getName());
System.out.println(person.getAge());
for (String str : person.getArr()) {
System.out.println(str);
}
for (String str : person.getList()) {
System.out.println(str);
}
System.out.println(person.getSet());
System.out.println(person.getMap());
System.out.println(person.getProperties());
System.out.println(person.getBirthday());
}
}
本文展示了一个使用Spring框架进行XML配置的例子,包括基本的属性注入、集合类型的配置及日期类型的处理方式。
802

被折叠的 条评论
为什么被折叠?



