Short answer is no. Although not originally mentioned in JSR-303 it is typically used for MVC data binding and validation. I would try the following.
Since you have JSR-303 annotations, so configure a JSR-303 validator in Spring context. In this case, it will be a LocalValidatorFactoryBean
that bootstraps a standard JSR-303 validation mechanism. Note that you still need to have a vendor/implementor of JSR-303 available in your class path (e.g. Hibernate Validator).
Implement an instance of ApplicationContextListener<ContextStartedEvent>
to receive the event when the application context is started and ready. Also, allow the bean to receive an instance of LocalValidatorFactoryBean
:
public class GenericApplicationValidator implements ApplicationListener<ContextStartedEvent> {
private ValidatorFactoryBean validatorFactoryBean;
public void onApplicationEvent(ContextStartedEvent e) {
// Refer to next step
}
public void setValidatorFactoryBean(ValidatorFactoryBean vfb) {
this.validatorFactoryBean = vfb;
}
}
In the method onApplicationEvent(ContextStartedEvent e)
:
- Fetch the instance of application context.
- Through the application context, find all the beans you need to validate.
- You have access to
validatorFactoryBean.getValidator()
- Iterate over all the beans, use the validator and validate the bean.
Remember that I have not had an experience on doing this. But it looks to me it can be one way to get close to achieve what you need.