Note that there are some explanatory texts on larger screens.

plurals
  1. POJava/Spring: Why won't Spring use the validator object I have configured?
    primarykey
    data
    text
    <p>I'm writing a web app with Java &amp; Spring 2.5.6 and using annotations for bean validation. I can get the <a href="http://wheelersoftware.com/articles/spring-bean-validation-framework.html" rel="nofollow noreferrer">basic annotation validation</a> working fine, and Spring will even call a custom Validator declared with @Validator on the target bean. But it always instantiates a brand new Validator object to do it. This is bad because the new validator has none of the injected dependencies it needs to run, and so it throws a null pointer exception on validate. I need one of two things and I don't know how to do either. </p> <ol> <li>Convince Spring to use the validator I have already configured.</li> <li>Convince Spring to honor the @Autowired annotations when it creates the new validator.</li> </ol> <p>The validator has the @Component annotation, like this.</p> <pre><code>@Component public class AccessCodeBeanValidator implements Validator { @Autowired private MessageSource messageSource; </code></pre> <p>Spring finds the validator in the component scan, injects the autowired dependencies, but then ignores it and creates a new one at validation time.</p> <p>The only thing that I can do at the moment is add a validator reference into the controller for each validator object and use that ref directly, instead of relying on the bean validation framework to call the validator for me. It looks like this.</p> <pre><code>// first validate via the annotations on the bean beanValidator.validate(accessCodeBean, result); // then validate using the specific validator class acbValidator.validate(accessCodeBean, result); if (result.hasErrors()) { </code></pre> <p>If anyone knows how to convince spring to use the existing validator, instead of creating a new one, or how to make it do the autowiring when it creates a new one, I'd love to know.</p> <p>Edit:<br> Here is the code that tells spring what validator to use for the bean.</p> <pre><code>@Validator(AccessCodeBeanValidator.class) public class AccessCodeBean { </code></pre> <p>It works, but is limited as described above.<br> And so, currently, I have the @Validator line commented out and instead I'm autowiring the validator to the controller like this.</p> <pre><code>@Resource(name="accessCodeBeanValidator") public void setAcbValidator(Validator acbValidator) { this.acbValidator = acbValidator; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload