Class LocalValidatorFactoryBean
- java.lang.Object
- org.springframework.validation.beanvalidation.SpringValidatorAdapter
- org.springframework.validation.beanvalidation.LocalValidatorFactoryBean
- All Implemented Interfaces:
Validator
,ValidatorFactory
,Aware
,DisposableBean
,InitializingBean
,ApplicationContextAware
,SmartValidator
,Validator
- Direct Known Subclasses:
OptionalValidatorFactoryBean
public class LocalValidatorFactoryBean extends SpringValidatorAdapter implements ValidatorFactory, ApplicationContextAware, InitializingBean, DisposableBean
This is the central class forjavax.validation
(JSR-303) setup in a Spring application context: It bootstraps ajavax.validation.ValidationFactory
and exposes it through the SpringValidator
interface as well as through the JSR-303Validator
interface and theValidatorFactory
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; considerMethodValidationInterceptor
instead. If you really need programmatic#forExecutables
access, inject this class as aValidatorFactory
and callgetValidator()
on it, then#forExecutables
on the returned nativeValidator
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.- Since:
- 3.0
- Author:
- Juergen Hoeller
- See Also:
ValidatorFactory
,Validator
,Validation.buildDefaultValidatorFactory()
,ValidatorFactory.getValidator()
Constructor Summary
Constructors Constructor Description LocalValidatorFactoryBean()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
afterPropertiesSet()
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.void
close()
void
destroy()
Invoked by the containingBeanFactory
on destruction of a bean.ConstraintValidatorFactory
getConstraintValidatorFactory()
MessageInterpolator
getMessageInterpolator()
TraversableResolver
getTraversableResolver()
Map<String,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(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(ParameterNameDiscoverer parameterNameDiscoverer)
Set the ParameterNameDiscoverer to use for resolving method and constructor parameter names if needed for message interpolation.void
setProviderClass(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(Properties jpaProperties)
Specify bean validation properties to be passed to the validation provider.void
setValidationPropertyMap(Map<String,String> validationProperties)
Specify bean validation properties to be passed to the validation provider as a Map.void
setValidationProviderResolver(ValidationProviderResolver validationProviderResolver)
Specify a JSR-303ValidationProviderResolver
for bootstrapping the provider of choice, as an alternative toMETA-INF
driven resolution.<T> T
unwrap(Class<T> type)
ValidatorContext
usingContext()
Methods inherited from class org.springframework.validation.beanvalidation.SpringValidatorAdapter
determineErrorCode, determineField, getArgumentsForConstraint, getConstraintsForClass, getRejectedValue, getResolvableField, processConstraintViolations, supports, validate, validate, validate, validateProperty, validateValue
Constructor Detail
LocalValidatorFactoryBean
public LocalValidatorFactoryBean()
Method Detail
setProviderClass
public void setProviderClass(Class providerClass)
Specify the desired provider class, if any.If not specified, JSR-303's default search mechanism will be used.
setValidationProviderResolver
public void setValidationProviderResolver(ValidationProviderResolver validationProviderResolver)
Specify a JSR-303ValidationProviderResolver
for bootstrapping the provider of choice, as an alternative toMETA-INF
driven resolution.- Since:
- 4.3
setMessageInterpolator
public void setMessageInterpolator(MessageInterpolator messageInterpolator)
Specify a custom MessageInterpolator to use for this ValidatorFactory and its exposed default Validator.
setValidationMessageSource
public 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. This may refer to a Spring context's shared "messageSource" bean, or to some special MessageSource setup for validation purposes only.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'sResourceBundleMessageInterpolator
and passing in a Spring-basedResourceBundleLocator
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, theMessageSource
instance specified here should not apply"useCodeAsDefaultMessage"
behavior. Please double-check your setup accordingly.- See Also:
ResourceBundleMessageInterpolator
setTraversableResolver
public void setTraversableResolver(TraversableResolver traversableResolver)
Specify a custom TraversableResolver to use for this ValidatorFactory and its exposed default Validator.
setConstraintValidatorFactory
public void setConstraintValidatorFactory(ConstraintValidatorFactory constraintValidatorFactory)
Specify a custom ConstraintValidatorFactory to use for this ValidatorFactory.Default is a
SpringConstraintValidatorFactory
, delegating to the containing ApplicationContext for creating autowired ConstraintValidator instances.
setParameterNameDiscoverer
public void setParameterNameDiscoverer(ParameterNameDiscoverer parameterNameDiscoverer)
Set the ParameterNameDiscoverer to use for resolving method and constructor parameter names if needed for message interpolation.Default is a
DefaultParameterNameDiscoverer
.
setMappingLocations
public void setMappingLocations(Resource... mappingLocations)
Specify resource locations to load XML constraint mapping files from, if any.
setValidationProperties
public void setValidationProperties(Properties jpaProperties)
Specify bean validation properties to be passed to the validation provider.Can be populated with a String "value" (parsed via PropertiesEditor) or a "props" element in XML bean definitions.
setValidationPropertyMap
public void setValidationPropertyMap(Map<String,String> validationProperties)
Specify bean validation properties to be passed to the validation provider as a Map.Can be populated with a "map" or "props" element in XML bean definitions.
getValidationPropertyMap
public Map<String,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.Useful for specifying entries directly, for example via "validationPropertyMap[myKey]".
setApplicationContext
public void setApplicationContext(ApplicationContext applicationContext)
Description copied from interface:ApplicationContextAware
Set the ApplicationContext that this object runs in. Normally this call will be used to initialize the object.Invoked after population of normal bean properties but before an init callback such as
InitializingBean.afterPropertiesSet()
or a custom init-method. Invoked afterResourceLoaderAware.setResourceLoader(org.springframework.core.io.ResourceLoader)
,ApplicationEventPublisherAware.setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher)
andMessageSourceAware
, if applicable.- Specified by:
setApplicationContext
in interfaceApplicationContextAware
- Parameters:
applicationContext
- the ApplicationContext object to be used by this object- See Also:
BeanInitializationException
afterPropertiesSet
public void afterPropertiesSet()
Description copied from interface:InitializingBean
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
- Specified by:
afterPropertiesSet
in interfaceInitializingBean
postProcessConfiguration
protected void postProcessConfiguration(Configuration<?> configuration)
Post-process the given Bean Validation configuration, adding to or overriding any of its settings.Invoked right before building the
ValidatorFactory
.- Parameters:
configuration
- the Configuration object, pre-populated with settings driven by LocalValidatorFactoryBean's properties
getValidator
public Validator getValidator()
- Specified by:
getValidator
in interfaceValidatorFactory
usingContext
public ValidatorContext usingContext()
- Specified by:
usingContext
in interfaceValidatorFactory
getMessageInterpolator
public MessageInterpolator getMessageInterpolator()
- Specified by:
getMessageInterpolator
in interfaceValidatorFactory
getTraversableResolver
public TraversableResolver getTraversableResolver()
- Specified by:
getTraversableResolver
in interfaceValidatorFactory
getConstraintValidatorFactory
public ConstraintValidatorFactory getConstraintValidatorFactory()
- Specified by:
getConstraintValidatorFactory
in interfaceValidatorFactory
unwrap
public <T> T unwrap(Class<T> type)
- Specified by:
unwrap
in interfaceValidator
- Specified by:
unwrap
in interfaceValidatorFactory
- Overrides:
unwrap
in classSpringValidatorAdapter
close
public void close()
destroy
public void destroy()
Description copied from interface:DisposableBean
Invoked by the containingBeanFactory
on destruction of a bean.- Specified by:
destroy
in interfaceDisposableBean