【问题标题】:Spring boot Field required a bean of type that could not be foundSpring boot Field 需要一个找不到类型的 bean
【发布时间】:2019-02-09 09:17:43
【问题描述】:

我正在努力学习 Spring Boot 的 JPA 入门教程。 我知道这个问题有时在这里被问到 ('Field required a bean of type that could not be found.' error spring restful API using mongodb)

但是这些问题和我的有点不同。

结构

java
  |
  helloWorld
  |
  web/ -- HelloWorldController
  Application
  Customer
  CustomerRepository
  ServletInitializer

如您所见,我所有与 JPA 相关的包都与我的应用程序文件处于同一级别。根据教程 (https://spring.io/guides/gs/accessing-data-jpa/) 这应该可以工作

我的应用类

package helloWorld;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

    @Autowired
    CustomerRepository customerRepository;

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}

客户资料库

package helloWorld;


import org.springframework.data.repository.CrudRepository;

import java.util.List;

public interface CustomerRepository extends CrudRepository<Customer, Long> {

    List<Customer> findByLastName(String lastName);
}

当我尝试使用@Autowired 时,我收到了

***************************
APPLICATION FAILED TO START
***************************

Description:

Field customerRepository in helloWorld.Application required a bean of type 'helloWorld.CustomerRepository' that could not be found.


Action:

Consider defining a bean of type 'helloWorld.CustomerRepository' in your configuration.

此外,将scanBasePackages={"helloWorld"}) 添加到@SpringBootApplication 并没有帮助,从我所读到的内容来看,它也应该不需要。

pom.xml:

<?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>helloWorld.com.example</groupId>
    <artifactId>helloWorld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

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

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </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-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.microsoft.azure</groupId>
                <artifactId>azure-webapp-maven-plugin</artifactId>
                <version>1.1.0</version>
                <configuration>
                    <resourceGroup>maven-projects</resourceGroup>
                    <appName>${project.artifactId}-${maven.build.timestamp}</appName>
                    <region>westus</region>
                    <javaVersion>1.8</javaVersion>
                    <deploymentType>war</deploymentType>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

link to the github project

【问题讨论】:

  • 尝试使用 @Repository 注释您的 CustomerRepository。
  • 不——它不起作用。当我从教程中下载示例时,它也不包含 @Repository 它可以工作 - 但不适用于我的项目,我找不到原因
  • ServletInitializer 在那里做什么。你不应该需要它。您还可以在配置中自动连接某些东西,该配置本身用于创建依赖项。为什么你需要在那里自动接线。
  • 您能否将存储库变量包含在Controller 类中而不是Application 中,看看它是否有效?

标签: java spring spring-boot spring-data-jpa


【解决方案1】:

您正在排除 JPA 存储库的自动配置。从application.properties 中删除该行,让Spring 将CustomerRepository 制作为一个bean 并对其进行配置。

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

【讨论】:

  • @AndrewTobilko 干得好,安德鲁,我知道了。值得一票
【解决方案2】:

就我而言,我只是忘记将@Component 添加到接口的 Impl 类中!

这些错误可能是由于缺少一个或多个刻板印象注释

【讨论】:

    【解决方案3】:

    我从我的应用程序类中更改了这个类级别注释。

    发件人:

    @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
    

    收件人:

    @SpringBootApplication
    

    【讨论】:

      【解决方案4】:

      尝试在Application类上方添加@ComponentScan("package.path.to.your.repository")注解。

      - 只需确保您的存储库与写入的包路径相同 @ComponentScan

          @SpringBootApplication
          @ComponentScan("helloworld")
          public class Application extends SpringBootServletInitializer {
      
               //your stuff 
          }
      

      【讨论】:

      • 我投了反对票,因为@SpringBootApplication 类位于同一个包中,所以没有必要
      • @AndrewTobilko 我认为你犯了一个错误兄弟!尝试通过stackoverflow.com/questions/33619532/…dzone.com/articles/spring-spring-boot-and-component-scan 更新自己
      • 如果没有定义具体的包,扫描将从声明这个注解的类的包开始。
      • @AndrewTobilko 这是您从javarticles.com/2016/01/… 复制的 ComponentScan 的功能,但是您是否在您所期望的问题中看到任何 ComponentScan?! ;)
      • 我不希望这里有任何ComponentScans,因为存储库和 spring starter 都处于同一包级别
      【解决方案5】:

      我遇到了同样的问题,除了添加以下依赖项外,一切都很好。

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

      而且,它对我有用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-11
        • 2019-02-18
        • 2020-05-09
        • 2018-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-03
        相关资源
        最近更新 更多