I can't autowire repository in spring
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.
Related videos on Youtube
Black
Updated on September 17, 2022Comments
-
Black over 1 year
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 correctbase-package
andcomponent-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>
-
duffymo about 8 yearsThe stack trace is clear: You're missing the Spring Data JARs from your CLASSPATH.
-
chrylis -cautiouslyoptimistic- about 8 years
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. -
Black about 8 yearsI have upgraded
spring-data-commons
from version 1.11.4 to 1.12.1 and it fixed the issue. Thanks @chrylis
-
-
Black about 8 yearsAccording to answer on http://stackoverflow.com/a/7456501/1997088
<context:component-scan>
can do job of<context:annotation-config/>
and annotation-config is then not required -
sreejagaths about 4 yearsI 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.