public class LocalValidatorFactoryBean extends SpringValidatorAdapter implements ValidatorFactory, ApplicationContextAware, org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.DisposableBean
javax.validation (JSR-303) setup in a Spring
application context: It bootstraps a javax.validation.ValidationFactory and
exposes it through the Spring Validator interface
as well as through the JSR-303 Validator interface and the
ValidatorFactory interface itself.
When talking to an instance of this bean through the Spring or JSR-303 Validator interfaces,
you'll be talking to the default Validator of the underlying ValidatorFactory. This is very
convenient in that you don't have to perform yet another call on the factory, assuming that
you will almost always use the default Validator anyway. This can also be injected directly
into any target dependency of type Validator!
As of Spring 4.0, this class supports Bean Validation 1.0 and 1.1, with special support
for Hibernate Validator 4.3 and 5.x (see setValidationMessageSource(org.springframework.context.MessageSource)).
Note that Bean Validation 1.1's #forExecutables method isn't supported: We do not
expect that method to be called by application code; consider MethodValidationInterceptor
instead. If you really need programmatic #forExecutables access, inject this class as
a ValidatorFactory and call getValidator() on it, then #forExecutables
on the returned native Validator reference instead of directly on this class.
Alternatively, call #unwrap(Validator.class) which will also provide the native object.
This class is also being used by Spring's MVC configuration namespace, in case of the
javax.validation API being present but no explicit Validator having been configured.
ValidatorFactory,
Validator,
Validation.buildDefaultValidatorFactory(),
ValidatorFactory.getValidator()| Constructor and Description |
|---|
LocalValidatorFactoryBean() |
| Modifier and Type | Method and Description |
|---|---|
void |
afterPropertiesSet() |
void |
close() |
void |
destroy() |
ConstraintValidatorFactory |
getConstraintValidatorFactory() |
MessageInterpolator |
getMessageInterpolator() |
TraversableResolver |
getTraversableResolver() |
java.util.Map<java.lang.String,java.lang.String> |
getValidationPropertyMap()
Allow Map access to the bean validation properties to be passed to the validation provider,
with the option to add or override specific entries.
|
Validator |
getValidator() |
protected void |
postProcessConfiguration(Configuration<?> configuration)
Post-process the given Bean Validation configuration,
adding to or overriding any of its settings.
|
void |
setApplicationContext(ApplicationContext applicationContext)
Set the ApplicationContext that this object runs in.
|
void |
setConstraintValidatorFactory(ConstraintValidatorFactory constraintValidatorFactory)
Specify a custom ConstraintValidatorFactory to use for this ValidatorFactory.
|
void |
setMappingLocations(org.springframework.core.io.Resource... mappingLocations)
Specify resource locations to load XML constraint mapping files from, if any.
|
void |
setMessageInterpolator(MessageInterpolator messageInterpolator)
Specify a custom MessageInterpolator to use for this ValidatorFactory
and its exposed default Validator.
|
void |
setParameterNameDiscoverer(org.springframework.core.ParameterNameDiscoverer parameterNameDiscoverer)
Set the ParameterNameDiscoverer to use for resolving method and constructor
parameter names if needed for message interpolation.
|
void |
setProviderClass(java.lang.Class providerClass)
Specify the desired provider class, if any.
|
void |
setTraversableResolver(TraversableResolver traversableResolver)
Specify a custom TraversableResolver to use for this ValidatorFactory
and its exposed default Validator.
|
void |
setValidationMessageSource(MessageSource messageSource)
Specify a custom Spring MessageSource for resolving validation messages,
instead of relying on JSR-303's default "ValidationMessages.properties" bundle
in the classpath.
|
void |
setValidationProperties(java.util.Properties jpaProperties)
Specify bean validation properties to be passed to the validation provider.
|
void |
setValidationPropertyMap(java.util.Map<java.lang.String,java.lang.String> validationProperties)
Specify bean validation properties to be passed to the validation provider as a Map.
|
void |
setValidationProviderResolver(ValidationProviderResolver validationProviderResolver)
Specify a JSR-303
ValidationProviderResolver for bootstrapping the
provider of choice, as an alternative to META-INF driven resolution. |
<T> T |
unwrap(java.lang.Class<T> type) |
ValidatorContext |
usingContext() |
determineErrorCode, determineField, getArgumentsForConstraint, getConstraintsForClass, getRejectedValue, getResolvableField, processConstraintViolations, supports, validate, validate, validate, validateProperty, validateValuepublic void setProviderClass(java.lang.Class providerClass)
If not specified, JSR-303's default search mechanism will be used.
public void setValidationProviderResolver(ValidationProviderResolver validationProviderResolver)
ValidationProviderResolver for bootstrapping the
provider of choice, as an alternative to META-INF driven resolution.public void setMessageInterpolator(MessageInterpolator messageInterpolator)
public void setValidationMessageSource(MessageSource messageSource)
NOTE: This feature requires Hibernate Validator 4.3 or higher on the classpath.
You may nevertheless use a different validation provider but Hibernate Validator's
ResourceBundleMessageInterpolator class must be accessible during configuration.
Specify either this property or "messageInterpolator",
not both. If you would like to build a custom MessageInterpolator, consider deriving from
Hibernate Validator's ResourceBundleMessageInterpolator and passing in a
Spring-based ResourceBundleLocator when constructing your interpolator.
In order for Hibernate's default validation messages to be resolved still, your
MessageSource must be configured for optional resolution (usually the default).
In particular, the MessageSource instance specified here should not apply
"useCodeAsDefaultMessage" behavior. Please double-check your setup accordingly.
ResourceBundleMessageInterpolatorpublic void setTraversableResolver(TraversableResolver traversableResolver)
public void setConstraintValidatorFactory(ConstraintValidatorFactory constraintValidatorFactory)
Default is a SpringConstraintValidatorFactory, delegating to the
containing ApplicationContext for creating autowired ConstraintValidator instances.
public void setParameterNameDiscoverer(org.springframework.core.ParameterNameDiscoverer parameterNameDiscoverer)
Default is a DefaultParameterNameDiscoverer.
public void setMappingLocations(org.springframework.core.io.Resource... mappingLocations)
public void setValidationProperties(java.util.Properties jpaProperties)
Can be populated with a String "value" (parsed via PropertiesEditor) or a "props" element in XML bean definitions.
public void setValidationPropertyMap(java.util.Map<java.lang.String,java.lang.String> validationProperties)
Can be populated with a "map" or "props" element in XML bean definitions.
public java.util.Map<java.lang.String,java.lang.String> getValidationPropertyMap()
Useful for specifying entries directly, for example via "validationPropertyMap[myKey]".
public void setApplicationContext(ApplicationContext applicationContext)
ApplicationContextAwareInvoked after population of normal bean properties but before an init callback such
as InitializingBean.afterPropertiesSet()
or a custom init-method. Invoked after ResourceLoaderAware.setResourceLoader(org.springframework.core.io.ResourceLoader),
ApplicationEventPublisherAware.setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) and
MessageSourceAware, if applicable.
setApplicationContext in interface ApplicationContextAwareapplicationContext - the ApplicationContext object to be used by this objectBeanInitializationExceptionpublic void afterPropertiesSet()
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBeanprotected void postProcessConfiguration(Configuration<?> configuration)
Invoked right before building the ValidatorFactory.
configuration - the Configuration object, pre-populated with
settings driven by LocalValidatorFactoryBean's propertiespublic Validator getValidator()
getValidator in interface ValidatorFactorypublic ValidatorContext usingContext()
usingContext in interface ValidatorFactorypublic MessageInterpolator getMessageInterpolator()
getMessageInterpolator in interface ValidatorFactorypublic TraversableResolver getTraversableResolver()
getTraversableResolver in interface ValidatorFactorypublic ConstraintValidatorFactory getConstraintValidatorFactory()
getConstraintValidatorFactory in interface ValidatorFactorypublic <T> T unwrap(java.lang.Class<T> type)
unwrap in interface Validatorunwrap in interface ValidatorFactoryunwrap in class SpringValidatorAdapterpublic void close()
public void destroy()
destroy in interface org.springframework.beans.factory.DisposableBean