I am trying to autowire repository in controller using spring annotation. I am getting error org.springframework.data.repository.query.QueryByExampleExecutor class not found
for which I couldn't find a solution.
Error that I am getting:
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'articleController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.payforeign.article.ArticleRepository com.payforeign.article.ArticleController.repository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'articleRepository': Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/data/repository/query/QueryByExampleExecutor
Controller
package com.payforeign.article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/service")
public class ArticleController {
@Autowired
private ArticleRepository repository;
//controller methods
}
Repository
I have annotated repository with @Repository
. According to spring documentation I am having only repository interface. Is it correct?
package com.payforeign.article;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ArticleRepository extends CrudRepository<Article, Long> {}
applicationContext.xml
I have included jpa:repositories
with correct base-package
and component-scan
. I have specified that it is annotation driven (<mvc:annotation-driven /> <tx:annotation-driven />
) and added JDBC and JPA settings. My applicationContext.xml is correctly loaded from web.xml
<?xml version='1.0' encoding='UTF-8' ?>
<beans ...>
<context:component-scan base-package="com.payforeign,com.payforeign.article" />
<mvc:annotation-driven />
<jpa:repositories base-package="com.payforeign.article" />
<!-- Data Source -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" >
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/payforeign" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<!-- Hibernate -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.payforeign.article" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="database" value="MYSQL" />
</bean>
</property>
</bean>
<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
Your issue is about dependencies.
The class, you are looking for is here: https://github.com/spring-projects/spring-data-jpa/blob/master/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java
As you can see, the latest version of spring-data-jpa has it, therefore you should either upgrade your failing delendency to the latest version or downdrade spring-data-jpa version a little bit (not recommended).
I had similar problem in my application with spring boot and mongoDB jpa repositories.
As example, I have spring-data-jpa and spring-data-mongodb dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.9.2.RELEASE</version>
</dependency>
But spring boot spring-boot-starter-data-jpa implicitly uses an older version of spring-data-mongodb than 1.9.2.RELEASE. The easiest way to fix was to downgrade spring-data-mongodb to 1.8.4.RELEASE version.