02_Spring3.2_属性类型设值注入
2013-07-03 07:45
453 查看
package cn.lichen.bean; import java.util.Date; public class User { private String name; private int age; private Date birthday; private String[] anyPro; private List<String> listPro; private Set<String> setPro; private Map<String, String> mapPro; private Properties properties; public void setName(String name) { this.name = name; } public String getName() { return name; } public int getAge() { return age; } public Date getBirthday() { return birthday; } public String[] getAnyPro() { return anyPro; } public List<String> getListPro() { return listPro; } public Set<String> getSetPro() { return setPro; } public Map<String, String> getMapPro() { return mapPro; } public void setAge(int age) { this.age = age; } public void setBirthday(Date birthday) { this.birthday = birthday; } public void setAnyPro(String[] anyPro) { this.anyPro = anyPro; } public void setListPro(List<String> listPro) { this.listPro = listPro; } public void setSetPro(Set<String> setPro) { this.setPro = setPro; } public void setMapPro(Map<String, String> mapPro) { this.mapPro = mapPro; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } }
util类:
public class UtilDatePropertyEditor extends PropertyEditorSupport { private String format; @Override public void setAsText(String text) throws IllegalArgumentException { SimpleDateFormat sdf = new SimpleDateFormat(format); try { Date date = sdf.parse(text); this.setValue(date); } catch (ParseException e) { e.printStackTrace(); } } public String getFormat() { return format; } public void setFormat(String format) { this.format = format; }
<?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">
<bean id="customEditorConfigurer"
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<!-- 内部Bean -->
<bean class="cn.lichen.util.UtilDatePropertyEditor">
<property name="format">
<value>yyyy-MM-dd</value>
</property>
</bean>
</entry>
</map>
</property>
</bean>
<bean id="user" class="cn.lichen.bean.User">
<!-- 设值注入:要求类提供无参的构造方法,必需提供属性的seter()方法 -->
<property name="name" value="LiChen"></property>
<property name="age" value="22"></property>
<property name="birthday" value="1992-2-6"></property>
<property name="anyPro">
<array>
<value>any1</value>
<value>any2</value>
<value>any3</value>
</array>
</property>
<property name="listPro">
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
</list>
</property>
<property name="setPro">
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
</set>
</property>
<property name="mapPro">
<map>
<entry key="1" value="map1"></entry>
<entry key="2" value="map2"></entry>
<entry key="3" value="map3"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="1">prop1</prop>
<prop key="2">prop2</prop>
<prop key="3">prop3</prop>
</props>
</property>
</bean>
</beans>
Test类:
package cn.lichen; import java.util.Iterator; public class TestUser { @Test public void testToString() { BeanFactory factory = new ClassPathXmlApplicationContext( "applicationContext.xml"); User user = (User) factory.getBean("user"); System.out.println("My name is " + user.getName() + "\n" + user.getAge() + "\n" + user.getBirthday().toString()); // 数组 System.out.println(user.getAnyPro()[0]); // List集合 List<String> list = user.getListPro(); Iterator<String> it = list.iterator(); while (it.hasNext()) { String value = it.next(); System.out.println(value); } // Set集合 Set<String> set = user.getSetPro(); for (Iterator<String> iterator = set.iterator(); iterator.hasNext();) { String value = iterator.next(); System.out.println(value); } // map集合 Map<String, String> map = user.getMapPro(); Set<Entry<String, String>> entry = map.entrySet(); for (Iterator<Entry<String, String>> iterator = entry.iterator(); iterator .hasNext();) { Entry<String, String> mapEntry = iterator.next(); System.out.println(mapEntry.getKey() + "\t" + mapEntry.getValue()); } //properties集合,以前 都没听说 Properties properties = user.getProperties(); Set<String> setProperties = properties.stringPropertyNames(); for (Iterator<String> iterator = setProperties.iterator(); iterator .hasNext();) { String key = iterator.next(); String value = properties.getProperty(key); System.out.println("properties:" + "\t" + key + "\t" + value); } } }
lib包无变化。
相关文章推荐
- 02_Spring3.2_属性类型构造注入
- Spring属性注入【三】 复杂类型注入
- Java框架spring 学习笔记(八):注入对象类型属性
- spring如何注入Date类型的属性
- Spring入门_02_属性注入
- spring如何注入Date类型的属性
- Spring注入Date类型的属性,自定义Date属性编辑器
- Spring注入对象类型的属性
- spring各种类型的属性注入
- 【JavaEE学习笔记】Spring_02_IoC/DI依赖注入,集合对象属性注入,自动装配,代理模式
- Spring、基本类型属性和集合类型属性的注入
- Spring_02 注入类型值、利用引用注入类型值、spring表达式、与类相关的注解、与依赖注入相关的注解、注解扫描
- Spring-02-各种类型信息的注入配置格式
- Spring、基本类型属性和集合类型属性的注入
- Spring-注入参数详解-[集合类型属性]
- Spring(7)注入对象类型的属性
- 如何让xdoclet 的spring 标签支持java.util.Map类型属性的注入
- Spring注入复杂类型属性
- Spring(8)注入复杂类型属性
- Spring入门_02_属性注入