(英文原版. Spring 3.2.14 Reference - 7 Validation, Data Binding, and Type Conversion)
(若有翻译不许确的地方,请私信我,共同提升;转载请注明出处,谢谢)html
对于把校验做为业务逻辑有正面意见也有负面意见,而Spring提供的验证设计(和数据绑定)不考虑这些。特别是,验证不该该依赖于Web层,它应该很容易被调用,能够插入任何可用的验证器。基于上述的考虑,Spring提供了一个Validator接口,这是一个基础组件,能够用于应用程序的每一层。前端
数据绑定机制容许将用户输入动态绑定到应用程序的域模型对象(或用来处理用户输入的任何对象)。Spring提供了DataBinder类实现数据绑定机制。java
Validator和DataBinder组成了validation包,主要但不限于在MVC framework中应用。BeanWrapper是Spring Framework中的一个基础组件,在不少地方都用到,可是开发者通常不会直接使用。由于这是一个参考文档,因此本章对BeanWrapper进行一些必要的解释。若是开发者想使用它,最多是在数据绑定场景下使用。web
Spring的DataBinder和底层的BeanWrapper都使用PropertyEditor来解析和格式化属性值。PropertyEditor是JavaBean所特有的,在本章也会进行必要的解释。Spring 3引入了“core.convert”包,既提供了一个通用类型转换工具,也提供了高级“format”包来格式化UI字段值。Spring中的这些新的程序包用起来可能比PropertyEditor简单,在本章也会讨论。spring
Spring 引入了Validator接口,能够用来验证对象。Validator接口与Errors对象一块儿使用,验证时Validator能够报告验证错误到Errors对象。
让咱们考虑一个简单数据对象。express
public class Person { private String name; private int age; // the usual getters and setters... }
经过实现org.springframework.validation.Validator接口的两个方法为Person类提供验证:编程
实现Validator比较简单,特别是借助Spring Framework提供的ValidationUtils帮助类时。api
public class PersonValidator implements Validator { /** * This Validator validates *just* Person instances */ public boolean supports(Class clazz) { return Person.class.equals(clazz); } public void validate(Object obj, Errors e) { ValidationUtils.rejectIfEmpty(e, "name", "name.empty"); Person p = (Person) obj; if (p.getAge() < 0) { e.rejectValue("age", "negativevalue"); } else if (p.getAge() > 110) { e.rejectValue("age", "too.darn.old"); } } }
正如你所看到的,若是name属性为null或者空字符串,ValidationUtils类的static rejectIfEmpty(..)方法会拒绝 name属性,并在Erros对象中返回验证失败。查阅ValidationUtils类的Javadoc文档,查看更多方法和使用。数组
开发者有可能会在一个Validator类中验证复杂对象(rich object)的每个内嵌对象,最好将每个内嵌对象的验证逻辑封装在各自的类实现中。咱们来看一个简单的“复杂对象”,Customer对象是由两个字符串(first name和second name)以及一个Address对象组成。Address对象与Custom对象是相互独立的,须要单独实现AddressValidator。若是开发者须要在CustomerValidator中重用AddressValidator中的验证逻辑,但不是经过复制粘贴代码的方式,可使用依赖注入(DI),或者在CustomerValidator中实例化一个AddressValidator,以下所示:spring-mvc
public class CustomerValidator implements Validator { private final Validator addressValidator; public CustomerValidator(Validator addressValidator) { if (addressValidator == null) { throw new IllegalArgumentException( "The supplied [Validator] is required and must not be null."); } if (!addressValidator.supports(Address.class)) { throw new IllegalArgumentException( "The supplied [Validator] must support the validation of [Address] instances."); } this.addressValidator = addressValidator; } /** * This Validator validates Customer instances, and any subclasses of Customer too * */ public boolean supports(Class clazz) { return Customer.class.isAssignableFrom(clazz); } public void validate(Object target, Errors errors) { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "field.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "surname", "field.required"); Customer customer = (Customer) target; try { errors.pushNestedPath("address"); ValidationUtils.invokeValidator(this.addressValidator, customer.getAddress(), errors); } finally { errors.popNestedPath(); } } } }
验证错误会报告给Errors对象,并传递给validator对象。若是是Spring Web MVC中使用,可使用
咱们讨论了数据绑定和验证。对应验证错误的消息输出是咱们须要讨论的最后一件事。在以前的例子中,咱们拒绝了name和age字段。若是咱们使用MessageSource输出错误消息,咱们会用到验证字段(name和age字段)错误时返回的错误码(error code)。当你调用(直接或者间接,使用ValidationUtils类)rejectValue或者Erros接口的其它reject方法,底层实现不会只注册传递过来的代码(code),还有一些额外的错误代码(error code)。注册哪些错误码是由使用的MessageCodesResolver决定的。默认使用DefaultMessageCodesResolver,它不只注册传递过来的代码的消息,还包括传递给reject方法的字段名称。所以当你使用rejectValue("age","to.darn.old")拒绝字段时,不光是too.darn.old代码,Spring还会注册too.darn.old.age和too.darn.old.age.int(第一个包含字段名称,第二个包含字段类型);这样作是为了方便开发者定位该类错误消息。
关于MessageCodesResolver更多信息和默认策略,能够分别查看MessageCodesResolver和DefaultMessageCodesResolver的在线Javadoc。
org.springframework.beans包由Sun提供,遵循JavaBean标准。JavaBean是一种简单的类,包含一个默认的无参构造函数,且遵循必定的命名规范。例如,若是有一个属性名为bingoMadness,必须包含一个赋值方法setBingoMadness(...),和一个获取属性值的方法getBingoMadness()。若是但愿获取更多的关于JavaBean的规范,请参考Sun网站(java.sun.com/products/javabeans)。
org.springframework.beans包中一个很是重要的类是BeanWrapper接口和它对应的实现类(BeanWrapperImpl)。正如在Javadoc中提到,BeanWrapper提供了一些功能,包括设置和获取属性值(单个属性或者组合属性),获取属性描述,查询属性是否可读或可写。同时,BeanWrapper提供嵌套属性的支持,能够对无限制深度的子属性进行设置。这样,不须要在目标类添加支持代码,BeanWrapper便可支持在目标类上添加标准JavaBean类JavaBeanPropertyChangeListeners和VetoableChangeListeners。不止如此,BeanWrapper还为索引属性提供设置支持。BeanWrapper一般不会在应用中直接使用,而是经过DataBinder和BeanFactory来使用。
从名称上能够看出BeanWrapper的工做机制,它封装了Bean,使得能够对Bean进行操做,例如设置和访问属性。
使用setPropertyValues(s)和getPropertyValue(s)方法设置和获取属性值,这些方法能够负载几个变量。这些在以后的Spring Javadoc文档中有更详细的描述。重要的是,表示一个对象的属性有一些规范。表7.1为属性表示样例。
表7.1 属性表示样例
属性 | 说明 |
---|---|
name | 表示属性name, 对应的方法为getName() 或 isName() ,以及 setName(..) |
account.name | 表示属性account的嵌套属性name, 对应的方法为getAccount().setName() 或者getAccount().getName() |
account[2] | 表示索引属性account的第3个元素。索引属性可使array、list或者天然排序的collection。 |
account[COMPANYNAME] | 表示Map类型的属性account中key为COMPANYNAME 的Map条目的value。 |
使用BeanWrapper设置和获取属性值的样例以下:
考虑如下两个类:
public class Company { private String name; private Employee managingDirector; public String getName() { returnthis.name; } public void setName(String name) { this.name = name; } public Employee getManagingDirector() { returnthis.managingDirector; } public void setManagingDirector(Employee managingDirector) { this.managingDirector = managingDirector; } } public class Employee { private String name; private float salary; public String getName() { return this.name; } public void setName(String name) { this.name = name; } public float getSalary() { return salary; } public void setSalary(float salary) { this.salary = salary; } }
下面的代码片断显示了如何获取和处理companies和employees对象的一些属性。
BeanWrapper company = BeanWrapperImpl(new Company()); // 设置company的name属性.. company.setPropertyValue("name", "Some Company Inc."); // ... 也能够这样设置: PropertyValue value = new PropertyValue("name", "Some Company Inc."); company.setPropertyValue(value); // 建立一个employee对象,赋值给managingDirector: BeanWrapper jim = BeanWrapperImpl(new Employee()); jim.setPropertyValue("name", "Jim Stravinsky"); company.setPropertyValue("managingDirector", jim.getWrappedInstance()); // 获取company对象的managingDirector属性的salary属性(嵌套属性) Float salary = (Float) company.getPropertyValue("managingDirector.salary");
Spring使用PropertyEditor来处理Object和String之间的转换。有时候使用PropertyEditor比使用对象自己能够更方便的表示属性。例如,能够用易读的方式(如“2007-14-09”)表示Date,也能够将其转换回原始Date对象(更进一步:将任意易读的时间格式转换为Date对象),而这种效果经过注册java.beans.PropertyEditor类型的自定义editor就能够实现。在BeanWrapper或IoC容器上注册自定义editor,能够转换对象的属性为指定的类型。若是须要获取PropertyEditor更多的信息,能够阅读由Sun提供的java.beans包的Javadoc文档。
Spring中使用属性编辑的两个例子:
表7.2. 内置的PropertyEditor清单
类 | 说明 |
---|---|
ByteArrayPropertyEditor | 将字符串转换为字节数组(byte[])。BeanWrapper中已经默认注册。 |
ClassEditor | 将字符串类名转换为java.lang.Class。当找不到对应的类时,抛出ellegalArgumentException异常。BeanWrapper中已经默认注册。 |
CustomBooleanEditor | Boolean类型的自定义属性编辑器, BeanWrapper中已经默认注册。能够注册自定义的实例覆盖原有实例。 |
CustomCollectionEditor | Collection类型的自定义属性编辑器,将Collection
|
CustomDateEditor | java.util.Date 类型的自定义属性编辑器,支持自定义DateFormat,BeanWrapper中没有默认注册,须要开发者自行注册。 |
CustomNumberEditor | Integer, Long, Float, Double等Number类型的自定义属性编辑器BeanWrapper中已经默认注册。能够注册自定义的实例覆盖原有实例。 |
FileEditor | java.io.File 类型的属性编辑器,解析字符串为File对象。BeanWrapper中已经默认注册。 |
InputStreamEditor | InputStream类型的单向属性编辑器,输入字符串,输出InputStream对象(经过ResourceEditor和Resource)。注意默认实现中Spring没有关闭InputStream,须要由开发者自行调用关闭。BeanWrapper中已经默认注册。 |
LocaleEditor | java.utils.Locale类型的属性编辑器。解析字符串为Locale对象,反之亦然。String字符串格式为[language][country][variant],与Locale的toString()方法同样。BeanWrapper中已经默认注册。 |
PatternEditor | JDK 1.5 Pattern类型的属性编辑器。解析字符串为Pattern对象,反之亦然。 |
PropertiesEditor | java.lang.Properties 类型的属性编辑器。解析字符串为Properties对象。String格式遵循java.lang.Properties的Javadoc定义。BeanWrapper中已经默认注册。 |
StringTrimmerEditor | 用于trim 字符串。开发者能够选择是否将空字符串转换为null。BeanWrapper中没有默认注册,须要开发者自行注册。 |
URLEditor | URL类型的属性编辑器。解析字符串为URL对象。BeanWrapper中已经默认注册。 |
Spring使用java.beans.PropertyEditorManager来设置所须要的PropertyEditor实现类的查找路径。查找路径中包括sun.bean.editors,包含了Font,Color类型以及大多数的基本类型的PropertyEditor接口实现。同时注意,若是PropertyEditor类文件和属性的类文件在同一个包下面,且PropertyEditor实现类类名为属性类名加Editor后缀,那么标准JavaBeans框架会自动发现PropertyEditor类(不须要显式注册)。例如,下面这个包结构和类,FooEditor会被默认认为是Foo类型的PropertyEditor属性编辑器。
com chank pop Foo FooEditor // the PropertyEditor for the Foo class
注意也可使用标准BeanInfo JavaBeans机制。下面是使用BeanInfo机制的例子,显式注册了一个或者多个PropertyEditor实例处理对应类的属性。
com chank pop Foo FooBeanInfo // the BeanInfo for the Foo class
下面是FooBeanInfo类的Java 源码参考。这里关联了一个处理age属性的CustomNumberEditor。
public class FooBeanInfo extends SimpleBeanInfo { public PropertyDescriptor[] getPropertyDescriptors() { try { final PropertyEditor numberPE = new CustomNumberEditor(Integer.class, true); PropertyDescriptor ageDescriptor = new PropertyDescriptor("age", Foo.class) { public PropertyEditor createPropertyEditor(Object bean) { return numberPE; }; }; return new PropertyDescriptor[] { ageDescriptor }; } catch (IntrospectionException ex) { throw new Error(ex.toString()); } } }
当为Bean的属性赋值为字符串时,Spring IoC容器最终会使用标准JavaBeans PropertyEditor将其转换为复杂类型。Spring预先注册了一些内置的PropertyEditor(例如,将用字符串表示的类名转换为实际的Class对象)。另外,Java的标准JavaBeans PropertyEditor查找机制容许对PropertyEditor进行简单适当的命名,而后与它支持的类放在同一个包下,PropertyEditor将被自动发现。
若是须要注册其它自定义的PropertyEditor,有如下几种方法。最经常使用的手动方法(不方便也不推荐),若是有一个BeanFactory实现类,可使用ConfigurableBeanFactory接口的registerCustomEditor()方法,。另外一个比较简便的方法,是使用CustomEditorConfigurer这个特殊的bean工厂后置处理器。虽然bean工厂后置处理器能够与BeanFactory实现类一块儿使用,可是CustomEditorConfigurer存在一个嵌套属性设置,因此强烈建议在ApplicationContext中使用,能够用相似的方式部署给其它Bean,会被自动检测和应用。
注意全部的bean工厂和应用程序上下文自动使用一些内置属性编辑器,经过使用BeanWrapper处理属性值的类型转换。上一章节罗列了BeanWrapper注册的标准的属性编辑器。另外,ApplicationConext也覆盖或者增长了额外的编辑器来处理资源查找(resource lookup)问题,以适合特定的应用上下文类型。
PropertyEditor实例用来转换字符串表示的属性值为实际的属性类型。能够经过CustomEditorConfigurer向ApplicationContext方便的添加额外的PropertyEditor实例。
考虑一个用户类ExoticType,以及DependsOnExoticType类,后者具备一个ExoticType的属性须要设置。
package example; public class ExoticType { private String name; public ExoticType(String name) { this.name = name; } } public class DependsOnExoticType { private ExoticType type; public void setType(ExoticType type) { this.type = type; } }
当设置完成后,咱们但愿可以将字符串赋值给该属性,由PropertyEditor完成字符串到ExoticType实例的转换。
<bean id="sample" class="example.DependsOnExoticType"> <property name="type" value="aNameForExoticType"/> </bean>
PropertyEditor的实现可能相似这样:
// 转换字符串为ExoticType; public class ExoticTypeEditor extends PropertyEditorSupport { public void setAsText(String text) { setValue(new ExoticType(text.toUpperCase())); } }
最后,咱们使用CustomEditorConfigurer在ApplicationContext中注册新的PropertyEditor,以即可以在须要的时候使用。
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="example.ExoticType" value="example.ExoticTypeEditor"/> </map> </property> </bean>
另外一个在Spring容器中注册属性编辑器(property editor)的机制是建立和使用PropertyEditorRegistrar接口。当须要在不一样场景下重复使用一组属性编辑器时比较有效,只须要写一个registrar,重用便可。
PropertyEditorRegistrars与PropertyEditorRegistry接口联合使用,这个接口由Spring BeanWrapper(和DataBinder)实现。
PropertyEditorRegistrars在与CustomEditorConfigurer联合使用时特别方便(在这里有介绍),CustomEditorConfigurer接口包含一个设置PropertyEditorRegistrars的方法setPropertyEditorRegistrars(..):添加的PropertyEditorRegistrars可以很容易的共享给DataBinder和Spring MVC Controller。此外,再也不须要同步自定义编辑器:每一次尝试建立bean时,PropertyEditorRegistrar会建立一个新的PropertyEditor实例。
最好用一个例子来讲明PropertyEditorRegistrars。首先,你须要建立本身的PropertyEditorRegistrar实现:
package com.foo.editors.spring; public final class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar { public void registerCustomEditors(PropertyEditorRegistry registry) { // it is expected that new PropertyEditor instances are created registry.registerCustomEditor(ExoticType.class, new ExoticTypeEditor()); // you could register as many custom property editors as are required here... } }
也能够将org.springframework.beans.support.ResourceEditorRegistrar做为一个实现PropertyEditorRegistrar的例子。注意它的registerCustomEditors(...)方法是如何建立每个属性编辑器的。
下一步咱们配置CustomEditorConfigurer,注入CustomPropertyEditorRegistrars实例。
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="propertyEditorRegistrars"> <list> <ref bean="customPropertyEditorRegistrar"/> </list> </property> </bean> <bean id="customPropertyEditorRegistrar" class="com.foo.editors.spring.CustomPropertyEditorRegistrar"/>
最后,稍微离开一点本章节的焦点,当你使用Spring MVC web框架时,与数据绑定控制器(例如SimpleFormController)联合使用会很是方便。看下面在initBinder(..)方法中使用 PropertyEditorRegistrar的例子。
public final class RegisterUserController extends SimpleFormController { private final PropertyEditorRegistrar customPropertyEditorRegistrar; public RegisterUserController(PropertyEditorRegistrar propertyEditorRegistrar) { this.customPropertyEditorRegistrar = propertyEditorRegistrar; } protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { this.customPropertyEditorRegistrar.registerCustomEditors(binder); } // other methods to do with registering a User }
PropertyEditor注册将会比较简洁(initBinder()方法中只有一行),而且容许经常使用的PropertyEditor注册代码放在类中封装,而后共享给须要的控制器(Controller)。
Spring 3 引入了core.convert包,提供通用的类型转换系统。这个系统定义了一个SPI(Service Provide API)来实现类型转换逻辑,相似在运行时执行类型转换的API。在Spring容器中,该系统能够做为PropertyEditor的替代品来转换外部bean属性的字符串值为须要的属性类型。这个公共的API能够用在应用程序任何须要类型转换的地方。
用来实现类型转换逻辑的SPI定义很简单可是颇有效:
package org.springframework.core.convert.converter public interface Converter<S,T>{ public <T> convert(S source); }
建立Converter,实现上述接口便可。S为转换的源类型,T为转换的目标类型。每次调用convert(S),要保证S不能为null。若是转换失败,Converter可能抛出异常。 抛出illegalArgumentException异常,表示源类型无效。请保证Converter的实现类是线程安全的。
为方便起见,core.convert.converter包提供了一些Converter的实现类,包括String转Number和其它常见类型。咱们来看下面的StringToInteger的样例:
package org.springframework.core.convert.support; final class StringToInteger implements Converter<String, Integer> { public Integer convert(String source) { return Integer.valueOf(source); } }
当须要为整个类层次结构集中类型转换时,例如,当将String转换为java.lang.Enum对象时,实现ConverterFactory:
package org.springframework.core.convert.converter public interface ConverterFactory<S,R>{ <T extends R> Converter<S,T> getConverter(Class<T> targetType); }
S为源类型,R为一系列目标类型的基类。实现getConverter(Class
下面是StringToEnum ConvertFactory的样例:
package org.springframework.core.convert.support; final class StringToEnumConverterFactory implements ConverterFactory<String, Enum> { public <T extends Enum> Converter<String, T> getConverter(Class<T> targetType) { return new StringToEnumConverter(targetType); } private final class StringToEnumConverter<T extends Enum> implements Converter<String, T> { private Class<T> enumType; public StringToEnumConverter(Class<T> enumType) { this.enumType = enumType; } public T convert(String source) { return (T) Enum.valueOf(this.enumType, source.trim()); } } }
当须要实现一个比较复杂的Converter,能够考虑GenericConverter接口。更灵活,可是不是强类型,GenericConverter支持多种源类型和目标类型之间的转换。另外,当实现转换逻辑时,可使用GenericConverter的源和目标字段上下文。这些上下文容许注解驱动的类型转换,或者在字段上声明的通用信息。
实现GenericConverter接口,须要实现getConvertibleTypes()方法,返回支持的源和目的类型集合。须要实现convert(Object, TypeDescriptor, TypeDescriptor)方法,在方法中实现转换逻辑。其中,3个参数依次表示待转换的源、源类型,目标类型,返回值为转换后的值。使用GenericConverter接口的一个比较好的例子是在Java Array和Collection之间转换。ArrayToCollection内省(introspect)声明Collection类型的字段来解析Collection的元素类型,实如今返回Collection对象以前Array中的每个元素都已经转换为Collection中元素的类型。
注意:GenericConverter是一个很复杂的SPI接口,只有当你须要它的时候再使用。处理基本类型转换时,使用Favor Converter或者ConverterFactory便可。
有时候,你可能但愿在某些条件下才执行类型转换。例如,你可能在只有当注解出如今目标字段上的时候才执行类型转换。或者,你可能在只有目标类包含一个静态方法ValueOf()的时候才能执行 类型转换。
public interface ConditionalGenericConverter extends GenericConverter { boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType); }
使用ConditionGenericConverter接口的一个比较好的例子是EntityConverter,用于转换持久化实体ID和实体引用。只有当实体类中声明了一个静态finder方法,如findAccount (Long),EntityConverter matches(TypeDescriptor,TypeDescriptor)方法才会返回true。在实现matches(TypeDescriptor,TypeDescriptor )时,须要检查finder方法是否存在。
ConversionService定义了一个统一的API,用来在运行时执行类型转换逻辑。一般,Converter在下面facade接口后面执行。
package org.springframework.core.convert; public interface ConversionService { boolean canConvert(Class<?> sourceType, Class<?> targetType); <T> T convert(Object source, Class<T> targetType); boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType); Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType); }
大多数ConversionService实现类实现了ConverterRegistry,它提供SPI来注册converter。在内部,ConversionService实现类代理已注册的转换器(converter)处理类型转换逻辑。
core.convert.support包提供了强壮的ConversionService实现。GenericConvertionService适合大多数环境下的通用实现。ConversionServiceFactory是ConversionService的工厂类,用来建立ConversionService配置。
ConversionService是一个无状态的对象,设计为应用程序启动时被加载,而后在多个线程间共享。在Spring应用程序中,你能够为每一个Spring容器(或者ApplicationContext)配置一个ConversionService。该ConversionService会被Spring加载,而后在须要类型转换的时候使用。你能够注入ConversionService到任何一个bean直接调用它。
注意,若是ConversionService没有在Spring注册,那么原始的基于PropertyEditor的系统会被使用。
为了在Spring中注入默认的ConversionService,在XML文件中增长下面的bean,id为conversionService。
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"/>
默认的ConversionService能够转换String,Numbers,enums,Collections,maps和其它经常使用的类型。为了支持或者用自定义的转换器(converter)覆盖默认的转换器,能够设置converters属性,属性值能够被Converter,ConverterFactory或者GenericConverter接口实现。
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <list> <bean class="example.MyCustomConverter"/> </list> </property> </bean>
Spring MVC中也常用ConversionService。7.6.5查看该部分详细内容。
在一些状况下,可能但愿在类型转换的时候进行格式转换。7.6.3查看该部分详细内容。
经过编程使用ConversionService,在你想要的任何bean的属性中注入ConversionService。
@Service public class MyService { @Autowired public MyService(ConversionService conversionService) { this.conversionService = conversionService; } public void doIt() { this.conversionService.convert(...) } }
在以前章节里讨论过,core.convert包是一个通用的类型转换系统。它提供了一个统一的转换服务API(ConversionService API),相似强类型Converter SPI,实现了从一个类型到另外一个类型的转换逻辑。Spring容器(Spring Container)使用这个系统绑定Bean属性值。另外,Spring Expression Language(SpEL)和DataBinder使用这个系统绑定字段值。例如,当调用expression.setValue(Object bean, Object value) SpEL表达式,须要尝试将Short类型强制转换为Long类型时,其强制转换工做就是由core.convert完成的。
如今考虑典型的客户端环境下的类型转换需求,如web应用或者桌面应用。在这种环境下,经常须要将String类型进行转换来支持客户端的POST请求,以及转换为String类型来支持前端视图渲染。另外,经常须要对String类型的值进行本地化。core.convert Converter SPI并不直接响应这样的格式化。为了直接响应,Spring 3为客户端环境引入了一个方便的Formatter SPI,给PropertyEditor提供了一个简单好用的替代品。
通常而言,当须要实现通用类型转换逻辑时,使用Converter SPI,例如须要在java.util.Date和java.lang.Long之间转换时。当在客户端环境时,使用Formatter SPI。例如Web应用,或者须要解析和打印本地化的字段值。ConversionService为这两种SPI提供一个统一的转换API接口。
Formatter SPI实现字段格式化很简单,且为强类型。
package org.springframework.format; public interface Formatter<T> extends Printer<T>, Parser<T> { }
Formatter SPI继承了Printer
public interface Printer<T> { String print(T fieldValue, Locale locale); } import java.text.ParseException; public interface Parser<T> { T parse(String clientValue, Locale locale) throws ParseException; }
建立自定义的Formatter时,实现Formatter接口便可。实例化T为被格式化的对象类型,例如java.util.Date。实现print()方法打印T实例,用于客户端本地显示;实现parse()方法解析T实例,用于接收从客户端传递过来的格式化后的表达式。当解析失败后,Formatter实现类应该抛出ParseException或者IllegalArgumentException异常。注意,须保证Formatter实现类线程安全。
为方便起见,在format子包中实现了部分Formatter。number包提供了NumberFormatter,CurrencyFormatter和PercentFormatter,使用java.text.NumberFormat来格式化java.lang.Number对象。datetime包提供了DateFormatter,使用java.text.DateFormat来格式化java.util.Date对象。基于Joda Time库,datetime.joda包提供了更为复杂的datetime格式化支持。
以DateFormatter为例,来看Formatter接口的实现:
package org.springframework.format.datetime; public final class DateFormatter implements Formatter<Date> { private String pattern; public DateFormatter(String pattern) { this.pattern = pattern; } public String print(Date date, Locale locale) { if (date == null) { return ""; } return getDateFormat(locale).format(date); } public Date parse(String formatted, Locale locale) throws ParseException { if (formatted.length() == 0) { return null; } return getDateFormat(locale).parse(formatted); } protected DateFormat getDateFormat(Locale locale) { DateFormat dateFormat = new SimpleDateFormat(this.pattern, locale); dateFormat.setLenient(false); return dateFormat; } }
正如您将看到,字段格式化能够经过字段类型或者注解(annotation)来配置。为了绑定注解到一个formatter,须要实现AnnotationFormatterFactory工厂接口。
package org.springframework.format; public interface AnnotationFormatterFactory<A extends Annotation> { Set<Class<?>> getFieldTypes(); Printer<?> getPrinter(A annotation, Class<?> fieldType); Parser<?> getParser(A annotation, Class<?> fieldType); }
实例化泛型A为格式化逻辑相关联的字段注解类型,例如
org.springframework.format.annotation.DateTimeFormat。
实现getFieldTypes(),返回注解能够应用到的字段类型;实现getPrinter(),返回可以为注解字段打印字段值的Printer;实现getParser(),返回可以为注解字段解析客户端上传数据(clientValue)的Parser。
public final class NumberFormatAnnotationFormatterFactory implements AnnotationFormatterFactory<NumberFormat> { public Set<Class<?>> getFieldTypes() { return new HashSet<Class<?>>(asList(new Class<?>[] { Short.class, Integer.class, Long.class, Float.class, Double.class, BigDecimal.class, BigInteger.class })); } public Printer<Number> getPrinter(NumberFormat annotation, Class<?> fieldType) { return configureFormatterFrom(annotation, fieldType); } public Parser<Number> getParser(NumberFormat annotation, Class<?> fieldType) { return configureFormatterFrom(annotation, fieldType); } private Formatter<Number> configureFormatterFrom(NumberFormat annotation, Class<?> fieldType) { if (!annotation.pattern().isEmpty()) { return new NumberFormatter(annotation.pattern()); } else { Style style = annotation.style(); if (style == Style.PERCENT) { return new PercentFormatter(); } else if (style == Style.CURRENCY) { return new CurrencyFormatter(); } else { return new NumberFormatter(); } } } }
为了使用格式化,用@NumberFormat注解相应的字段。
public class MyModel { @NumberFormat(style=Style.CURRENCY) private BigDecimal decimal; }
格式化注解API(Format Annotation API)位于org.springframework.format.annotation包中。使用@NumberFormat来格式化java.lang.Number字段;使用@DateTimeFormat来格式化java.util.Date,java.util.Calendar,java.util.Long或者Joda Time字段。
下面的例子使用@DateTimeFormat来格式化java.util.Date为ISO Date格式(yyyy-MM-dd):
public class MyModel { @DateTimeFormat(iso=ISO.DATE) private Date date; }
FormatterRegistry SPI用来注册formatter和converter。
FormattingConversionService是FormatterRegistry的一个实现类,适用于绝大多数环境,经过代码编程或者声明FormattingConversionServiceFactoryBean为Spring Bean来引入。这个类同时实现了ConversionService接口,所以配置后能够被Spring的DataBinder和SpEL直接调用。
看一下FormatterRegistry SPI的代码:
package org.springframework.format; public interface FormatterRegistry extends ConverterRegistry { void addFormatterForFieldType(Class<?> fieldType, Printer<?> printer, Parser<?> parser); void addFormatterForFieldType(Class<?> fieldType, Formatter<?> formatter); void addFormatterForFieldType(Formatter<?> formatter); void addFormatterForAnnotation(AnnotationFormatterFactory<?, ?> factory); }
正如上面显示,能够经过fieldType或者注解来注册Formatter。
FormatterRegistry SPI容许开发者集中配置格式化(Formatting)规则,而不是在控制器(Controller)中重复这些配置。例如,若是但愿按照某种规则对全部的Date字段进行格式化,或者按照某种规则对注解字段进行格式化,能够经过共享FormatterRegistry,实现“一次定义,按需应用”。
FormatterRegistrar SPI用来利用FormatterRegistry注册formatter和converter。
package org.springframework.format; public interface FormatterRegistrar { void registerFormatters(FormatterRegistry registry); }
在为给定的一组分类注册多个相关的converter和formatter时,FormatterRegistrar比较有用,例如Date格式化。一样在声明注册不足以表示时比较有用。例如,当formatter须要在不一样于它自 己的泛型
Spring MVC应用程序中,开发者能够显式配置一个自定义的ConversionService实例,做为
为了使用默认的格式化规则,不在Spring MVC配置文件中添加自定义配置便可。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven/> </beans>
只须要配置一行
若是使用自定义的formatter和converter,能够注入自定义的ConversionService实例。在Spring配置文件中设置conversion-service属性,类型为 FormatteringConversionServiceFactoryBean,设置自定义的converter、formatter或者FormatterRegistrar做为conversion-service的属性。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven conversion-service="conversionService"/> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="org.example.MyConverter"/> </set> </property> <property name="formatters"> <set> <bean class="org.example.MyFormatter"/> <bean class="org.example.MyAnnotationFormatterFactory"/> </set> </property> <property name="formatterRegistrars"> <set> <bean class="org.example.MyFormatterRegistrar"/> </set> </property> </bean> </beans>
没有被@DateTimeFormat注解的date和time字段默认使用DateFormat.SHORT全局样式。开发者能够按照本身的需求从新定义全局格式。
Spring没有注册默认的formatter,你须要确保手动注册全部的formatter。使用org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar
仍是org.springframework.format.datetime.DateFormatterRegistrar类,取决你是否使用了Joda Time库。
例以下面的Java配置会注册一个'yyyyMMdd'的全局格式,不依赖Joda Time库。
@Configuration public class AppConfig { @Bean public FormattingConversionService conversionService() { // Use the DefaultFormattingConversionService but do not register defaults DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(false); // Ensure @NumberFormat is still supported conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory()); // Register date conversion with a specific global format DateFormatterRegistrar registrar = new DateFormatterRegistrar(); registrar.setFormatter(new DateFormatter("yyyyMMdd")); registrar.registerFormatters(conversionService); return conversionService; } }
若是你倾向于基于XML的配置,你可使用FormattingConversionServiceFactoryBean。下面是相同的样例,使用Joda Time。
<?xml version="1.0" encoding="UTF-8"?><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.xsd> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="registerDefaultFormatters" value="false" /> <property name="formatters"> <set> <bean class="org.springframework.format.number.NumberFormatAnnotationFormatterFactory" /> </set> </property> <property name="formatterRegistrars"> <set> <bean class="org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar"> <property name="dateFormatter"> <bean class="org.springframework.format.datetime.joda.DateTimeFormatterFactoryBean"> <property name="pattern" value="yyyyMMdd"/> </bean> </property> </bean> </set> </property> </bean> </beans>
在Spring MVC中应用的状况下,要显式配置conversion service。若是使用基于Java的@Configuration注解,须要扩展WebMvcConfigurationSupport类,覆盖mvcConversionService()方法。若是使用XML配置文件,须要使用'mvc:annotation-driven'元素的'conversion-service'属性。查阅7.6.5章节“在Spring MVC中配置格式化”获取更详细的信息。
Spring 3 加强了验证支持库。第一,彻底支持JSR-303 Bean Validation API;第二,当使用代码方式时,Spring的DataBinder除了绑定对象,还能够验证对象。第三,Spring MVC如今已经支持声明式验证@Controller输入。
JSR-303 为Java平台验证定义了验证约束声明和元数据模型。使用该API,经过声明式验证约束来注解域模型(Domain Model)属性,运行时平台会强制执行。可使用API中的内置约束,也能够定义本身的约束。
下面用一个简单的包含两个属性的PersonForm模型演示说明。
public class PersonForm { private String name; private int age; }
JSR-303容许对这些属性定义声明式验证约束。
public class PersonForm { @NotNull @Size(max=64) private String name; @Min(0) private int age; }
当该类的实例被JSR-303 Validator验证时,这些约束就会执行。
若是须要了解JSR-303的更多信息,请浏览Bean Validation Specification(Bean验证规范)。若是须要了解默认参考实现,请浏览Hibernate Validator(Hibernate验证器)。学习如何将JSR-303实现框架设置为Spring Bean,请阅读下面章节。
Spring彻底支持JSR-303 Bean Validation API。这包括支持方便的注入JSR-303实现类为Spring Bean,能够按需将javax.validation.ValidatorFactory或者javax.validation.Validator注入到应用程序中。
使用LocalValidatorFactoryBean来配置一个默认的JSR-303实现类做为Spring Bean。
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
以上的基本配置会使用默认的引导机制触发JSR-303初始化。将JSR-303提供者(例如Hibernate Validation)的库文件加入classpath,它会被自动检测。
LocalValidatorFactoryBean实现了javax.validation.ValidatorFactory接口和javax.validation.Validator接口,以及Spring的org.springframework.validation.Validator接口。能够将上述任意一个接口注入到须要验证的Bean中,来启动验证逻辑。
若是你但愿直接使用JSR-303 API,注入javax.validation.Validator。
import javax.validation.Validator; @Service public class MyService { @Autowired private Validator validator;
若是你的Bean须要使用Spring Validator API,注入org.springframework.validation.Validator
import org.springframework.validation.Validator;
@Service public class MyService { @Autowired private Validator validator; }
每一个JSR-303验证约束包含两部分。第一部分,@Constraint注解,声明约束,以及可配置的属性。第二部分,javax.validation.ConstraintValidator接口的实现,实现约束行为。为了将实现与声明关联起来,每个@Constraint注解指向一个对应的ValidationConstraint实现类。在运行时,当在域模型中遇到约束注解后,ConstraintValidatorFactory就实例化一个ConstraintValidator。
默认LocalValidatorFactoryBean配置一个SpringConstraintValidatorFactory,使用Spring建立ConstraintValidator实例。与普通Spring Bean同样,能够在自定义的ConstraintValidator中使用依赖注入。
下面是一个自定义的@Constraint声明,后面跟着相关联的ConstraintValidator实现,使用Spring进行依赖注入。
@Target({ElementType.METHOD, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy=MyConstraintValidator.class) public @interface MyConstraint { } import javax.validation.ConstraintValidator; public class MyConstraintValidator implements ConstraintValidator { @Autowired; private Foo aDependency; ... }
正如你看到的,ConstraintValidator实现类中能够像其它Spring Bean同样使用Spring的依赖注入(@Autowired关键字)。
默认的LocaValidatorFactoryBean配置对于绝大多数状况是足够的。不一样的JSR-303构造器还有一些其它配置项,如message interpolation和traversal resolution。对于这些选项的具体意义,能够查看LocalValidatorFactoryBean 的JavaDoc。
从Spring 3开始,DataBinder实例能够配置一个Validator。一旦配置,Validator能够经过binder.validate()触发。任何验证错误都会自动添加到binder的BindingResult。
当使用代码方式使用DataBinder时,在绑定一个目标对象后,能够触发验证逻辑。
Foo target = new Foo(); DataBinder binder = new DataBinder(target); binder.setValidator(new FooValidator()); // bind to the target object binder.bind(propertyValues); // validate the target object binder.validate(); // get BindingResult that includes any validation errors BindingResult results = binder.getBindingResult();
经过dataBinder.addValidators和dataBinder.replaceValidators,DataBinder能够配置多个Validator实例。对DataBinder实例中配置的局部Spring Validator和已配置的全局JSR-303 Bean Validator进行合并时,颇有用。浏览“经过Spring MVC配置Validator”章节。
由Spring 3开始,Spring MVC能够自动验证@Controller输入。在以前的版本,须要开发者手动触发验证逻辑。
为了触发@Controller输入验证,为输入参数配置注解,@Valid:
@Controller public class MyController { @RequestMapping("/foo", method=RequestMethod.POST) public void processFoo(@Valid Foo foo) { /* ... */ }
在绑定以后,Spring MVC会验证@Valid对象,由于一个合适的Validator已经被配置。
注意:@Valid注解是标准JSR-303 Bean Validation API中的一部分,不是Spring特有的。
当@Valid方法参数配置完成后,Validator实例能够经过两种方式配置。第一种,在@Controller的@InitBinder回调中调用binder.setValidator(Validator);这样作能够为每个@Controller类配置一个 Validator实例。
@Controller public class MyController { @InitBinder protected void initBinder(WebDataBinder binder) { binder.setValidator(new FooValidator()); } @RequestMapping("/foo", method=RequestMethod.POST) public void processFoo(@Valid Foo foo) { ... } }
第二种,能够在全局WebBindingInitializer中调用setValidator(Validator),能够为全部@Controller配置一个Validator实例。可使用Spring MVC namespace很方便的配置。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven validator="globalValidator"/> </beans>
同时应用全局和局部验证器(validator),按上一步骤配置全局Validator,而后按下面步骤配置局部Validator。
@Controller public class MyController { @InitBinder protected void initBinder(WebDataBinder binder) { binder.addValidators(new FooValidator()); } }
经过JSR-303,一个javax.validation.Validator实例能够验证全部声明验证约束的模型对象。只须要在classpath中增长JSR-303 Provider的包,例如Hibernate Validator,Spring MVC就会检测到,并自动为全部Controller提供JSR-303支持。
Spring MVC配置支持JSR-303以下所示:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- JSR-303 support will be detected on classpath and enabled automatically --> <mvc:annotation-driven/> </beans>
使用最少配置,当遇到@Valid @Controller输入时,由JSR-303 provider进行验证。反过来,JSR-303会强制对声明验证的输入进行约束。当BindingResult中有错误时,ConstraintValidation会自动显示,经过标准的Spring MVC 表单标签渲染。