DAL 发表于 2017-8-15 10:13:56

关于在spring框架中注入日期bean的疑问

在下面的代码中我只在java的main函数中调用了bean为customer名的bean为什么spring可以自动转换为date类型的变量在main函数中输出???????
main函数:
ackage com.yiibai.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
        public static void main(String[] args) {
                ApplicationContext context = new ClassPathXmlApplicationContext(
                                "SpringBeans.xml");

                Customer cust = (Customer) context.getBean("customer");
                System.out.println(cust);
               
        }
}
对应的xml文件的配置:
<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.5.xsd">

        <bean id="dateEditor"
                class="org.springframework.beans.propertyeditors.CustomDateEditor">

                <constructor-arg>
                        <bean class="java.text.SimpleDateFormat">
                                <constructor-arg value="yyyy-MM-dd" />
                        </bean>
                </constructor-arg>
                <constructor-arg value="true" />

        </bean>

        <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
                <property name="customEditors">
                        <map>
                                <entry key="java.util.Date">
                                        <ref local="dateEditor" />
                                </entry>
                        </map>
                </property>
        </bean>

        <bean id="customer" class="com.yiibai.common.Customer">
                <property name="date" value="2015-12-31" />
        </bean>

</beans>

凡凡殇清 发表于 2017-12-29 14:19:10

因该是 customer 这个类里面有toString方法你输出了date 这个属性了   
<bean id="customer" class="com.yiibai.common.Customer">
                <property name="date" value="2015-12-31" />
      </bean>
这里你就已经给他赋值了   
页: [1]
查看完整版本: 关于在spring框架中注入日期bean的疑问