【问题标题】:Caused by: NoSuchBeanDefinitionException: No qualifying bean of type xxx expected at least 1 bean which qualifies as autowire candidate引起:NoSuchBeanDefinitionException:没有 xxx 类型的合格 bean 预期至少 1 个 bean 有资格作为自动装配候选者
【发布时间】:2017-10-18 21:14:39
【问题描述】:

我有以下代码:

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 有资格作为自动装配候选者。

有什么想法吗?

【问题讨论】:

    标签: java spring spring-mvc dependency-injection


    【解决方案1】:

    你正在尝试

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

    但您尚未将 StorageService 定义为 Bean。

    因此,您应该在 StorageService 类上添加 @Component 注释或等效项。

    【讨论】:

      【解决方案2】:

      在 StorageProperties 类上添加 @Component 注解。

      【讨论】:

      • @ConfigurationProperties 应与@EnableConfigurationProperties 一起使用
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-13
      • 2015-03-21
      • 2019-09-25
      • 2020-05-31
      • 2021-01-30
      • 1970-01-01
      相关资源
      最近更新 更多