Spring Boot Field需要一个找不到类型的bean

时间:2018-09-04 10:51:36

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

我正在阅读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);
    }
}

CustomerRepository

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

5 个答案:

答案 0 :(得分:3)

您将排除JPA存储库的自动配置。从application.properties中删除该行,以使Spring使CustomerRepository成为bean并对其进行配置。

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

答案 1 :(得分:1)

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

这些错误可能是由于缺少一个或多个定型注释

答案 2 :(得分:1)

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

发件人:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })

收件人:

@SpringBootApplication

答案 3 :(得分:0)

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

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

而且,它对我有用

答案 4 :(得分:-1)

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

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

    @SpringBootApplication
    @ComponentScan("helloworld")
    public class Application extends SpringBootServletInitializer {

         //your stuff 
    }