zoukankan      html  css  js  c++  java
  • Spring中为bean注入Date对象

    比如我们有下面的一个bean:

    import java.util.Date;
     
    public class Customer {
     
    	Date date;
     
    	public Date getDate() {
    		return date;
    	}
     
    	public void setDate(Date date) {
    		this.date = date;
    	}
     
    	@Override
    	public String toString() {
    		return "Customer [date=" + date + "]";
    	}
     
    }
    

      注意我们上面的bean中有一个Date,但是如果我们使用下面的配置:

    <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="customer" class="com.mkyong.common.Customer">
    		<property name="date" value="2010-01-31" />
    	</bean>
     
    </beans>
    

      然后我们尝试着运行的话

    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);
     
    	}
    }
    

    会出现如下的错误:

    Caused by: org.springframework.beans.TypeMismatchException: 
    	Failed to convert property value of type [java.lang.String] to 
    	required type [java.util.Date] for property 'date'; 
     
    nested exception is java.lang.IllegalArgumentException: 
    	Cannot convert value of type [java.lang.String] to
    	required type [java.util.Date] for property 'date': 
    	no matching editors or conversion strategy foun
    在这里提供两种解决办法:

    1. Factory bean

    声明一个dateFormat的bean,然后引用。如下解决:

    <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="dateFormat" class="java.text.SimpleDateFormat">
    		<constructor-arg value="yyyy-MM-dd" />
    	</bean>
     
    	<bean id="customer" class="com.mkyong.common.Customer">
    		<property name="date">
    			<bean factory-bean="dateFormat" factory-method="parse">
    				<constructor-arg value="2010-01-31" />
    			</bean>
    		</property>
    	</bean>
     
    </beans>
    

      

    2. CustomDateEditor

    我们声明一个CustomDateEditor,将String转换为Date对象。

    <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>
    

      完整的配置为:

    <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.mkyong.common.Customer">
    		<property name="date" value="2010-02-31" />
    	</bean>
     
    </beans>
    

      

  • 相关阅读:
    2018-10-8-Win10-使用-GHO-安装出现-UWP-软件打开闪退-应用商店无法安装软件
    2019-4-29-WPF-如何判断一个控件在滚动条的里面是用户可见
    2018-9-29-Roslyn-通过-Nuget-引用源代码-在-VS-智能提示正常但是无法编译
    2019-8-31-dotnet-判断程序当前使用管理员运行降低权使用普通权限运行
    2019-8-31-dotnet-非泛型-类型-System.Collections.IEnumerable-不能与类型实参一起使用
    2019-3-16-win10-uwp-在-ItemsPanelTemplate-里面通过样式绑定-Orientation-显示方向
    2018-10-29-微软-Tech-Summit-技术暨生态大会课程-·-基于-Roslyn-打造高性能预编译框架...
    2019-7-27-解决从旧格式的-csproj-迁移到新格式的-csproj-格式-AssemblyInfo-文件值重复问题...
    about
    2018-2-13-win10-uwp-上传Nuget-让别人用我们的库
  • 原文地址:https://www.cnblogs.com/rollenholt/p/2835191.html
Copyright © 2011-2022 走看看