Class LocalValidatorFactoryBean
- All Implemented Interfaces:
jakarta.validation.Validator, jakarta.validation.ValidatorFactory, AutoCloseable, Aware, DisposableBean, InitializingBean, ApplicationContextAware, SmartValidator, Validator
- Direct Known Subclasses:
OptionalValidatorFactoryBean
jakarta.validation (JSR-303) setup in a Spring
application context: It bootstraps a jakarta.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!
This class is also being used by Spring's MVC configuration namespace, in case of the
jakarta.validation API being present but no explicit Validator having been configured.
- Since:
- 3.0
- Author:
- Juergen Hoeller, Sebastien Deleuze
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.voidclose()voiddestroy()Invoked by the containingBeanFactoryon destruction of a bean.jakarta.validation.ClockProviderjakarta.validation.ConstraintValidatorFactoryjakarta.validation.MessageInterpolatorjakarta.validation.ParameterNameProviderjakarta.validation.TraversableResolverAllow Map access to the bean validation properties to be passed to the validation provider, with the option to add or override specific entries.jakarta.validation.Validatorprotected voidpostProcessConfiguration(jakarta.validation.Configuration<?> configuration) Post-process the given Bean Validation configuration, adding to or overriding any of its settings.voidsetApplicationContext(ApplicationContext applicationContext) Set the ApplicationContext that this object runs in.voidsetConfigurationInitializer(Consumer<jakarta.validation.Configuration<?>> configurationInitializer) Specify a callback for customizing the Bean ValidationConfigurationinstance, as an alternative to overriding thepostProcessConfiguration(Configuration)method in customLocalValidatorFactoryBeansubclasses.voidsetConstraintValidatorFactory(jakarta.validation.ConstraintValidatorFactory constraintValidatorFactory) Specify a custom ConstraintValidatorFactory to use for this ValidatorFactory.voidsetMappingLocations(Resource... mappingLocations) Specify resource locations to load XML constraint mapping files from, if any.voidsetMessageInterpolator(jakarta.validation.MessageInterpolator messageInterpolator) Specify a custom MessageInterpolator to use for this ValidatorFactory and its exposed default Validator.voidsetParameterNameDiscoverer(ParameterNameDiscoverer parameterNameDiscoverer) Set the ParameterNameDiscoverer to use for resolving method and constructor parameter names if needed for message interpolation.voidsetProviderClass(Class providerClass) Specify the desired provider class, if any.voidsetTraversableResolver(jakarta.validation.TraversableResolver traversableResolver) Specify a custom TraversableResolver to use for this ValidatorFactory and its exposed default Validator.voidsetValidationMessageSource(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.voidsetValidationProperties(Properties jpaProperties) Specify bean validation properties to be passed to the validation provider.voidsetValidationPropertyMap(@Nullable Map<String, String> validationProperties) Specify bean validation properties to be passed to the validation provider as a Map.voidsetValidationProviderResolver(jakarta.validation.ValidationProviderResolver validationProviderResolver) Specify a JSR-303ValidationProviderResolverfor bootstrapping the provider of choice, as an alternative toMETA-INFdriven resolution.<T> TReturn a contained validator instance of the specified type, unwrapping as far as necessary.jakarta.validation.ValidatorContextMethods inherited from class SpringValidatorAdapter
determineErrorCode, determineField, forExecutables, getArgumentsForConstraint, getConstraintsForClass, getRejectedValue, getResolvableField, processConstraintViolations, requiresMessageFormat, supports, validate, validate, validate, validateProperty, validateValue, validateValueMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Validator
validateObject
-
Constructor Details
-
LocalValidatorFactoryBean
public LocalValidatorFactoryBean()
-
-
Method Details
-
setProviderClass
Specify the desired provider class, if any.If not specified, JSR-303's default search mechanism will be used.
- See Also:
-
setValidationProviderResolver
public void setValidationProviderResolver(jakarta.validation.ValidationProviderResolver validationProviderResolver) Specify a JSR-303ValidationProviderResolverfor bootstrapping the provider of choice, as an alternative toMETA-INFdriven resolution.- Since:
- 4.3
-
setMessageInterpolator
public void setMessageInterpolator(jakarta.validation.MessageInterpolator messageInterpolator) Specify a custom MessageInterpolator to use for this ValidatorFactory and its exposed default Validator. -
setValidationMessageSource
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
ResourceBundleMessageInterpolatorclass 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'sResourceBundleMessageInterpolatorand passing in a Spring-basedResourceBundleLocatorwhen constructing your interpolator.In order for Hibernate's default validation messages to be resolved still, your
MessageSourcemust be configured for optional resolution (usually the default). In particular, theMessageSourceinstance specified here should not apply"useCodeAsDefaultMessage"behavior. Please double-check your setup accordingly.- See Also:
-
setTraversableResolver
public void setTraversableResolver(jakarta.validation.TraversableResolver traversableResolver) Specify a custom TraversableResolver to use for this ValidatorFactory and its exposed default Validator. -
setConstraintValidatorFactory
public void setConstraintValidatorFactory(jakarta.validation.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
Set the ParameterNameDiscoverer to use for resolving method and constructor parameter names if needed for message interpolation.Default is Hibernate Validator's own internal use of standard Java reflection. This may be overridden with a custom subclass or a Spring-controlled
DefaultParameterNameDiscovererif necessary. -
setMappingLocations
Specify resource locations to load XML constraint mapping files from, if any. -
setValidationProperties
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.
- See Also:
-
setValidationPropertyMap
-
getValidationPropertyMap
-
setConfigurationInitializer
public void setConfigurationInitializer(Consumer<jakarta.validation.Configuration<?>> configurationInitializer) Specify a callback for customizing the Bean ValidationConfigurationinstance, as an alternative to overriding thepostProcessConfiguration(Configuration)method in customLocalValidatorFactoryBeansubclasses.This enables convenient customizations for application purposes. Infrastructure extensions may keep overriding the
postProcessConfiguration(Configuration)template method.- Since:
- 5.3.19
-
setApplicationContext
Description copied from interface:ApplicationContextAwareSet 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(ResourceLoader),ApplicationEventPublisherAware.setApplicationEventPublisher(ApplicationEventPublisher)andMessageSourceAware, if applicable.- Specified by:
setApplicationContextin interfaceApplicationContextAware- Parameters:
applicationContext- the ApplicationContext object to be used by this object- See Also:
-
afterPropertiesSet
public void afterPropertiesSet()Description copied from interface:InitializingBeanInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.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:
afterPropertiesSetin interfaceInitializingBean
-
postProcessConfiguration
protected void postProcessConfiguration(jakarta.validation.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 jakarta.validation.Validator getValidator()- Specified by:
getValidatorin interfacejakarta.validation.ValidatorFactory
-
usingContext
public jakarta.validation.ValidatorContext usingContext()- Specified by:
usingContextin interfacejakarta.validation.ValidatorFactory
-
getMessageInterpolator
public jakarta.validation.MessageInterpolator getMessageInterpolator()- Specified by:
getMessageInterpolatorin interfacejakarta.validation.ValidatorFactory
-
getTraversableResolver
public jakarta.validation.TraversableResolver getTraversableResolver()- Specified by:
getTraversableResolverin interfacejakarta.validation.ValidatorFactory
-
getConstraintValidatorFactory
public jakarta.validation.ConstraintValidatorFactory getConstraintValidatorFactory()- Specified by:
getConstraintValidatorFactoryin interfacejakarta.validation.ValidatorFactory
-
getParameterNameProvider
public jakarta.validation.ParameterNameProvider getParameterNameProvider()- Specified by:
getParameterNameProviderin interfacejakarta.validation.ValidatorFactory
-
getClockProvider
public jakarta.validation.ClockProvider getClockProvider()- Specified by:
getClockProviderin interfacejakarta.validation.ValidatorFactory
-
unwrap
Description copied from interface:SmartValidatorReturn a contained validator instance of the specified type, unwrapping as far as necessary.- Specified by:
unwrapin interfaceSmartValidator- Specified by:
unwrapin interfacejakarta.validation.Validator- Specified by:
unwrapin interfacejakarta.validation.ValidatorFactory- Overrides:
unwrapin classSpringValidatorAdapter- Type Parameters:
T- the type of the object to return- Parameters:
type- the class of the object to return- Returns:
- a validator instance of the specified type;
nullif there isn't a nested validator; an exception may be raised if the specified validator type does not match.
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfacejakarta.validation.ValidatorFactory
-
destroy
public void destroy()Description copied from interface:DisposableBeanInvoked by the containingBeanFactoryon destruction of a bean.- Specified by:
destroyin interfaceDisposableBean
-