11

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>
naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
Black
  • 9,541
  • 3
  • 54
  • 54
  • 1
    The stack trace is clear: You're missing the Spring Data JARs from your CLASSPATH. – duffymo Apr 18 '16 at 16:07
  • 2
    `NoClassDefFoundError` nearly always means a version mismatch. In this case, I believe you need the RC versions (or at least the very latest release versions) to get query-by-example. – chrylis -cautiouslyoptimistic- Apr 18 '16 at 16:22
  • 1
    I have upgraded `spring-data-commons` from version 1.11.4 to 1.12.1 and it fixed the issue. Thanks @chrylis – Black Apr 18 '16 at 17:16

4 Answers4

12

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.

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
Viacheslav Shalamov
  • 4,149
  • 6
  • 44
  • 66
0

Try to add this config in your applicationContext.xml

<context:annotation-config/>
naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
tcharaf
  • 630
  • 1
  • 5
  • 11
  • According to answer on [http://stackoverflow.com/a/7456501/1997088](http://stackoverflow.com/a/7456501/1997088) `` can do job of `` and annotation-config is then not required – Black Apr 18 '16 at 17:14
0

You may encounter NoClassDefFoundError if the version of spring-data-commons is older then required by spring-data-jpa. This exception nearly always means a version mismatch (dependency hell). Here's an example of such a bad dependency combination:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-commons</artifactId>
    <version>1.6.2.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>1.11.8.RELEASE</version>
</dependency>

The solution is as simple as removing spring-data-commons from your project dependencies (anyway you don't need to explicitly specify this library cause it's a transitive dependency of spring-data-jpa).

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
  • I was referring multiple spring-data dependencies namely spring-boot-starter-data-jpa and spring-boot-starter-data-rest, from my pom, which caused the trouble. Removal of the unwanted solved the issue in my case. This answer helped me getting this idea. – sreejagaths Feb 12 '20 at 04:46
-3

Please change the

<context:component-scan base-package="com.payforeign,com.payforeign.article" />

to

<context:component-scan base-package="com.payforeign" />

It is define the base package using this you com.payforeign.article will automatically scan.

Charnjeet Singh
  • 3,056
  • 6
  • 35
  • 65