java 我无法在春季自动装配存储库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36699042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I can't autowire repository in spring
提问by Black
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.
我正在尝试使用 spring 注释在控制器中自动装配存储库。我收到错误org.springframework.data.repository.query.QueryByExampleExecutor class not found
,找不到解决方案。
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?
我用@Repository
. 根据 spring 文档,我只有存储库接口。这是正确的吗?
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
应用上下文.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
我已经包含jpa:repositories
了正确的base-package
和component-scan
。我已经指定它是注释驱动的 ( <mvc:annotation-driven /> <tx:annotation-driven />
) 并添加了 JDBC 和 JPA 设置。我的 applicationContext.xml 从 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>
回答by Viacheslav Shalamov
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
您正在寻找的课程在这里:https: //github.com/spring-projects/spring-data-jpa/blob/master/src/main/java/org/springframework/data/jpa/repository/JpaRepository。爪哇
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).
如您所见,最新版本的 spring-data-jpa 具有它,因此您应该将失败的依赖升级到最新版本,或者稍微降级 spring-data-jpa 版本(不推荐)。
I had similar problem in my application with spring boot and mongoDB jpa repositories.
我的应用程序在使用 spring boot 和 mongoDB jpa 存储库时遇到了类似的问题。
As example, I have spring-data-jpa and spring-data-mongodb dependencies:
例如,我有 spring-data-jpa 和 spring-data-mongodb 依赖项:
<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.
但是 spring boot spring-boot-starter-data-jpa 隐式使用了比 1.9.2.RELEASE 更旧的 spring-data-mongodb 版本。最简单的修复方法是将 spring-data-mongodb 降级到 1.8.4.RELEASE 版本。
回答by tcharaf
Try to add this config in your applicationContext.xml
尝试在 applicationContext.xml 中添加此配置
<context:annotation-config/>
回答by naXa
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:
您可能会遇到NoClassDefFoundError
,如果版本spring-data-commons
是旧的然后要求spring-data-jpa
。这个异常几乎总是意味着版本不匹配(依赖地狱)。这是这种不良依赖组合的示例:
<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
).
解决方案就像spring-data-commons
从项目依赖项中删除一样简单(无论如何,您不需要显式指定此库,因为它是 的传递依赖项spring-data-jpa
)。
回答by Charnjeet Singh
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.articlewill automatically scan.
它是使用此定义基本包,您com.payforeign.article将自动扫描。