Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Validation

Ruud Claassen
Ruud Claassen
9,359 Points

Could not autowire. There is more than one bean of 'Validator' type

Wanted to autowire the Validator validator, as done in the video (4:20). Reviewed the other questions, but none covers this specific case. I now receive a curly red line underneath the validator variable, with the following message on hover:

Beans: Could not autowire. There is more than one bean of 'Validator' type

   defaultValidator (ValidationAutoConfiguration.class) 
   mvcValidator (WebMvcAutoConfiguration.class)

Code does run however...

Thanks!

1 Answer

Brice Roberts
Brice Roberts
22,415 Points

Change

@Autowired
    private Validator validator;

    @Override
    public void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) {
        validatingListener.addValidator("beforeCreate", validator);
        validatingListener.addValidator("beforeSave", validator);
    }

to

@Bean
public Validator validator() {
    return new LocalValidatorFactoryBean();
}


@Override
public void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) {
    validatingListener.addValidator("beforeCreate", validator());
    validatingListener.addValidator("beforeSave", validator());
}

More info

https://stackoverflow.com/questions/39677352/spring-validator-could-not-autowire-no-beans-of-validator-type-found/39679081#39679081

Had the same issue. Brice's solution worked for me. Thanks! ??