Strange behavior of spring-boot when trying to autostart javax.validation.Validator

Here's my problem:

I am developing a spring-boot based web application, spring auto-updated annotation works in all layers except this interface "javax.validation.Validator".

When I try to authorize "javax.validation.Validator" in my validator like this:

@Component
public class BrandValidator{

@Autowired
private javax.validation.Validator validator;

      

I have this exception: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualified bean of type [javax.validation.Validator] was found for dependency

At first I thought there was no bean for this class in spring, but actually, when I list the bean available at runtime (after commenting out the Autwired annotation on my validator), I see that this bean is available:

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

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

The weird part is that I can trace perfectly the "spring" class (org.springframework.validation.Validator):

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

      

I can see that in debug mode the OptionValidatorFactoryBean has been added in context.

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

@Component
public class BrandValidator{

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

@Autowired
private javax.validation.Validator validator;

      

The javax.validation.Validator is now correctly entered (in debug mode, I can see that both validators have the same OptionalValidatorFactoryBean object). I really don't understand what happens when the context is loaded and I really don't like this workaround (I don't need the org.springframework.validation.Validator file in my class).

Any idea how to insert javax.validation.Validator correctly using spring boot?

+3


source to share


1 answer


Try adding @DependsOn(value="mvcValidator")

to the BrandValidator class.



+1


source







All Articles