spring bean 日期格式注入的几种模式
来源:互联网 发布:网站seo工作内容 编辑:程序博客网 时间:2024/06/05 03:20
1 当我们进行bean注入到spring当中的时候,这个bean当中可能会存在着一些特殊类型的数据元素,如Date类型,当我们不进行一些特殊处理的时候,想直接给其赋值就会报告错误。
我们定义了如下类
- package com.japie.customproperty;
- import java.util.Date;
- /**
- * 自定义属性配置
- * @author Japie
- * 下午06:15:04
- */
- public class BoyMan {
- private Date birthDay;
- private String username;
- public BoyMan() {
- }
- public Date getBirthDay() {
- return birthDay;
- }
- public void setBirthDay(Date birthDay) {
- this.birthDay = birthDay;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- @Override
- public String toString()
- {
- return "BoyMan:["+username+this.birthDay+"]";
- }
- public static void main(String[] args) {
- }
- }
配置文件为
- <bean id="boyMan" class="com.japie.customproperty.BoyMan">
- <property name="birthDay" value="2011-06-17">
- <property name="username" value="japie"/>
- </bean>
当进行测试的时候报告如下的错误。
- Caused by: org.springframework.beans.TypeMismatchException:
- Failed to convert property value of type [java.lang.String] to
- required type [java.util.Date] for property 'birthDay';
2 那么如何解决上面的问题呢,有如下几种方法供参考
Declare a dateFormat bean, in “customer” bean, reference “dateFormat” bean as a factory bean. The factory method will call SimpleDateFormat.parse()
to convert String into Date object automatically.
1) 定义一个dateFormat bean,而后通而后通过工程模式在boyman这个类当中用构造器的模式进行引用,其实就是调用SimpleDateFormat.parse() 的方法,配置文件如下
- <bean id="boyMan" class="com.japie.customproperty.BoyMan">
- <property name="birthDay">
- <bean factory-bean="dateFormat" factory-method="parse">
- <constructor-arg value="2011-06-17" />
- </bean>
- </property>
- <property name="username" value="japie"/>
- </bean>
如此就可以进行正常的赋值了。
2)自定义日期熟悉编辑器
- <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>
3』通过重写spring的熟悉方法来实现代码如下
- package com.japie.injection;
- import java.beans.PropertyEditorSupport;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- /**
- * java.util.Date属性编辑器
- * @author japie
- *
- */
- public class UtilDatePropertyEditor extends PropertyEditorSupport {
- private String pattern;
- @Override
- public void setAsText(String text) throws IllegalArgumentException {
- System.out.println("---UtilDatePropertyEditor.setAsText()--->" + text);
- try {
- Date date = new SimpleDateFormat(pattern).parse(text);
- this.setValue(date);
- } catch (ParseException e) {
- e.printStackTrace();
- throw new IllegalArgumentException(text);
- }
- }
- public void setPattern(String pattern) {
- this.pattern = pattern;
- }
- }
以及在我们具体的bean当中引用其的配置
- <bean id="boyMan" class="com.japie.customproperty.BoyMan">
- <property name="birthDay" value="2011-06-17" />
- <property name="username" value="japie"/>
- </bean>
转载自:http://cjjwzs.iteye.com/blog/1097099
0 0
- spring bean 日期格式注入的几种模式
- Spring注入Bean的几种方式
- spring的注入bean的几种方式
- Spring学习,Bean配置文件注入的几种方式
- 声明Spring Bean和注入Bean的几种常用注解和区别
- Spring给bean注入日期属性的方法
- Spring的bean注入
- 【Spring】 Bean的注入
- Spring学习笔记二(Bean注入的几种方式)
- Spring注入日期到bean属性-CustomDateEditor
- Spring bean的三种注入方式
- 实例注入Bean的几种形式,特别注意多例模式下Bean生命周期无人管理
- spring几种依赖注入方式以及ref-local/bean,factory-bean,factory-method区别联系
- Spring的bean注入方式
- spring bean注入的方式
- Spring中bean的注入
- 获取Spring Bean 的几种方法.
- Spring获取Bean的几种方式
- error C2143: 语法错误 : 缺少“;”(在“template”的前面)
- 位域
- 15.2.2
- oracle lag和lead函数
- 字符串转换成整数&带通配符的字符串匹配问题
- spring bean 日期格式注入的几种模式
- Linux下网络流量实时监控工具大全
- JavaScript-2.4 改进的Hello程序,使用div,---ShinePans
- Kinetis K系列(飞思卡尔)
- 15.2.3
- linux c 信号处理
- Ant和Maven的作用是什么?两者之间功能、特点有哪些区别?
- C/C++ 类型转换
- Linux下的计算器(bc、expr、dc、echo、awk)知多少?