【问题标题】:Unable to Autowire an interface which extends CRUD repository无法自动装配扩展 CRUD 存储库的接口
【发布时间】:2017-05-23 07:49:12
【问题描述】:

我是 Spring Boot 的新手,正在尝试构建一个演示应用程序。我想使用休眠连接到 mysql 数据库并从表中获取内容。这是我的项目结构的样子:

DemoApplication.java 类如下所示:

    package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

HibernateTestController.java 如下所示:

    package com.example.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.example.repositories.PatchRepository;

@Controller
public class HibernateTestController {

    @Autowired
    PatchRepository patchRepository;

    @RequestMapping(value={"/hibernateTest"})
    public ModelAndView home() {
        //List<Patch> patches = patchRepository.findAll();
        return new ModelAndView("hibernateTest");
    }
}

PatchRepository.java 如下所示:

    package com.example.repositories;
import java.util.List;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.example.models.Patch;

@Repository
public interface PatchRepository extends CrudRepository<Patch, Long> {
    List<Patch> findAll();
}

现在,每当我启动应用程序时,都会收到一条错误消息:

***************************应用程序启动失败


说明:

字段patchRepository in com.example.controllers.HibernateTestController 需要一个 bean 输入 'com.example.repositories.PatchRepository' 不能 找到了。

行动:

考虑定义一个 bean 类型 'com.example.repositories.PatchRepository' 在您的配置中。

我无法弄清楚为什么 Autowire for PatchRepository 不起作用。我认为我的项目结构是正确的,并且 PatchRepository 应该被 spring 自动检测为 bean。

这是 POM.xml 的样子:

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.2.RELEASE</version>
    <relativePath /> 
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jersey</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>jquery</artifactId>
        <version>3.1.1-1</version>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.3.7</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-commons-core</artifactId>
        <version>1.4.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.10.6.RELEASE</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

【问题讨论】:

  • 你不需要接口上的@Repository注解,你应该删除findAll方法,因为CrudRepository已经包含了这样一个返回类型不同的方法。
  • @JensSchauder -- 我按照你的建议做了,没有任何区别。我仍然遇到同样的错误。
  • 将您的pom.xml 添加到问题中。

标签: spring hibernate spring-boot spring-data


【解决方案1】:

让我们尝试添加“@ComponentScan”

@SpringBootApplication
@ComponentScan("com.example")
public class DemoApplication {
public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
}

}

【讨论】:

    【解决方案2】:

    我按如下方式重构了我的代码并且一切正常:

    package com.example.repositories;
    import java.util.List;
    
    import org.springframework.data.repository.CrudRepository;
    import org.springframework.stereotype.Repository;
    
    import com.example.models.Patch;
    public interface PatchRepository extends CrudRepository<Patch, Long> {
        List<Patch> findAll();
    }
    
    
        <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>demo</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.4.3.RELEASE</version>
            <relativePath />
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.webjars</groupId>
                <artifactId>jquery</artifactId>
                <version>3.1.1-1</version>
            </dependency>
            <dependency>
                <groupId>org.webjars</groupId>
                <artifactId>bootstrap</artifactId>
                <version>3.3.7</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-security</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    
    </project>
    
    
        spring.thymeleaf.cache: false
    
    ##DB connections##
    spring.datasource.url=jdbc:mysql://localhost/testdb
    spring.datasource.username=root
    spring.datasource.password=root
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    
    # Keep the connection alive if idle for a long time (needed in production)
    spring.datasource.testWhileIdle = true
    spring.datasource.validationQuery = SELECT 1
    
    # Hibernate Configuration
    spring.jpa.hibernate.ddl-auto=update
    packagesToScan=com.example
    hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
    hibernate.show_sql=false
    hibernate.format_sql=true
    
    # Naming strategy
    spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
    
    entitymanager.packagesToScan=com.example
    
    # Use spring.jpa.properties.* for Hibernate native properties (the prefix is
    # stripped before adding them to the entity manager)
    
    #logging.level.org.springframework.web=DEBUG
    #logging.level.org.hibernate=DEBUG
    
    
    
     package com.example.controllers;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    import com.example.models.Patch;
    import com.example.repositories.PatchRepository;
    
    @Controller
    public class HibernateTestController {
    
        @Autowired
        PatchRepository patchRepository;
    
        @RequestMapping(value={"/hibernateTest"})
        public ModelAndView home() {
            List<Patch> patches = patchRepository.findAll();
            return new ModelAndView("hibernateTest");
        }
    }
    

    Application.properties 如下所示:

    spring.thymeleaf.cache: false
    
    ##DB connections## spring.datasource.url=jdbc:mysql://localhost/testdb spring.datasource.username=root spring.datasource.password=root
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    
    # Keep the connection alive if idle for a long time (needed in production) spring.datasource.testWhileIdle = true
    spring.datasource.validationQuery = SELECT 1
    
    # Hibernate Configuration spring.jpa.hibernate.ddl-auto=update packagesToScan=com.example
    hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
    hibernate.show_sql=false hibernate.format_sql=true
    
    # Naming strategy spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
    
    entitymanager.packagesToScan=com.example
    
    # Use spring.jpa.properties.* for Hibernate native properties (the prefix is
    # stripped before adding them to the entity manager)
    
    #logging.level.org.springframework.web=DEBUG
    #logging.level.org.hibernate=DEBUG
    

    【讨论】:

    • 那么,您究竟做了哪些改变才能让它发挥作用?
    • @AdrianShum -- 之前我使用 DatabaseConfig.java 文件来初始化数据源。我摆脱了 DatabaseConfig 类,并将 hibernate 的所有初始化移至 application.properties。
    【解决方案3】:

    尝试在您的 DemoApplication 类中使用存储库扫描

    @SpringBootApplication
    @EnableJpaRepositories("com.example.repositories")
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
    

    【讨论】:

      【解决方案4】:

      确保你有

          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-data-jpa</artifactId>
          </dependency>
      

          <dependency>
              <groupId>mysql</groupId>
              <artifactId>mysql-connector-java</artifactId>
          </dependency>
      

      进入你的 pom.xml

      正确的配置

      spring.datasource.url=jdbc:mysql://localhost:3306/test
      spring.datasource.username=root
      spring.datasource.password=root
      spring.datasource.driver-class-name=com.mysql.jdbc.Driver
      

      您的 Patch 类使用 @Entity 注释,并具有使用 @Id 注释的 Id 属性。

      一切都应该正常。

      【讨论】:

      • 不明白为什么会收到反对票。这是您能够自动装配和使用扩展 CrudRepository 的接口所需的最低配置。
      猜你喜欢
      • 2017-03-28
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      • 2020-04-11
      • 1970-01-01
      • 2017-09-01
      • 2015-07-17
      • 2017-07-17
      相关资源
      最近更新 更多