当函数的字段类型是int型等普通类型或集合类型时,Spring已经内置了属性编辑器,用来把配置文件中的字符串转换成相应的类型为相对应的类型注入值.同时,程序员也可以自己编写属性编辑器,来实现把字符型变量转换成日期型或自定义类型.
首先,设计一个培训机构类AttributeEditor,代码如下:
import java.util.Date
public class AttributeEditor {
private Date dateValue;
public Date getDateValue() {
return dateValue;
}
public void setDateValue(Date dateValue) {
this.dateValue = dateValue;
}
}
接着,设计一个测试类UtilDatePropertyEditor,代码如下:
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class UitlDatePropertyEditor extends PropertyEditorSupport {
private String format = "yyyy-MM-dd"; //定义固定字符串格式
//重写父类方法
public void setAsText(String text)
throws IllegalArgumentException{
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
Date date = sdf.parse(text); //获取时间信息
this.setValue(date);
} catch(ParseException) {
e.printStackTrace();
}
}
public void setFormat(String format) {
this.format = format;
}
}
Junit测试代码如下:
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import junit.framework.TestCase;
public class TestAttrEditor extends TestCase {
private BeanFactory factory;
//重写父类方法
protected void setup() throws Exception {
factory = new ClassPathXmlApplicationContext ("applicationContext.xml");
}
public void testInjection() {
AttributeEditor ae = (AttributeEditor)factory.getBean("attributEditor");
System.out.println("bean.dateValue = " + ae.getDateValue());
}
}
其中,Spring配置文件applicationContext.xml配置如下:
<bean id="customEditorConfigurer"
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class=".../UtilDatePropertyEditor">
<!--设置时间格式-->
<property name="format" value="yyyy-MM-dd">
</bean>
</entry>
</map>
</property>
</bean>
<!--创建对象attributeEditor-->
<bean id="attributeEditor" class=".../AttributeEditor">
<property name="dateValue">
<value>2009-12-01</value>
</property>
</bean>
说明:属性编辑器类UtilDatePropertyEditor中,首先要继承PropertyEditorSupport,
该类在包java.bean.PropertyEditorSupport中.接着要覆盖类PropertyEditorSupport中的setAsText方法,该方法中就是实现类型转换的具体代码。因为setAsText没有返回值,所以把SImpleDateFormat的返回值传入到PopertyEditorSupport方法的setValue方法中.
编写完属性编辑器后,就要把该属性编辑器注入到IOC容器中,如下面代码:
<bean id="customEditorConfigurer"
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class=".../UtilDatePropertyEditor">
<!--设置时间格式-->
<property name="format" value="yyyy-MM-dd">
</bean>
</entry>
</map>
</property>
</bean>
首先要把自己编写的属性编辑器注入到org.springframework.beans.factory.config.CustomEditorConfigurer中,查看API文档,可以得到如下定义:
public class CustomEditorConfigurer() {
private Map customEditors;
public void setCustomEditors(Map customEditors) {
this.customEditors = customEditors;
}
public Map getCustomEditors() {
return customEditors;
}
}
从上面两段代码可以看出,属性编辑器被注入到CustomEditorConfigurer方法的customEditors中,同时要注意customEditors是Map集合类型.
分享到:
相关推荐
Spring 自定义注解注入properties文件的值jar包,下面为使用方法 在xml配置文件中,这样加载properties文件 ...
自定义依赖注入工具类SpringUtil.java
关于spring-集合注入、自定义转换器的实例代码,全,可以下载学习!
NULL 博文链接:https://jiawu.iteye.com/blog/400351
用springMVC+velocity时,当使用自定义标签,发现自定义标签无法注入spring的其它服务类,归根源码发现,velocity的源码中有一段,有一段代码永远获取的是新实例。所以,把这个地方的源码改了,所以使用这个的时候,...
自己实现spring注入和实现一个自定义的注入
Spring注解注入属性
自定义spring实现读取applicationContext.xml并依赖注入,可以参考学习控制反转的原理,非常简单。
SpringBoot中使用类型安全的配置来注入大量自定义属性示例源码
Spring 框架提供了多种方式来实现依赖注入,包括 getter 方法、setter 方法和自定义赋值方法。 A. getter 方法:通过 getter 方法来获取依赖对象。 B. setter 方法:通过 setter 方法来注入依赖对象,例如,在 ...
DynamicConfigRefresh:依赖Spring自定义注解完成配置文件注入和更改配置文件动态更新数据功能
自定义的ioc支持负责类型注入,对理解学习spring ioc 学习起到很好的作用!
ssh2框架整合,struts2和hibernate均交由spring管理,用注解的方式由spring注入
Spring注入Date类型的三种方法总结 测试Bean: public class DateBean { private Date birthday; public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this....
还了解依赖注入如何与Spring,IoC容器,具有@ Component,@ Configuration和@Bean的bean定义,注入点,与@ Primary,@ Qualifier和自定义注释进行歧义消除一起工作。 使用Spring概要文件,按概要文件配置...
主要介绍了SpringSecurity学习之自定义过滤器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
(3)byType:根据属性 类型 自动装配,相同类型多个会抛出异常,设值注入 <bean class="xxx" ></bean> (4)constructor:与 byType 方式类似,不同之处是构造注入 <bean class="xxx" ></bean> 本例,演示 byName...
spring4+cxf3,因为自己项目要用到接口开发,所以综合现有网上所有教程,终于成功写出能自动注入的demo,这个是含拦截器
1.19 Spring Boot使用Druid(编程注入) 1.20 Spring Boot普通类调用bean 1.21 使用模板(thymeleaf-freemarker) 1.22 Spring Boot 添加JSP支持 1.23 Spring Boot Servlet 1.24 Spring Boot过滤器、监听器 1.25 ...
6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...