public class Objectbean {
private String str= "";
private String[] str1;
private Map map = new HashMap();
private List list = new ArrayList();
private Date date;

public Objectbean() {}

public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public String[] getStr1() {
return str1;
}
public void setStr1(String[] str1) {
this.str1 = str1;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
} public Date getDate() {
return date;
} public void setDate(Date date) {
this.date = date;
}

}
public class UtilDatePropertyEditor extends PropertyEditorSupport{
private String format="yyyy-MM-DD";
@Override
public void setAsText(String arg0) throws IllegalArgumentException {
System.out.println(arg0);
SimpleDateFormat sdf = new SimpleDateFormat(format);

Date d;
try {
d = sdf.parse(arg0);
this.setValue(d);
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}}
<?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-2.0.xsd">
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="com.way.spring.UtilDatePropertyEditor"></bean>
</entry>
</map>
</property>
</bean>

</beans>
public class Mytest extends TestCase{
private BeanFactory factory;
@Override
protected void setUp() throws Exception {
factory = new ClassPathXmlApplicationContext("applicationContext_*.xml");
}

public void test() {
Objectbean ob = (Objectbean)factory.getBean("ob");
System.out.println(ob.getStr());
System.out.println(ob.getList());
System.out.println(ob.getMap());
System.out.println("数组=" + ob.getStr1());
System.out.println(ob.getDate());
}
}
<?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-2.0.xsd">
<bean id="ob" class="com.way.spring.Objectbean">
<property name="str">
<value>sky</value>
</property>
<property name="str1">
<list>
<value>array1</value>
</list>
</property>
<property name="map">
<map>
<entry key="1" value="map1"/>
<entry key="2" value="map2"></entry>

</map>
</property>
<property name="list">
<list>
<value>english</value>
<value>macth</value>
</list>
</property>
<property name="date">
<value>2009-12-12</value>这个值得为什么会自己跑到属性编辑器里面去,我没有传这个值得
</property>
</bean>
</beans>setAsText()这个方法是怎么实现去调用property里面的date值.我根本没传进来阿.他是怎么实现的?

解决方案 »

  1.   

    不知道你有没有主意到spring在处理多语言的时候,会有如下的bean: <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames">
    <list>
    <value>messages</value>
    </list>
    </property>
    </bean>注意,这里的bean后面的id必须是messageSource。我的意思是想说,有些id是固定的,就跟你写的那个customEditorConfigurer一样。
    下面这一段很关键:                <entry key="java.util.Date">
                        <bean class="com.way.spring.UtilDatePropertyEditor"></bean>
                    </entry>如果类型是java.util.Date,则你写的这个UtilDatePropertyEditor类就起作用了。
    它重写了setAsText方法。有了这样一个接口,你可以将String型的值(文本文件中只能是String型)赋给任何其他类型的变量。
    其实楼主,在写这些之前我也不知道。我看完了你的帖子,把代码copy过来,改了一下,然后自己另外加了一个属性尝试了一下。后来我明白了。
      

  2.   

    你是不是又加了一个date值试了?
    怎么测试的
    我就想不出测试方法....
      

  3.   

    我的是这个
    org.springframework.beans.factory.config.CustomEditorConfigurer
    和你的下面这个有关系吗?
    org.springframework.context.support.ResourceBundleMessageSource
    能稍微解释下吗
      

  4.   

    没有关系。我只是想举个例子跟你说明你写的那些内容为什么能生效。
    注意下面的代码:
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    只要你在这里将id定义成messageSource,那么Spring就知道它是一个message资源bean。所以说,这里的messageSource是有特殊的含义的。在这里定义了messageSource的一些属性以后,你只需要按照你定义的属性做配置就行了,其他的事情会有Spring来帮你处理。
    和这个相同的道理
    <bean id="customEditorConfigurer"
    你这里的customEditorConfigurer也是有特殊含义的,Spring认识它。你可以自己再写一个例子,比如写一个Calendar的属性,bean.xml中如下:            <map>
                    <entry key="java.util.Date">
                        <bean class="com.way.spring.UtilDatePropertyEditor"></bean>
                    </entry>
                    <entry key="java.util.Calendar">
                        <bean class="com.way.spring.UtilCalendarPropertyEditor"></bean>
                    </entry>
                </map>然后定义相应的类:com.way.spring.UtilCalendarPropertyEditor
    UtilCalendarPropertyEditor中也和UtilDatePropertyEditor做类似的操作,比如set成当前日期。
    整个一趟配置完,运行结果出来以后,你就明白了。
      

  5.   

     <entry key="java.util.Date">
        <bean class="com.way.spring.UtilDatePropertyEditor"></bean>
     </entry> 这个key指的就是你的数据类型,你现在是要解析date,所以key就是java.util.Date假如有一个自定义的类com.text.testBean ,只有2个属性,String和Date 那key一般设置成class的全名,在对应的TestBeanPropertyEditor中,可以直接拿 <value>a,2008-9-9</value> 中的a,2008-9-9的这个字符串到TestBeanPropertyEditor的setAsText()中对应方法中巴a,2008-9-9解析成String和Date 就可以实例化com.text.testBean 了