java “ItemController”中构造函数的参数0需要类型为.“ItemRepository”的Bean,但找不到该Bean,(Spring Data Cloud Firestore)

jchrr9hc  于 2023-01-24  发布在  Java
关注(0)|答案(2)|浏览(128)

我刚接触Spring,在使用"标准" API创建了第一个简单的项目后,我尝试使用Spring Cloud GCP将所有内容与Google Firestore集成,特别是与Firestore(Spring Data Cloud Firestore)相关的内容,但我一直遇到这个错误,我无法解决它:

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

Description:

Parameter 0 of constructor in 'com.example.spring_backend.ItemController' required a bean of type 'com.example.spring_backend.ItemRepository' that could not be found.

Action:

Consider defining a bean of type 'com.example.spring_backend.ItemRepository' in your configuration.

我的项目结构如下:

我已经在application.properties中放入了项目名和GC服务的的路径。
下面是各个文件的代码:

    • 项目。Java:**
@Document(collectionName = "items")
public class Item {

    @DocumentId
    private String name;
    
    private ArrayList<String> labels;
    private Boolean classified;
    private String base64;

    // Constructors
    public Item() {
    }

    public Item(String name, String base64) {
        this.name = name;
        this.labels = null;
        this.classified = false;
        this.base64 = base64;
    }

    public Item(String name, ArrayList<String> labels, String base64) {
        this.name = name;
        this.labels = labels;
        this.classified = false;
        this.base64 = base64;
    }

    public Item(String name, Boolean classified, ArrayList<String> labels, String base64) {
        this.name = name;
        this.labels = labels;
        this.classified = classified;
        this.base64 = base64;
    }

    public Item(String name, boolean classified, ArrayList<String> labels) {
        this.name = name;
        this.labels = labels;
        this.classified = classified;
        this.base64 = null;
    }

    // Getters and Setters
}
    • 项目控制器. java:**
@CrossOrigin
@RestController
public class ItemController {

    private final ItemRepository itemRepository;

    ItemController(ItemRepository repository) {
        this.itemRepository = repository;
    }

    @GetMapping("/items")
    public Flux<Item> all() {
        System.out.print("ciao");

        return itemRepository.findAll();
    }

    @PostMapping("/save")
    public Mono<Item> newEmployee(@RequestBody Item item) {

        System.out.print("ciao");

        item.setClassified(false);
        item.setLabels(new ArrayList<String>());

        // ! Da togliere
        item.setBase64(null);

        return itemRepository.save(item);
    }
}
    • 项目存储库. java:**
public interface ItemRepository extends FirestoreReactiveRepository<Item>{
}
    • SpringBackend应用程序. java:**
@SpringBootApplication
public class SpringBackendApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBackendApplication.class, args);
    }

}
    • pom. xml文件**:
<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>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.2</version>
    </parent>

    <name>spring_backend</name>
    <groupId>com.example</groupId>
    <artifactId>spring_backend</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java.version>17</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.google.cloud</groupId>
                <artifactId>spring-cloud-gcp-dependencies</artifactId>
                <version>3.4.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>spring-cloud-gcp-data-firestore</artifactId>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>
    </dependencies>

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

</project>

@M. Deinum变更后的pom.xml:

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

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.8</version>
    </parent>

    <name>spring_backend</name>
    <groupId>com.example</groupId>
    <artifactId>spring_backend</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java.version>17</java.version>
    </properties>

    <repositories>
        <repository>
            <id>snapshots-repo</id>
            <url>https://google.oss.sonatype.org/content/repositories/snapshots</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.google.cloud</groupId>
                <artifactId>spring-cloud-gcp-dependencies</artifactId>
                <version>4.0.0-SNAPSHOT</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>spring-cloud-gcp-data-firestore</artifactId>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>
    </dependencies>

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

    <updatePolicy>always</updatePolicy>

</project>
    • 完整堆栈跟踪**:
2023-01-23T15:20:20.257+01:00  INFO 10996 --- [           main] c.e.s.SpringBackendApplication           : Starting SpringBackendApplication using Java 17.0.5 with PID 10996 (/home/beppetemp/Tirocinio_NTTData/components/backend/target/classes started by beppetemp in /home/beppetemp/Tirocinio_NTTData)
2023-01-23T15:20:20.263+01:00  INFO 10996 --- [           main] c.e.s.SpringBackendApplication           : No active profile set, falling back to 1 default profile: "default"
2023-01-23T15:20:21.510+01:00  INFO 10996 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-01-23T15:20:21.522+01:00  INFO 10996 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-01-23T15:20:21.522+01:00  INFO 10996 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.5]
2023-01-23T15:20:21.699+01:00  INFO 10996 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-01-23T15:20:21.703+01:00  INFO 10996 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1322 ms
2023-01-23T15:20:21.842+01:00  WARN 10996 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'itemController' defined in file [/home/beppetemp/Tirocinio_NTTData/components/backend/target/classes/com/example/spring_backend/ItemController.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type 'com.example.spring_backend.ItemRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2023-01-23T15:20:21.848+01:00  INFO 10996 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2023-01-23T15:20:21.884+01:00  INFO 10996 --- [           main] .s.b.a.l.ConditionEvaluationReportLogger : 

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-01-23T15:20:21.969+01:00 ERROR 10996 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

Parameter 0 of constructor in com.example.spring_backend.ItemController required a bean of type 'com.example.spring_backend.ItemRepository' that could not be found.

Action:

Consider defining a bean of type 'com.example.spring_backend.ItemRepository' in your configuration.

有人能帮我吗?

iezvtpos

iezvtpos1#

您正在使用Sping Boot ,但您的spring-cloud-gcp-dependencies与Spring Boot 2.6/2.7兼容,而不是Spring Boot 3。因此,它不工作。
您需要:
1.降级到 Spring Boot 2.7.x
1.使用与Sping Boot 3.x兼容的spring-cloud-gcp-dependencies依赖项的4.0.0-SNAPSHOT版本。
另请参见版本兼容性列表。
对于选项2,您将需要添加存储库,如这里提到的,添加到您的Maven文件中。

xjreopfe

xjreopfe2#

感谢M.代努姆的回答,我明白了这是一个依赖性问题,所以我创建了一个新的项目,其中包含Spring Initializer,并添加了Spring GCP依赖性。
生成的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.8</version>
        <relativePath/>
        <!-- lookup parent from repository -->
    </parent>

    <name>spring_backend</name>
    <groupId>com.example</groupId>
    <artifactId>spring_backend</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java.version>17</java.version>
        <spring-cloud-gcp.version>3.4.2</spring-cloud-gcp.version>
        <spring-cloud.version>2021.0.5</spring-cloud.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.google.cloud</groupId>
                <artifactId>spring-cloud-gcp-dependencies</artifactId>
                <version>${spring-cloud-gcp.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>spring-cloud-gcp-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>spring-cloud-gcp-data-firestore</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>
    </dependencies>

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

</project>

相关问题