This section describes the metadata annotations and deployment descriptor entries that allow an application to obtain instances of the Bean Validation Validator and ValidatorFactory types.
Applications that need to use those interfaces can find appropriate objects by looking up the name java:comp/Validator for Validator and java:comp/ValidatorFactory for ValidatorFactory, or by requesting the injection of an object of the appropriate type via the Resource annotation. The authenticationType and shareable elements of the Resource annotation must not be specified.
@Resource ValidatorFactory validatorFactory;
@Resource Validator validator;
For Validator objects, the default validation context is used. This means that all such Validators will be equivalent to those obtained by first acquiring a ValidatorFactory and then invoking the getValidator method on it with no arguments. In other words, the following two code snippets are equivalent:
// obtaining a Validator directly
Context initCtx = new InitialContext();
Validator validator = (Validator)initCtx.lookup("java:comp/Validator");
// obtaining a Validator from a ValidatorFactory
Context initCtx = new InitialContext();
Validator validator =
((ValidatorFactory) initCtx.lookup("java:comp/ValidatorFactory")).getValidator();
A Validator or ValidatorFactory object reference may also be declared in a deployment descriptor in the same way as a resource environment reference.
In order to customize the returned ValidatorFactory, an ejb, web or application client module may specify a Bean Validation XML deployment descriptor. The name of the descriptor is WEB-INF/validation.xml for web modules, META-INF/validation.xml for all other types of modules. A validation deployment descriptor only affects ValidatorFactory instances in that module. There is no per-application validation deployment descriptor.