Class Validation
There are three ways to bootstrap it:
- The easiest approach is to build the default
ValidatorFactory.ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
In this case, the default validation provider resolver will be used to locate available providers.The chosen provider is defined as followed:
- if the XML configuration defines a provider, this provider is used
- if the XML configuration does not define a provider or if no XML
configuration is present the first provider returned by the
ValidationProviderResolverinstance is used.
-
The second bootstrap approach allows to choose a custom
ValidationProviderResolver. The chosenValidationProvideris then determined in the same way as in the default bootstrapping case (see above).Configuration<?> configuration = Validation .byDefaultProvider() .providerResolver( new MyResolverStrategy() ) .configure(); ValidatorFactory factory = configuration.buildValidatorFactory(); -
The third approach allows you to specify explicitly and in
a type safe fashion the expected provider.
Optionally you can choose a custom
ValidationProviderResolver.ACMEConfiguration configuration = Validation .byProvider(ACMEProvider.class) .providerResolver( new MyResolverStrategy() ) // optionally set the provider resolver .configure(); ValidatorFactory factory = configuration.buildValidatorFactory();
Note:
-
The
ValidatorFactoryobject built by the bootstrap process should be cached and shared amongstValidatorconsumers. - This class is thread-safe.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classFindsValidationProvideraccording to the defaultValidationProviderResolverdefined in the Bean Validation specification.private static classprivate static classprivate static classValidation.NewProviderInstance<T extends ValidationProvider<?>>private static classValidation.ProviderSpecificBootstrapImpl<T extends Configuration<T>,U extends ValidationProvider<T>> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ValidatorFactoryBuilds and returns aValidatorFactoryinstance based on the default Bean Validation provider and following the XML configuration.static GenericBootstrapBuilds aConfiguration.static <T extends Configuration<T>,U extends ValidationProvider<T>>
ProviderSpecificBootstrap<T> byProvider(Class<U> providerType) Builds aConfigurationfor a particular provider implementation.private static voidNot a public API; it can be used reflectively by code that integrates with Bean Validation, e.g.
-
Constructor Details
-
Validation
public Validation()
-
-
Method Details
-
buildDefaultValidatorFactory
Builds and returns aValidatorFactoryinstance based on the default Bean Validation provider and following the XML configuration.The provider list is resolved using the default validation provider resolver logic.
The code is semantically equivalent to
Validation.byDefaultProvider().configure().buildValidatorFactory().- Returns:
ValidatorFactoryinstance- Throws:
NoProviderFoundException- if no Bean Validation provider was foundValidationException- if a Bean Validation provider was found but theValidatorFactorycannot be built
-
byDefaultProvider
Builds aConfiguration. The provider list is resolved using the strategy provided to the bootstrap state.Configuration<?> configuration = Validation .byDefaultProvider() .providerResolver( new MyResolverStrategy() ) .configure(); ValidatorFactory factory = configuration.buildValidatorFactory();The provider can be specified in the XML configuration. If the XML configuration does not exist or if no provider is specified, the first available provider will be returned.- Returns:
- instance building a generic
Configurationcompliant with the bootstrap state provided
-
byProvider
public static <T extends Configuration<T>,U extends ValidationProvider<T>> ProviderSpecificBootstrap<T> byProvider(Class<U> providerType) Builds aConfigurationfor a particular provider implementation.Optionally overrides the provider resolution strategy used to determine the provider.
Used by applications targeting a specific provider programmatically.
ACMEConfiguration configuration = Validation.byProvider(ACMEProvider.class) .providerResolver( new MyResolverStrategy() ) .configure();, whereACMEConfigurationis theConfigurationsub interface uniquely identifying the ACME Bean Validation provider. andACMEProvideris theValidationProviderimplementation of the ACME provider.- Type Parameters:
T- the type of theConfigurationcorresponding to thisValidationProviderU- the type of theValidationProviderimplementation- Parameters:
providerType- theValidationProviderimplementation type- Returns:
- instance building a provider specific
Configurationsub interface implementation
-
clearDefaultValidationProviderResolverCache
private static void clearDefaultValidationProviderResolverCache()Not a public API; it can be used reflectively by code that integrates with Bean Validation, e.g. application servers, to clear the provider cache maintained by the default provider resolver.This is a strictly unsupported API, its definition may be changed or removed at any time. Its purpose is to explore the addition of standardized methods for dealing with provider caching in a future revision of the spec.
-