public class LocalValidatorFactoryBean extends SpringValidatorAdapter implements ValidatorFactory, ApplicationContextAware, InitializingBean
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
!
NOTE: This class is based on Bean Validation 1.0 and (optionally) Hibernate Validator 4.x.
Nevertheless, its core functionality does work against Bean Validation 1.1 at runtime.
However, there is no special support for Hibernate Validator 5.0 yet; as a consequence,
setValidationMessageSource(org.springframework.context.MessageSource)
won't work in that scenario. Please stick with
Hibernate Validator 4.3 for the time being, or upgrade to Spring Framework 4.0.
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()
Invoked by a BeanFactory after it has set all bean properties supplied
(and satisfied BeanFactoryAware and ApplicationContextAware).
|
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 |
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.
|
ValidatorContext |
usingContext() |
getArgumentsForConstraint, getConstraintsForClass, processConstraintViolations, supports, unwrap, validate, validate, validate, validateProperty, validateValue
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
unwrap
public void setProviderClass(Class providerClass)
If not specified, JSR-303's default search mechanism will be used.
public void setMessageInterpolator(MessageInterpolator messageInterpolator)
public void setValidationMessageSource(MessageSource messageSource)
NOTE: This feature requires Hibernate Validator 4.1 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 MessageSourceResourceBundleLocator
when constructing your interpolator.
public 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 setMappingLocations(Resource... mappingLocations)
public void setValidationProperties(Properties jpaProperties)
Can be populated with a String "value" (parsed via PropertiesEditor) or a "props" element in XML bean definitions.
public void setValidationPropertyMap(Map<String,String> validationProperties)
Can be populated with a "map" or "props" element in XML bean definitions.
public Map<String,String> getValidationPropertyMap()
Useful for specifying entries directly, for example via "validationPropertyMap[myKey]".
public void setApplicationContext(ApplicationContext applicationContext)
ApplicationContextAware
Invoked 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 ApplicationContextAware
applicationContext
- the ApplicationContext object to be used by this objectBeanInitializationException
public void afterPropertiesSet()
InitializingBean
This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.
afterPropertiesSet
in interface InitializingBean
protected 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 ValidatorFactory
public ValidatorContext usingContext()
usingContext
in interface ValidatorFactory
public MessageInterpolator getMessageInterpolator()
getMessageInterpolator
in interface ValidatorFactory
public TraversableResolver getTraversableResolver()
getTraversableResolver
in interface ValidatorFactory
public ConstraintValidatorFactory getConstraintValidatorFactory()
getConstraintValidatorFactory
in interface ValidatorFactory