3

Here's my problem:

I develop a web application based on spring-boot, the autowired annotation of spring works in every layers except for this interface "javax.validation.Validator".

When I'm trying to autowired "javax.validation.Validator" in my validator like this:

@Component
public class BrandValidator{

@Autowired
private javax.validation.Validator validator;

I've got this exception: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.validation.Validator] found for dependency

First I was thinking that no bean was available for this class in the spring context but actually when I list the bean available at the run time (after commenting the Autowired annotation on my validator) I see that this bean is available :

"mvcValidator" with the following type OptionalValidatorFactoryBean (this class extend LocalValidatorFactoryBean and LocalValidatorFactoryBean implements javax.validation.Validator and org.springframework.validation.Validator you can see here http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html)

I have try to add the @Qualifier(value ="mvcValidator") annotation but I got the same exception.

The strange part is that I m able to autowire the "spring" validator (org.springframework.validation.Validator) class perfectly well :

@Autowired
private org.springframework.validation.Validator validatorSpring;

I see in debug mode that the OptionalValidatorFactoryBean present in the context is injected.

And finally if I autowire org.springframework.validation.Validator just before javax.validation.Validator like this :

@Component
public class BrandValidator{

//workaround
@Autowired
private org.springframework.validation.Validator validatorSpring;

@Autowired
private javax.validation.Validator validator;

Now javax.validation.Validator is correctly injected(In debug mode I see that both validators the same object OptionalValidatorFactoryBean). I really don't understand what is going when the context is loaded and I really don't like this workaround (I don't need org.springframework.validation.Validator in my class).

Any idea how correctly inject javax.validation.Validator with spring boot ?

Mattatyahu
  • 71
  • 4
  • How do you inject OptionalValidatorFactoryBean, by XML or Java config? Also, have you tried the solution in [Spring-Boot How to properly inject javax.validation.Validator](http://stackoverflow.com/questions/23604540/spring-boot-how-to-properly-inject-javax-validation-validator) ? It works for me. – Adi Sutanto Nov 04 '15 at 10:01

1 Answers1

1

Try adding a @DependsOn(value="mvcValidator") on the BrandValidator class.

Akhil
  • 29
  • 3