Top Description Inners Fields Constructors Methods
org.springframework.validation.beanvalidation

public Class LocalValidatorFactoryBean

extends SpringValidatorAdapter
implements ValidatorFactory, ApplicationContextAware, InitializingBean, DisposableBean
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.Aware, jakarta.validation.ValidatorFactory, java.lang.AutoCloseable
Known Direct Subclasses
org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean
Imports
java.io.IOException, .InputStream, java.lang.reflect.Constructor, .Method, java.util.ArrayList, .Arrays, .HashMap, .List, .Map, .Properties, java.util.function.Consumer, jakarta.validation.ClockProvider, .Configuration, .ConstraintValidatorFactory, .MessageInterpolator, .ParameterNameProvider, .TraversableResolver, .Validation, .ValidationException, .ValidationProviderResolver, .Validator, .ValidatorContext, .ValidatorFactory, jakarta.validation.bootstrap.GenericBootstrap, .ProviderSpecificBootstrap, org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator, org.springframework.beans.factory.DisposableBean, .InitializingBean, org.springframework.context.ApplicationContext, .ApplicationContextAware, .MessageSource, org.springframework.core.ParameterNameDiscoverer, org.springframework.core.io.Resource, org.springframework.lang.Nullable, org.springframework.util.Assert, .CollectionUtils, .ReflectionUtils

This is the central class for jakarta.validation (JSR-303) setup in a Spring application context: It bootstraps a jakarta.validation.ValidationFactory and exposes it through the Spring org.springframework.validation.Validator interface as well as through the JSR-303 jakarta.validation.Validator interface and the jakarta.validation.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 org.springframework.validation.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.

Authors
Juergen Hoeller, Sebastien Deleuze
Since
3.0
See Also
jakarta.validation.ValidatorFactory, jakarta.validation.Validator, jakarta.validation.Validation#buildDefaultValidatorFactory(), jakarta.validation.ValidatorFactory#getValidator()

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
LocalValidatorFactoryBean.HibernateValidatorDelegate

Inner class to avoid a hard-coded Hibernate Validator dependency.

Field Summary

Modifier and TypeField and Description
private ApplicationContext
private Consumer<Configuration<?>>
private ConstraintValidatorFactory
private Resource[]
private MessageInterpolator
private ParameterNameDiscoverer
private Class<T>
private TraversableResolver
private final Map<String, String>
private ValidationProviderResolver
private ValidatorFactory

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, 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.

public void
close()

Implements jakarta.validation.ValidatorFactory.close.

Closes the ValidatorFactory instance.
private void
private void
public void
destroy()

Implements org.springframework.beans.factory.DisposableBean.destroy.

Invoked by the containing BeanFactory on destruction of a bean.
public ClockProvider
getClockProvider()

Implements jakarta.validation.ValidatorFactory.getClockProvider.

Returns the ClockProvider instance configured at initialization time for the ValidatorFactory.
public ConstraintValidatorFactory
getConstraintValidatorFactory()

Implements jakarta.validation.ValidatorFactory.getConstraintValidatorFactory.

Returns the ConstraintValidatorFactory instance configured at initialization time for the ValidatorFactory.
public MessageInterpolator
getMessageInterpolator()

Implements jakarta.validation.ValidatorFactory.getMessageInterpolator.

Returns the MessageInterpolator instance configured at initialization time for the ValidatorFactory.
public ParameterNameProvider
getParameterNameProvider()

Implements jakarta.validation.ValidatorFactory.getParameterNameProvider.

Returns the ParameterNameProvider instance configured at initialization time for the ValidatorFactory.
public TraversableResolver
getTraversableResolver()

Implements jakarta.validation.ValidatorFactory.getTraversableResolver.

Returns the TraversableResolver instance configured at initialization time for the ValidatorFactory.
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.

public Validator
getValidator()

Implements jakarta.validation.ValidatorFactory.getValidator.

Returns an initialized Validator instance using the factory defaults for message interpolator, traversable resolver and constraint validator factory.
protected void
postProcessConfiguration(Configuration<?>
the Configuration object, pre-populated with settings driven by LocalValidatorFactoryBean's properties
configuration
)

Post-process the given Bean Validation configuration, adding to or overriding any of its settings.

public void
setApplicationContext(ApplicationContext
the ApplicationContext object to be used by this object
applicationContext
)

Implements org.springframework.context.ApplicationContextAware.setApplicationContext.

Set the ApplicationContext that this object runs in. Normally this call will be used to initialize the object.
public void
setConfigurationInitializer(Consumer<Configuration<?>> configurationInitializer)

Specify a callback for customizing the Bean Validation Configuration instance, as an alternative to overriding the postProcessConfiguration(Configuration) method in custom LocalValidatorFactoryBean subclasses.

public void
setConstraintValidatorFactory(ConstraintValidatorFactory constraintValidatorFactory)

Specify a custom ConstraintValidatorFactory to use for this ValidatorFactory.

public void
setMappingLocations(Resource... mappingLocations)

Specify resource locations to load XML constraint mapping files from, if any.

public void
setMessageInterpolator(MessageInterpolator messageInterpolator)

Specify a custom MessageInterpolator to use for this ValidatorFactory and its exposed default Validator.

public void
setParameterNameDiscoverer(ParameterNameDiscoverer parameterNameDiscoverer)

Set the ParameterNameDiscoverer to use for resolving method and constructor parameter names if needed for message interpolation.

public void
setProviderClass(Class<T> providerClass)

Specify the desired provider class, if any.

public void
setTraversableResolver(TraversableResolver traversableResolver)

Specify a custom TraversableResolver to use for this ValidatorFactory and its exposed default Validator.

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.

public void
setValidationProperties(Properties jpaProperties)

Specify bean validation properties to be passed to the validation provider.

public void
setValidationPropertyMap(Map<String, String> validationProperties)

Specify bean validation properties to be passed to the validation provider as a Map.

public void
setValidationProviderResolver(ValidationProviderResolver validationProviderResolver)

Specify a JSR-303 ValidationProviderResolver for bootstrapping the provider of choice, as an alternative to META-INF driven resolution.

public <T> T
unwrap(Class<T>
the class of the object to be returned
type
)

Overrides org.springframework.validation.beanvalidation.SpringValidatorAdapter.unwrap.

Overrides default org.springframework.validation.SmartValidator.unwrap.

Implements jakarta.validation.ValidatorFactory.unwrap, jakarta.validation.Validator.unwrap.

Returns an instance of the specified type allowing access to provider-specific APIs.
public ValidatorContext
usingContext()

Implements jakarta.validation.ValidatorFactory.usingContext.

Defines a new validator context and returns a Validator compliant this new context.
Inherited from org.springframework.validation.beanvalidation.SpringValidatorAdapter:
determineErrorCodedetermineFieldforExecutablesgetArgumentsForConstraintgetConstraintsForClassgetRejectedValuegetResolvableFieldprocessConstraintViolationsrequiresMessageFormatsetTargetValidatorsupportsvalidatevalidatevalidatevalidatePropertyvalidateValuevalidateValue

Field Detail

applicationContextback to summary
private ApplicationContext applicationContext
Annotations
@Nullable
configurationInitializerback to summary
private Consumer<Configuration<?>> configurationInitializer
Annotations
@Nullable
constraintValidatorFactoryback to summary
private ConstraintValidatorFactory constraintValidatorFactory
Annotations
@Nullable
mappingLocationsback to summary
private Resource[] mappingLocations
Annotations
@Nullable
messageInterpolatorback to summary
private MessageInterpolator messageInterpolator
Annotations
@Nullable
parameterNameDiscovererback to summary
private ParameterNameDiscoverer parameterNameDiscoverer
Annotations
@Nullable
providerClassback to summary
private Class<T> providerClass
Annotations
@SuppressWarnings:rawtypes
@Nullable
traversableResolverback to summary
private TraversableResolver traversableResolver
Annotations
@Nullable
validationPropertyMapback to summary
private final Map<String, String> validationPropertyMap
validationProviderResolverback to summary
private ValidationProviderResolver validationProviderResolver
Annotations
@Nullable
validatorFactoryback to summary
private ValidatorFactory validatorFactory
Annotations
@Nullable

Constructor Detail

LocalValidatorFactoryBeanback to summary
public LocalValidatorFactoryBean()

Method Detail

afterPropertiesSetback to summary
public void afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Doc from org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, 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.

Annotations
@Override
@SuppressWarnings:rawtypes, unchecked
closeback to summary
public void close()

Implements jakarta.validation.ValidatorFactory.close.

Doc from jakarta.validation.ValidatorFactory.close.

Closes the ValidatorFactory instance. After the ValidatorFactory instance is closed, calling the following methods is not allowed:

  • methods of this ValidatorFactory instance
  • methods of Validator instances created by this ValidatorFactory
Annotations
@Override
closeMappingStreamsback to summary
private void closeMappingStreams(List<InputStream> mappingStreams)
configureParameterNameProviderback to summary
private void configureParameterNameProvider(ParameterNameDiscoverer discoverer, Configuration<?> configuration)
destroyback to summary
public void destroy()

Implements org.springframework.beans.factory.DisposableBean.destroy.

Doc from org.springframework.beans.factory.DisposableBean.destroy.

Invoked by the containing BeanFactory on destruction of a bean.

Annotations
@Override
getClockProviderback to summary
public ClockProvider getClockProvider()

Implements jakarta.validation.ValidatorFactory.getClockProvider.

Doc from jakarta.validation.ValidatorFactory.getClockProvider.

Returns the ClockProvider instance configured at initialization time for the ValidatorFactory. This is the instance used by #getValidator().

Returns:ClockProvider

ClockProvider instance

Annotations
@Override
getConstraintValidatorFactoryback to summary
public ConstraintValidatorFactory getConstraintValidatorFactory()

Implements jakarta.validation.ValidatorFactory.getConstraintValidatorFactory.

Doc from jakarta.validation.ValidatorFactory.getConstraintValidatorFactory.

Returns the ConstraintValidatorFactory instance configured at initialization time for the ValidatorFactory. This is the instance used by getValidator().

Returns:ConstraintValidatorFactory

ConstraintValidatorFactory instance

Annotations
@Override
getMessageInterpolatorback to summary
public MessageInterpolator getMessageInterpolator()

Implements jakarta.validation.ValidatorFactory.getMessageInterpolator.

Doc from jakarta.validation.ValidatorFactory.getMessageInterpolator.

Returns the MessageInterpolator instance configured at initialization time for the ValidatorFactory. This is the instance used by getValidator().

Returns:MessageInterpolator

MessageInterpolator instance

Annotations
@Override
getParameterNameProviderback to summary
public ParameterNameProvider getParameterNameProvider()

Implements jakarta.validation.ValidatorFactory.getParameterNameProvider.

Doc from jakarta.validation.ValidatorFactory.getParameterNameProvider.

Returns the ParameterNameProvider instance configured at initialization time for the ValidatorFactory. This is the instance used by #getValidator().

Returns:ParameterNameProvider

ParameterNameProvider instance

Annotations
@Override
getTraversableResolverback to summary
public TraversableResolver getTraversableResolver()

Implements jakarta.validation.ValidatorFactory.getTraversableResolver.

Doc from jakarta.validation.ValidatorFactory.getTraversableResolver.

Returns the TraversableResolver instance configured at initialization time for the ValidatorFactory. This is the instance used by getValidator().

Returns:TraversableResolver

TraversableResolver instance

Annotations
@Override
getValidationPropertyMapback to summary
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]".

getValidatorback to summary
public Validator getValidator()

Implements jakarta.validation.ValidatorFactory.getValidator.

Doc from jakarta.validation.ValidatorFactory.getValidator.

Returns an initialized Validator instance using the factory defaults for message interpolator, traversable resolver and constraint validator factory.

Validator instances can be pooled and shared by the implementation.

Returns:Validator

an initialized Validator instance

Annotations
@Override
postProcessConfigurationback to summary
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:Configuration<?>

the Configuration object, pre-populated with settings driven by LocalValidatorFactoryBean's properties

setApplicationContextback to summary
public void setApplicationContext(ApplicationContext applicationContext)

Implements org.springframework.context.ApplicationContextAware.setApplicationContext.

Doc from org.springframework.context.ApplicationContextAware.setApplicationContext.

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 org.springframework.beans.factory.InitializingBean#afterPropertiesSet() or a custom init-method. Invoked after ResourceLoaderAware#setResourceLoader, ApplicationEventPublisherAware#setApplicationEventPublisher and MessageSourceAware, if applicable.

Parameters
applicationContext:ApplicationContext

the ApplicationContext object to be used by this object

Annotations
@Override
setConfigurationInitializerback to summary
public void setConfigurationInitializer(Consumer<Configuration<?>> configurationInitializer)

Specify a callback for customizing the Bean Validation Configuration instance, as an alternative to overriding the postProcessConfiguration(Configuration) method in custom LocalValidatorFactoryBean subclasses.

This enables convenient customizations for application purposes. Infrastructure extensions may keep overriding the postProcessConfiguration template method.

Since
5.3.19
setConstraintValidatorFactoryback to summary
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.

setMappingLocationsback to summary
public void setMappingLocations(Resource... mappingLocations)

Specify resource locations to load XML constraint mapping files from, if any.

setMessageInterpolatorback to summary
public void setMessageInterpolator(MessageInterpolator messageInterpolator)

Specify a custom MessageInterpolator to use for this ValidatorFactory and its exposed default Validator.

setParameterNameDiscovererback to summary
public void setParameterNameDiscoverer(ParameterNameDiscoverer parameterNameDiscoverer)

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 org.springframework.core.DefaultParameterNameDiscoverer if necessary.

setProviderClassback to summary
public void setProviderClass(Class<T> providerClass)

Specify the desired provider class, if any.

If not specified, JSR-303's default search mechanism will be used.

Annotations
@SuppressWarnings:rawtypes
See Also
jakarta.validation.Validation#byProvider(Class), jakarta.validation.Validation#byDefaultProvider()
setTraversableResolverback to summary
public void setTraversableResolver(TraversableResolver traversableResolver)

Specify a custom TraversableResolver to use for this ValidatorFactory and its exposed default Validator.

setValidationMessageSourceback to summary
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'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.

See Also
ResourceBundleMessageInterpolator
setValidationPropertiesback to summary
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.

See Also
jakarta.validation.Configuration#addProperty(String, String)
setValidationPropertyMapback to summary
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.

See Also
jakarta.validation.Configuration#addProperty(String, String)
setValidationProviderResolverback to summary
public void setValidationProviderResolver(ValidationProviderResolver validationProviderResolver)

Specify a JSR-303 ValidationProviderResolver for bootstrapping the provider of choice, as an alternative to META-INF driven resolution.

Since
4.3
unwrapback to summary
public <T> T unwrap(Class<T> type)

Overrides org.springframework.validation.beanvalidation.SpringValidatorAdapter.unwrap.

Overrides default org.springframework.validation.SmartValidator.unwrap.

Implements jakarta.validation.ValidatorFactory.unwrap, jakarta.validation.Validator.unwrap.

Doc from jakarta.validation.ValidatorFactory.unwrap.

Returns an instance of the specified type allowing access to provider-specific APIs. If the Jakarta Bean Validation provider implementation does not support the specified class, a ValidationException is thrown.

Parameters
<T>
the type of the object to be returned
type:Class<T>

the class of the object to be returned

Returns:T

an instance of the specified class

Annotations
@Override
@SuppressWarnings:unchecked
usingContextback to summary
public ValidatorContext usingContext()

Implements jakarta.validation.ValidatorFactory.usingContext.

Doc from jakarta.validation.ValidatorFactory.usingContext.

Defines a new validator context and returns a Validator compliant this new context.

Returns:ValidatorContext

a ValidatorContext instance

Annotations
@Override
org.springframework.validation.beanvalidation back to summary

private Class LocalValidatorFactoryBean.HibernateValidatorDelegate

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.validation.beanvalidation.LocalValidatorFactoryBean.HibernateValidatorDelegate

Inner class to avoid a hard-coded Hibernate Validator dependency.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static MessageInterpolator
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

HibernateValidatorDelegateback to summary
private HibernateValidatorDelegate()

Method Detail

buildMessageInterpolatorback to summary
public static MessageInterpolator buildMessageInterpolator(MessageSource messageSource)