问题
Hi I have set up a Spring MVC (4)+Postgresql+JPA app and I encountered the following situation: I want to set up custom queries for my "User" repository so added these lines to the existing (empty) UserRepository:
public interface AccountRepository extends JpaRepository<User,Long> {
@Query("select u from User u where u.Email = ?1")
User findByEmail(String emailAddress);
}
With the empty Repository, everything is OK with the server, with the verison above I got the following exception on startup:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountController': Injection of autowired dependencies failed;
nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.springapp.mvc.Repositories.AccountRepository com.springapp.mvc.AccountController.accountRepository;
nested exception is
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean] for bean with name 'org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean#0' defined in null;
nested exception is
java.lang.ClassNotFoundException: org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean
Even tough I have OptionalValidatorFactoryBean.java in the jar (extracted it) provided with maven. I'm really confused.
回答1:
today i come across the same problem , It caused by jar confliction . spring-context3.1-RELEASE.jar and spring-context4.1-RELEASE.jar , my project using spring4.1 , but spring3.1-context is also downloaded because of maven dependencies,then I remvoed spring3.1.jar ,all goes well
回答2:
Make sure you have Spring Context in your maven dependencies, like;
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.9.RELEASE</version>
</dependency>
来源:https://stackoverflow.com/questions/28255432/spring-mvc-optionalvalidatorfactorybean-not-found