Spring注入Date类型的三种方法总结
spring注入date类型的三种方法总结
测试bean:
public class datebean {
private date birthday;
public date getbirthday() {
return birthday;
}
public void setbirthday(date birthday) {
this.birthday = birthday;
}
}
方式1:利用simpledateformat的构造方法注入
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" 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.xsd">
<bean id="dateformat" class="java.text.simpledateformat">
<constructor-arg value="yyyy-mm-dd" />
</bean>
<bean id="datebean" class="com.springdemo1.date类型注入.datebean">
<property name="birthday">
<bean factory-bean="dateformat" factory-method="parse">
<constructor-arg value="2015-12-31" />
</bean>
</property>
</bean>
</beans>
方式2:纯配置,先自定义customdateeditor,再转换类型
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" 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.xsd">
<!-- 自定义日期编辑器 -->
<bean id="dateeditor"
class="org.springframework.beans.propertyeditors.customdateeditor">
<constructor-arg>
<bean class="java.text.simpledateformat">
<constructor-arg value="yyyy-mm-dd"></constructor-arg>
</bean>
</constructor-arg>
<constructor-arg value="true" />
</bean>
<!-- 使 spring转换为java.util.date -->
<bean class="org.springframework.beans.factory.config.customeditorconfigurer">
<property name="customeditors">
<map>
<entry key="java.util.date">
<ref bean="dateeditor" />
</entry>
</map>
</property>
</bean>
</beans>
方式3:先用一个类重写propertyeditorsupport的setastext方法,再在配置文件中,配置转换类型就可以了,跟上面方法类似
public class mydatepropertyeditor extends propertyeditorsupport {
private 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();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" 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.xsd">
<!--方式3:创建一个类 重写propertyeditorsupport的setastext方法 -->
<!-- 自定义日期编辑器 -->
<bean class="org.springframework.beans.factory.config.customeditorconfigurer">
<property name="customeditors"> <!--需要编辑的属性类型,是一个map -->
<map>
<entry key="java.util.date">
<bean class="com.springdemo1.date类型注入.mydatepropertyeditor">
<property name="format" value="yyyy-mm-dd" /> <!--注入需要转换的格式 -->
</bean>
</entry>
</map>
</property>
</bean>
</beans>
测试:
public class datetest {
@test
public void testname() throws exception {
applicationcontext context = new classpathxmlapplicationcontext(
"applicationcontext.xml");
datebean bean = (datebean) context.getbean("datebean");
system.out.println(bean.getbirthday());
}
}
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关文章
- jsp+servlet实现文件上传与下载功能
- EJB3.0部署消息驱动Bean抛javax.naming.NameNotFoundException异常
- 在JSP中使用formatNumber控制要显示的小数位数方法
- 秒杀系统Web层设计的实现方法
- 将properties文件的配置设置为整个Web应用的全局变量实现方法
- JSP使用过滤器防止Xss漏洞
- 在JSP页面中动态生成图片验证码的方法实例
- 详解JSP 内置对象request常见用法
- 使用IDEA编写jsp时EL表达式不起作用的问题及解决方法
- jsp实现局部刷新页面、异步加载页面的方法
- Jsp中request的3个基础实践
- JavaServlet的文件上传和下载实现方法
- JSP页面的静态包含和动态包含使用方法


