当存在复杂类型的属性时,需要这个,拿日期类型为例:
转化器类:
public class StringToDateConvertor implements Converter<String, Date> {
@Override
public Date convert(String s) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
return simpleDateFormat.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
转化器配置:
<bean id="user" class="cn.itnls.User">
<property name="birthday" value="1995-11-06"/>
</bean>
<bean id="StringToDateConvertor" class="cn.itnls.config.StringToDateConvertor"/>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<ref bean="StringToDateConvertor"/>
</set>
</property>
</bean>
测试类:
@Test
public void iocTest1(){
AbstractApplicationContext applicationContext =
new ClassPathXmlApplicationContext("application.xml");
User bean = applicationContext.getBean(User.class);
System.out.println(bean);
}