gpt4 book ai didi

java - 如何修复 “Field … required a bean of type … that could not be found” 异常 Spring Boot

转载 作者:行者123 更新时间:2023-12-05 03:51:19 24 4
gpt4 key购买 nike

我正在使用有关@Autowired 和@Primary 注释的spring boot 教程
下面你可以找到我的主类

package com.example.springtest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import service.Animal;

@SpringBootApplication
public class SpringtestApplication {
@Autowired
private Animal animal;

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

}

}

动物界面(我应该 Autowiring 的界面)

package service;

public interface Animal {


String characteristics();
}

Dog 类(Animal 接口(interface)的主要实现)

package service;


import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

@Primary
@Service
public class Dog implements Animal {

@Override
public String characteristics() {
return "Bark";
}
}

Cat类(Animal接口(interface)的第二次实现)

package service;


import org.springframework.stereotype.Service;

@Service
public class Cat implements Animal {

@Override
public String characteristics() {
return "Meow";
}
}

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.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springtest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springtest</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

</dependencies>

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

</project>

当我运行应用程序时,动物注入(inject)出现问题,我收到以下错误:

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

Description:

Field animal in com.example.springtest.SpringtestApplication required a bean of type 'service.Animal' that could not be found.

The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'service.Animal' in your configuration.

Spring 不能 Autowiring 的原因是什么?

最佳答案

您的配置文件只会选择子包中的服务。

您的SpringtestApplication 应用程序当前位于package com.example.springtest 包中,这意味着它只会 Autowiring 该包下的bean。例如 com.example.springtest.services

但是,看起来您的实际服务 Dog 服务位于包 service 中,因此不会被看到。

尝试将您的服务移动到 com.example.springtest.services

关于java - 如何修复 “Field … required a bean of type … that could not be found” 异常 Spring Boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62957855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com