spring如何注入Date类型的属性
2016-08-29 14:10
337 查看
spring可以为属性注入基本类型的值,也可以注入一个bean。当想注入非基本类型的值就得用到属性编辑器。它一般用在类型无法识别,如日期等。
实现步骤为以下两步:
1)继承PropertyEditorSupport
2)重写其setAsText方法,text是配置文件中的值(也就是为bean注入的值),我们就是将这个text按照需求进行转换。
先看下没用属性编辑器的情况:
public class MyDate {
private Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext(
"classpath:com/pb/propertyeditor/applicationContext.xml");
MyDate date = (MyDate) context.getBean("md");
System.out.println(date.getDate());
}
}
bean id="md" class="com.pb.propertyeditor.MyDate">
<property name="date">
<value>2011-1-1</value>
</property>
</bean>
发生异常:
Caused by: 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 found
发生异常,没有发现匹配的编辑器或转换器。
现在需要的就是定义一个属性编辑器,并在spring中加入
public class CustomerProperty extends PropertyEditorSupport {
String format;
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
// text为需要转换的值,当为bean注入的类型与编辑器转换的类型匹配时就会交给setAsText方法处理
public void setAsText(String text) throws IllegalArgumentException {
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
this.setValue(sdf.parse(text));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <!--配置一个自定义编辑器-->
<property name="customEditors"><span style="white-space:pre"> </span><!--需要编辑的属性类型,是一个map-->
<map>
<entry key="java.util.Date">
<bean class="com.pb.propertyeditor.CustomerProperty">
<property name="format" value="yyyy-mm-dd" /> <!--注入需要转换的格式-->
</bean>
</entry>
</map>
</property>
</bean>
<bean id="md" class="com.pb.propertyeditor.MyDate">
<property name="date">
<value>2011-1-1</value>
</property>
</bean>
输出结果:Sat Jan 01 00:01:00 CST 2011
实现步骤为以下两步:
1)继承PropertyEditorSupport
2)重写其setAsText方法,text是配置文件中的值(也就是为bean注入的值),我们就是将这个text按照需求进行转换。
先看下没用属性编辑器的情况:
public class MyDate {
private Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext(
"classpath:com/pb/propertyeditor/applicationContext.xml");
MyDate date = (MyDate) context.getBean("md");
System.out.println(date.getDate());
}
}
bean id="md" class="com.pb.propertyeditor.MyDate">
<property name="date">
<value>2011-1-1</value>
</property>
</bean>
发生异常:
Caused by: 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 found
发生异常,没有发现匹配的编辑器或转换器。
现在需要的就是定义一个属性编辑器,并在spring中加入
public class CustomerProperty extends PropertyEditorSupport {
String format;
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
// text为需要转换的值,当为bean注入的类型与编辑器转换的类型匹配时就会交给setAsText方法处理
public void setAsText(String text) throws IllegalArgumentException {
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
this.setValue(sdf.parse(text));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <!--配置一个自定义编辑器-->
<property name="customEditors"><span style="white-space:pre"> </span><!--需要编辑的属性类型,是一个map-->
<map>
<entry key="java.util.Date">
<bean class="com.pb.propertyeditor.CustomerProperty">
<property name="format" value="yyyy-mm-dd" /> <!--注入需要转换的格式-->
</bean>
</entry>
</map>
</property>
</bean>
<bean id="md" class="com.pb.propertyeditor.MyDate">
<property name="date">
<value>2011-1-1</value>
</property>
</bean>
输出结果:Sat Jan 01 00:01:00 CST 2011
相关文章推荐
- spring如何注入Date类型的属性
- Spring注入Date类型的属性,自定义Date属性编辑器
- 如何让xdoclet 的spring 标签支持java.util.Map类型属性的注入
- spring 3配置文件中如何注入map list set等类型
- 02_Spring3.2_属性类型设值注入
- Spring学习笔记——Spring如何装配各种类型的属性以及实际应用
- ITCAST视频-Spring学习笔记(Spring如何装配各种集合类型的属性)
- [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间
- Spring、基本类型属性和集合类型属性的注入
- Spring+Servlet整合(如何向Servlet注入属性(转),servlet获取spring容器中的bean)
- Spring 中如何向 Bean 注入系统属性或环境变量
- Spring如何通过注解方式注入静态属性
- Spring:Set注入:基本类型、List、Map、Set、Array、Date类型注入...
- Struts2、Hibernate、Spring整合时如何向Servlet注入属性
- Spring依赖注入对于Date类型数据的处理
- spring属性注入中的date注入异常解决方案
- spring注入,不同类型如何注入
- Spring如何装配各种集合类型的属性
- Spring注入日期到bean属性-CustomDateEditor
- 09_传智播客Spring2.5视频教程_Spring如何装配各种集合类型的属性