引起原因:NoSuchBeanDefinitionException:没有 xxx 类型的合格 bean,预计至少有 1 个符合自动装配候选资格的 bean

2024-04-05

我有以下代码:

package far.botshop.backend.controller;

/**
 */
import java.util.logging.Logger;

import far.botshop.backend.storage.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class FileUploadController {

    private final StorageService storageService;

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

并创建以下类:

package far.botshop.backend.storage;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("storage")
public class StorageProperties {
    /**
     * Folder location for storing files
     */
    private String location = "upload-dir";

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

}

我相信 StorageProperties 应该很容易找到,但由于某种原因我收到此错误:

UnsatisfiedDependencyException:使用名称创建 bean 时出错 文件中定义的“fileSystemStorageService” [/home/x/workspace/botshop-backend-java/target/classes/far/botshop/backend/storage/FileSystemStorageService.class]: 通过构造函数参数0表达的不满足的依赖关系; 嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:否 合格的bean类型 “far.botshop.backend.storage.StorageProperties”可用:预计在 至少 1 个有资格作为自动装配候选者的 bean。

任何想法?


你正在尝试

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

但您还没有将 StorageService 定义为 Bean。

所以你应该添加@ComponentStorageService 类上的注释或等效项。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

引起原因:NoSuchBeanDefinitionException:没有 xxx 类型的合格 bean,预计至少有 1 个符合自动装配候选资格的 bean 的相关文章

随机推荐