I've got a problem injecting the Validator
into the spring application bean when attempting to validate a model using JSR-303 (hibernate-validator)
My main configuration class is:
@EnableAutoConfiguration
@EnableWebMvc // <---
@EnableJpaRepositories("com.example")
@EntityScan("com.example")
public class MainConfiguration {
According to the javadocs:
/**
* Provide a custom {@link Validator} instead of the one created by default.
* The default implementation, assuming JSR-303 is on the classpath, is:
* {@link org.springframework.validation.beanvalidation.LocalValidatorFactoryBean}.
* Leave the return value as {@code null} to keep the default.
*/
Validator getValidator();
Hibernate-validator is on the classpath.
I'm trying to inject it into the Repository:
@Repository
public class UserRepositoryImpl implements UserRepositoryCustom {
@Autowired
private Validator validator;
Exception being thrown:
No qualifying bean of type [javax.validation.Validator] found for dependency:
UPDATE:
The partial work-around for this is to define this in the main configuration class:
@Bean
public Validator validator() {
return new org.springframework.validation.beanvalidation.LocalValidatorFactoryBean();
}
But integration tests (the ones which require org.springframework.test.context.web.WebAppConfiguration;
annotation and use validation logic) fail.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…