鱼C论坛

 找回密码
 立即注册
查看: 3250|回复: 1

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

[复制链接]
发表于 2017-8-15 10:13:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

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

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

  8.                 Customer cust = (Customer) context.getBean("customer");
  9.                 System.out.println(cust);
  10.                
  11.         }
  12. }
复制代码

对应的xml文件的配置:
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.         xsi:schemaLocation="http://www.springframework.org/schema/beans
  4.         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

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

  7.                 <constructor-arg>
  8.                         <bean class="java.text.SimpleDateFormat">
  9.                                 <constructor-arg value="yyyy-MM-dd" />
  10.                         </bean>
  11.                 </constructor-arg>
  12.                 <constructor-arg value="true" />

  13.         </bean>

  14.         <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
  15.                 <property name="customEditors">
  16.                         <map>
  17.                                 <entry key="java.util.Date">
  18.                                         <ref local="dateEditor" />
  19.                                 </entry>
  20.                         </map>
  21.                 </property>
  22.         </bean>

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

  26. </beans>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 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>
这里你就已经给他赋值了     
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 03:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表