首页 文章 精选 留言 我的

精选列表

搜索[SQL注入],共10014篇文章
优秀的个人博客,低调大师

Spring中为bean注入Date对象

比如我们有下面的一个bean: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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,但是如果我们使用下面的配置: 1 2 3 4 5 6 7 8 9 10 < 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 > 然后我们尝试着运行的话 1 2 3 4 5 6 7 8 9 10 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,然后引用。如下解决: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 < 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对象。 1 2 3 4 5 6 7 8 9 10 < 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 > 1 2 3 4 5 6 7 8 9 < bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> < property name="customEditors"> < map > < entry key="java.util.Date"> < ref local="dateEditor" /> </ entry > </ map > </ property > </ bean > 完整的配置为: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 < 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 > ============================================================================== 本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/12/27/2835191.html,如需转载请自行联系原作者

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册