02_Spring3.2_属性类型设值注入

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包无变化。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值