spring-framework / org.springframework.validation.beanvalidation / LocalValidatorFactoryBean

LocalValidatorFactoryBean

open class LocalValidatorFactoryBean : SpringValidatorAdapter, ValidatorFactory, ApplicationContextAware, InitializingBean, DisposableBean

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

As of Spring 5.0, this class requires Bean Validation 1.1+, with special support for Hibernate Validator 5.x (see #setValidationMessageSource). This class is also runtime-compatible with Bean Validation 2.0 and Hibernate Validator 6.0, with one special note: If you'd like to call BV 2.0's getClockProvider() method, obtain the native ValidatorFactory through #unwrap(ValidatorFactory.class) and call the getClockProvider() method on the returned native reference there.

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.

Author
Juergen Hoeller

Since
3.0

See Also
javax.validation.ValidatorFactoryjavax.validation.Validatorjavax.validation.Validation#buildDefaultValidatorFactory()javax.validation.ValidatorFactory#getValidator()

Constructors

<init>

LocalValidatorFactoryBean()

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

As of Spring 5.0, this class requires Bean Validation 1.1+, with special support for Hibernate Validator 5.x (see #setValidationMessageSource). This class is also runtime-compatible with Bean Validation 2.0 and Hibernate Validator 6.0, with one special note: If you'd like to call BV 2.0's getClockProvider() method, obtain the native ValidatorFactory through #unwrap(ValidatorFactory.class) and call the getClockProvider() method on the returned native reference there.

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.

Functions

afterPropertiesSet

open fun afterPropertiesSet(): Unit

close

open fun close(): Unit

destroy

open fun destroy(): Unit

getConstraintValidatorFactory

open fun getConstraintValidatorFactory(): ConstraintValidatorFactory

getMessageInterpolator

open fun getMessageInterpolator(): MessageInterpolator

getParameterNameProvider

open fun getParameterNameProvider(): ParameterNameProvider

getTraversableResolver

open fun getTraversableResolver(): TraversableResolver

getValidationPropertyMap

open fun getValidationPropertyMap(): MutableMap<String, String>

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]".

getValidator

open fun getValidator(): Validator

setApplicationContext

open fun setApplicationContext(applicationContext: ApplicationContext): Unit

setConstraintValidatorFactory

open fun setConstraintValidatorFactory(constraintValidatorFactory: ConstraintValidatorFactory): Unit

Specify a custom ConstraintValidatorFactory to use for this ValidatorFactory.

Default is a SpringConstraintValidatorFactory, delegating to the containing ApplicationContext for creating autowired ConstraintValidator instances.

setMappingLocations

open fun setMappingLocations(vararg mappingLocations: Resource): Unit

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

setMessageInterpolator

open fun setMessageInterpolator(messageInterpolator: MessageInterpolator): Unit

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

setParameterNameDiscoverer

open fun setParameterNameDiscoverer(parameterNameDiscoverer: ParameterNameDiscoverer): Unit

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

Default is a org.springframework.core.DefaultParameterNameDiscoverer.

setProviderClass

open fun setProviderClass(providerClass: Class<Any>): Unit

Specify the desired provider class, if any.

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

setTraversableResolver

open fun setTraversableResolver(traversableResolver: TraversableResolver): Unit

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

setValidationMessageSource

open fun setValidationMessageSource(messageSource: MessageSource): Unit

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.

setValidationProperties

open fun setValidationProperties(jpaProperties: Properties): Unit

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

open fun setValidationPropertyMap(validationProperties: MutableMap<String, String>): Unit

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.

setValidationProviderResolver

open fun setValidationProviderResolver(validationProviderResolver: ValidationProviderResolver): Unit

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

unwrap

open fun <T : Any> unwrap(type: Class<T>): T

usingContext

open fun usingContext(): ValidatorContext

Inheritors

OptionalValidatorFactoryBean

open class OptionalValidatorFactoryBean : LocalValidatorFactoryBean

LocalValidatorFactoryBean subclass that simply turns org.springframework.validation.Validator calls into no-ops in case of no Bean Validation provider being available.

This is the actual class used by Spring's MVC configuration namespace, in case of the javax.validation API being present but no explicit Validator having been configured.