【问题标题】:SpringBoot Bean Type Can't be found找不到 Spring Boot Bean 类型
【发布时间】:2020-04-13 18:08:40
【问题描述】:

我在 Spring 网站上关注此 SpringBoot Demo 以了解如何创建一个接受上传的文件。我收到一条错误消息:

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

Description:

Parameter 0 of constructor in com.example.springdemouploadingfiles.FileUploadController required a bean of type 'com.example.springdemouploadingfiles.storage.StorageService' that could not be found.


Action:

Consider defining a bean of type 'com.example.springdemouploadingfiles.storage.StorageService' in your configuration.


Execution failed for task ':bootRun'.

Process 'command '/Applications/IntelliJ IDEA CE.app/Contents/jbr/Contents/Home/bin/java'' finished with non-zero exit value 1
  • 我尝试通过向 FileUploadController 添加@Bean 注释来修复它。
  • 我尝试添加以下依赖项 implementation 'org.springframework.boot:spring-boot-starter-data-jpa' runtimeOnly 'com.h2database:h2' 构建.gradle
  • 注意:我不想将数据库连接到这个演示项目。该演示承认它不是在此演示中尝试连接数据库,而是在生产环境中我们会这样做。
  • 我尝试在存储服务接口上方添加@Service 注释。
  • 我尝试在 StorageService 接口上方添加@Component
  • 我尝试在主应用程序类SpringDemoUploadingFilesApplication.java 上方添加@ComponentScan("com.example.springdemouploadingfiles") 注释。

无论如何,我都希望得到一些帮助来解决这个错误,以便我可以运行演示。下面是我的配置。

build.gradle

plugins {
    id 'org.springframework.boot' version '2.2.6.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    runtimeOnly 'com.h2database:h2'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}

FileUploadController.java

@Controller
public class FileUploadController {

    private final StorageService storageService;

    @Autowired
    public FileUploadController(StorageService storageService) {
        this.storageService = storageService;
    }

SpringDemoUploadingFilesApplication.java

@SpringBootApplication
@EnableConfigurationProperties(StorageProperties.class)
public class SpringDemoUploadingFilesApplication {

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

    @Bean
    CommandLineRunner init(StorageService storageService) {
        return (args) -> {
            storageService.deleteAll();
            storageService.init();
        };
    }
}

StorageService接口

@Service
public interface StorageService {
// implements interface. 
}

【问题讨论】:

  • StorageService 是您自己创建的类吗?如果是这样,请在该类的顶部添加 @Component。
  • storageService的代码在哪里?
  • 是的,我有一个 StorageService 接口,我只是尝试了 StorageService 上面的@Service 注解,但没有成功。
  • @AbigailHowe:您需要创建一个实现 StorageService 的具体类并将 Service 注释放在那里。请参阅下面的答案。

标签: java spring spring-boot gradle


【解决方案1】:

也许你可以尝试添加@ComponentScan("com.example.springdemouploadingfiles"),如下所示,这是为了明确向 Spring Boot 提及你应该扫描那个包

@ComponentScan("com.example.springdemouploadingfiles")
@SpringBootApplication
@EnableConfigurationProperties(StorageProperties.class)
public class SpringDemoUploadingFilesApplication {

    //all above processes here
}

【讨论】:

【解决方案2】:

我自己一直在用头撞砖墙,我认为这个特定的教程有点……嗯。

它声称您可以从头开始执行这些步骤,并且与从 GitHub 下载源代码一样。

与大多数 Spring 入门指南一样,您可以从头开始并完成每个步骤,也可以绕过您已经熟悉的基本设置步骤。无论哪种方式,您最终都会得到工作代码。

但是,稍后它明确指出(在“创建文件上传控制器”部分)...

初始应用程序已经包含一些类来处理在磁盘上存储和加载上传的文件。它们都位于 com.example.uploadingfiles.storage 包中。您将在新的 FileUploadController 中使用它们

所以,这里偏离了最初的前提,因为你从 GitHub 下载的源代码的 ./initial 目录中有类,但教程中没有提到它们。

一旦您将这些文件从 com.example.uploadingfiles.storage 类路径复制到您的项目中,它应该可以正确编译。

【讨论】:

    【解决方案3】:

    您可能缺少StorageService 的实现:

    您将需要提供 StorageService 以便控制器可以与存储层(例如文件系统)进行交互。以下清单(来自 src/main/java/com/example/uploadingfiles/storage/StorageService.java)显示了该接口:

    你可以从:

    @Service
    public class StorageServiceImpl implements StorageService {
        // Implement all the methods from StorageService.
    }
    

    【讨论】:

    • 我有,只是试了@Service注解没有用。
    • @AbigailHowe:您更新的问题显示您在界面上添加了@Service 注释。您需要创建一个实现此接口的具体类并在其中添加注释。
    【解决方案4】:

    我有一个类似的错误,一切似乎都是正确的。一切都是正确的。我通过在 Eclipse IDE 中运行 maven clean 和 maven Build 解决了这个问题。 如果您在运行 maven clean 和 maven Build 时遇到错误,我邀请您查看此内容(如果您使用 eclipse IDE): Spring Maven clean error - The requested profile "pom.xml" could not be activated because it does not exist

    【讨论】:

      猜你喜欢
      • 2018-06-12
      • 2017-11-05
      • 2022-11-10
      • 2018-05-11
      • 1970-01-01
      • 2019-02-09
      • 2018-12-13
      • 2018-10-01
      • 1970-01-01
      相关资源
      最近更新 更多