Package javax.validation
Interface Configuration<T extends Configuration<T>>
public interface Configuration<T extends Configuration<T>>
Receives configuration information, selects the appropriate
Bean Validation provider and builds the appropriate
ValidatorFactory
.
Usage:
Configuration<?> configuration = //provided by one of the Validation bootstrap methods ValidatorFactory = configuration .messageInterpolator( new CustomMessageInterpolator() ) .buildValidatorFactory();By default, the configuration information is retrieved from
META-INF/validation.xml
.
It is possible to override the configuration retrieved from the XML file
by using one or more of the Configuration
methods.
The ValidationProviderResolver
is specified at configuration time
(see ValidationProvider
).
If none is explicitly requested, the default ValidationProviderResolver
is used.
The provider is selected in the following way:
- if a specific provider is requested programmatically using
Validation.byProvider(Class)
, find the first provider implementing the provider class requested and use it - if a specific provider is requested in
META-INF/validation.xml
>, find the first provider implementing the provider class requested and use it - otherwise, use the first provider returned by the
ValidationProviderResolver
-
Method Summary
Modifier and TypeMethodDescriptionaddMapping
(InputStream stream) Add a stream describing constraint mapping in the Bean Validation XML format.addProperty
(String name, String value) Adds a provider specific property.Build aValidatorFactory
implementation.constraintValidatorFactory
(ConstraintValidatorFactory constraintValidatorFactory) Defines the constraint validator factory.Returns configuration information stored in theMETA-INF/validation.xml
file.Returns an implementation of theConstraintValidatorFactory
interface following the defaultConstraintValidatorFactory
defined in the specification: uses the public no-arg constructor of theConstraintValidator
Returns an implementation of theMessageInterpolator
interface following the defaultMessageInterpolator
defined in the specification: use theValidationMessages
resource bundle to load keys useLocale.getDefault()
Returns an implementation of theParameterNameProvider
interface following the defaultParameterNameProvider
defined in the specification: returns names in the formarg<PARAMETER_INDEX>
wherePARAMETER_INDEX
starts at 0 for the first parameter, e.g.Returns an implementation of theTraversableResolver
interface following the defaultTraversableResolver
defined in the specification: if Java Persistence is available in the runtime environment, a property is considered reachable if Java Persistence considers the property as loaded if Java Persistence is not available in the runtime environment, all properties are considered reachable all properties are considered cascadable.Ignores data from theMETA-INF/validation.xml
file if this method is called.messageInterpolator
(MessageInterpolator interpolator) Defines the message interpolator used.parameterNameProvider
(ParameterNameProvider parameterNameProvider) Defines the parameter name provider.traversableResolver
(TraversableResolver resolver) Defines the traversable resolver used.
-
Method Details
-
ignoreXmlConfiguration
T ignoreXmlConfiguration()Ignores data from theMETA-INF/validation.xml
file if this method is called. This method is typically useful for containers that parseMETA-INF/validation.xml
themselves and pass the information via theConfiguration
methods.- Returns:
this
following the chaining method pattern.
-
messageInterpolator
Defines the message interpolator used. Has priority over the configuration based message interpolator. Ifnull
is passed, the default message interpolator is used (defined in XML or the specification default).- Parameters:
interpolator
- message interpolator implementation- Returns:
this
following the chaining method pattern
-
traversableResolver
Defines the traversable resolver used. Has priority over the configuration based traversable resolver. Ifnull
is passed, the default traversable resolver is used (defined in XML or the specification default).- Parameters:
resolver
- traversable resolver implementation- Returns:
this
following the chaining method pattern
-
constraintValidatorFactory
Defines the constraint validator factory. Has priority over the configuration based constraint factory. Ifnull
is passed, the default constraint validator factory is used (defined in XML or the specification default).- Parameters:
constraintValidatorFactory
- constraint factory implementation- Returns:
this
following the chaining method pattern
-
parameterNameProvider
Defines the parameter name provider. Has priority over the configuration based provider. Ifnull
is passed, the default parameter name provider is used (defined in XML or the specification default).- Parameters:
parameterNameProvider
- parameter name provider implementation- Returns:
this
following the chaining method pattern.- Since:
- 1.1
-
addMapping
Add a stream describing constraint mapping in the Bean Validation XML format. The stream should be closed by the client API after theValidatorFactory
has been built. The Bean Validation provider must not close the stream.- Parameters:
stream
- XML mapping stream; the given stream should support the mark/reset contract (seeInputStream.markSupported()
); if it doesn't, it will be wrapped into a stream supporting the mark/reset contract by the Bean Validation provider- Returns:
this
following the chaining method pattern- Throws:
IllegalArgumentException
- ifstream
is null
-
addProperty
Adds a provider specific property. This property is equivalent to XML configuration properties. If the underlying provider does not know how to handle the property, it must silently ignore it. Note: Using this non type-safe method is generally not recommended. It is more appropriate to use, if available, the type-safe equivalent provided by a specific provider via itsConfiguration
subclass.ValidatorFactory factory = Validation.byProvider(ACMEProvider.class) .configure() .providerSpecificProperty(ACMEState.FAST) .buildValidatorFactory();
This method is typically used by containers parsingMETA-INF/validation.xml
themselves and injecting the state to theConfiguration
object. If a property with a given name is defined both via this method and in the XML configuration, the value set programmatically has priority. Ifnull
is passed as a value, the value defined in XML is used. If no value is defined in XML, the property is considered unset.- Parameters:
name
- property namevalue
- property value- Returns:
this
following the chaining method pattern- Throws:
IllegalArgumentException
- ifname
is null
-
getDefaultMessageInterpolator
MessageInterpolator getDefaultMessageInterpolator()Returns an implementation of theMessageInterpolator
interface following the defaultMessageInterpolator
defined in the specification:- use the
ValidationMessages
resource bundle to load keys - use
Locale.getDefault()
- Returns:
- default
MessageInterpolator
implementation compliant with the specification
- use the
-
getDefaultTraversableResolver
TraversableResolver getDefaultTraversableResolver()Returns an implementation of theTraversableResolver
interface following the defaultTraversableResolver
defined in the specification:- if Java Persistence is available in the runtime environment, a property is considered reachable if Java Persistence considers the property as loaded
- if Java Persistence is not available in the runtime environment, all properties are considered reachable
- all properties are considered cascadable.
- Returns:
- default
TraversableResolver
implementation compliant with the specification
-
getDefaultConstraintValidatorFactory
ConstraintValidatorFactory getDefaultConstraintValidatorFactory()Returns an implementation of theConstraintValidatorFactory
interface following the defaultConstraintValidatorFactory
defined in the specification:- uses the public no-arg constructor of the
ConstraintValidator
- Returns:
- default
ConstraintValidatorFactory
implementation compliant with the specification
- uses the public no-arg constructor of the
-
getDefaultParameterNameProvider
ParameterNameProvider getDefaultParameterNameProvider()Returns an implementation of theParameterNameProvider
interface following the defaultParameterNameProvider
defined in the specification:- returns names in the form
arg<PARAMETER_INDEX>
wherePARAMETER_INDEX
starts at 0 for the first parameter, e.g.arg0
,arg1
etc.
- Returns:
- default
ParameterNameProvider
implementation compliant with the specification - Since:
- 1.1
- returns names in the form
-
getBootstrapConfiguration
BootstrapConfiguration getBootstrapConfiguration()Returns configuration information stored in theMETA-INF/validation.xml
file. Note:
Implementations are encouraged to lazily build this object to delay parsing.- Returns:
- returns an instance of
BootstrapConfiguration
; this method never returnsnull
; if there is noMETA-INF/validation.xml
the different getters of the returned instance will returnnull
respectively an empty set or map - Since:
- 1.1
-
buildValidatorFactory
ValidatorFactory buildValidatorFactory()Build aValidatorFactory
implementation.- Returns:
- the
ValidatorFactory
- Throws:
ValidationException
- if theValidatorFactory
cannot be built
-