我不能在春天自动装配存储库

时间:2016-04-18 16:04:37

标签: java spring spring-mvc spring-data noclassdeffounderror

我正在尝试使用spring注释在控制器中自动装配存储库。我收到错误org.springframework.data.repository.query.QueryByExampleExecutor class not found我无法找到解决方案。

我收到错误:

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

控制器

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的带注释的存储库。根据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

我已将jpa:repositoriesbase-packagecomponent-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>

4 个答案:

答案 0 :(得分:12)

您的问题与依赖关系有关。

你要找的班级在这里: https://github.com/spring-projects/spring-data-jpa/blob/master/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java

正如您所看到的,最新版本的spring-data-jpa有它,因此您应该将失败的删除版本升级到最新版本或稍微降级spring-data-jpa版本(不推荐)。

我在使用spring boot和mongoDB jpa存储库的应用程序中遇到了类似的问题。

例如,我有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>

但是spring boot spring-boot-starter-data-jpa隐式使用了比1.9.2.RELEASE更早版本的spring-data-mongodb。 最简单的修复方法是将spring-data-mongodb降级为1.8.4.RELEASE版本。

答案 1 :(得分:0)

尝试在applicationContext.xml中添加此配置

<context:annotation-config/>

答案 2 :(得分:-1)

如果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>

解决方案就像从项目依赖项中删除spring-data-commons一样简单(无论如何,您不需要明确指定此库,因为它是spring-data-jpa的传递依赖项。)< / p>

答案 3 :(得分:-3)

请更改

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

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

使用此功能定义基本包 com.payforeign.article 将自动扫描。