As far as i know, in JEE7 Bean Validation is automatically integrated with CDI. For example if i use a CDI container, i don't need to inject and use a javax.validation.Validator to check if my bean violate some constraint. So, i don't need to do something like this:
@Inject
Validator validator;
...
SoccerPlayer player = new SoccerPlayer();
player.setFirstName(firstName);
player.setLastName(lastName);
player.setAge(age);
Set<ConstraintViolation<SoccerPlayer>> violations = validator
.validate(player);
But the following SoccerPlayer bean
public class SoccerPlayer {
@NotNull
@Size(min = 5)
private String firstName;
@NotNull
@Size(min = 5)
private String lastName;
@Max(50)
@Min(value=16, groups = GoalKeeper.class)
private int age;
private String position;
public SoccerPlayer() {
}
// Getters and setters omitted
}
could automatically validate with @Valid annotation.
public class SoccerPlayerProcessor {
public void processPlayer(@Valid SoccerPlayer player){
// Do stuff with the player.
}
}
Now in this case i can't use groups in conjuction with @Valid annotation, to diversify validation behaviour.
So, is there any method to achieve my goal? Or as an alternative, could i disable CDI integration with bean validation? Could i override default CDI javax.validation.Validator with my custom implementation?
I'm using WebSphere Liberty profile as application server.
Thank you in advance.
EDIT:
To be more accurate, this is an extract from: https://docs.jboss.org/hibernate/validator/5.1/reference/en-US/html/validator-integration.html#section-integration-with-cdi
As of version 1.1, Bean Validation is integrated with CDI (Contexts and Dependency Injection for JavaTM EE).
This integration provides CDI managed beans for Validator and ValidatorFactory and enables dependency injection in constraint validators as well as custom message interpolators, traversable resolvers, constraint validator factories and parameter name providers.
Furthermore, parameter and return value constraints on the methods and constructors of CDI managed beans will automatically be validated upon invocation.
When your application runs on a Jave EE container, this integration is enabled by default.
My question is, is there any method to disable this default behaviour? Or in alternative can i override default CDI interceptor? Or is it impossible?