I'm using Java Beans validation through hibernate-validation on a JavaFX application, so, without a framework to help with the wiring. I added these dependencies to my project:
compile group: "org.hibernate", name: "hibernate-validator", version: "6.0.2.Final"
compile group: "org.glassfish", name: "javax.el", version: "3.0.1-b08"
compile group: "javax.validation", name: "validation-api", version: "2.0.0.Final"
and I found this works to get modelObject
validated:
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
validator.validate(modelObject);
My question is, is it bad to create a new factory and validator every time I validate? Should I cache them somewhere and re-use them? How expensive and how multi-threaded are the validators?