@ -1207,6 +1207,271 @@ public interface FormatterRegistry {
@@ -1207,6 +1207,271 @@ public interface FormatterRegistry {
This applies the Formatter to the field, and overrides any Formatter that would have been applied by field type or annotation.
</para>
</section>
<sectionid="validation.beanvalidation">
<title>Spring 3 Validation</title>
<para>
Spring 3 introduces several enhancements to its validation support.
First, the JSR-303 Bean Validation API is now fully supported.
Second, when used programatically, Spring's DataBinder can now validate objects as well as bind to them.
Third, Spring MVC now has support for declaratively validating @Controller inputs.
</para>
<sectonid="validation.beanvalidation.overview">
<title>Overview of the Bean Validation API (JSR-303)</title>
<para>
The Bean Validation API (JSR-303) standardizes validation constraint declaration and metadata for the Java platform.
Using this API, you annotate domain model properties with declarative validation constraints and the runtime enforces them.
There are a number of built-in constraints you can can take advantage of.
You may also define your own custom constraints.
</para>
<para>
To illustrate, consider a simple Person model with two properties:
</para>
<programlistinglanguage="java"><![CDATA[
public class Person {
private String name;
private int age;
}]]>
</programlisting>
<para>
JSR-303 allows you to define declarative validation constraints against such properties:
</para>
<programlistinglanguage="java"><![CDATA[
public class Person {
@NotNull
@Max(64)
private String name;
@Min(0)
private int age;
}]]>
</programlisting>
<para>
When this object is processed by a JSR-303 Validator, these constraints will be validated.
</para>
<para>
For general information on JSR-303, see the <ulinkurl="http://jcp.org/en/jsr/detail?id=303">Bean Validation Specification</ulink>.
For information on the specific capabilities of the default reference implementation, see the <ulinkurl="https://www.hibernate.org/412.html">Hibernate Validator</ulink> documentation.
For how to setup a JSR-303 implementation as a Spring bean, keep reading.
</para>
</section>
<sectonid="validation.beanvalidation.spring">
<title>Configuring a Bean Validation Implementation</title>
<para>
Spring provides full support for the JSR-303 Bean Validation API.
This includes convenient support for bootstrapping a JSR-303 implementation as a Spring bean.
This allows a <code>javax.validation.Validator</code> to be injected wherever validation is needed.
</para>
<para>
Use the LocalValidatorFactoryBean to configure a default JSR-303 Validator as a Spring bean:
Each JSR-303 validation constraint consists of two parts.
First, a @Constraint annotation that declares the constraint and its configurable properties.
Second, an implementation of the <code>javax.validation.ConstraintValidator</code> interface that implements the constraint's behavior.
To associate a declaration with an implementation, each @Constraint annotation references its corresponding ValidationConstraint implementation class.
At runtime, the <code>ConstraintValidatorFactory</code> then creates instances of this class when the constraint annotation is encountered in your domain model.
</para>
<para>
The <code>LocalValidatorFactoryBean</code> automatically configures a <code>SpringConstraintValidatorFactory</code> that uses Spring to create ConstraintValidator instances.
This allows your custom ConstraintValidators to benefit from dependency injection like any other Spring bean.
</para>
<para>
Below is an example of a custom constraint declaration and implementation that uses Spring dependency injection:
<title>Configuring a JSR-303 Validator for use by Spring MVC</title>
<para>
With JSR-303, the default <code>javax.validation.Validator</code> implementation is quite generic.
A single instance typically coordinates the validation of <emphasis>all</emphasis> application objects that declare validation constraints.
To configure such a Validator for use by Spring MVC, simply inject a <code>LocalValidatorFactoryBean</code> reference into the <code>WebBindingInitializer</code> as shown in the previous section.
<code>LocalValidatorFactoryBean</code> already implements <code>org.springframework.validation.Validation</code>, delegating to the JSR-303 provider underneath.
</para>
<para>
A full configuration example showing injection of a JSR-303 backed Validator into Spring MVC is shown below: