簡體   English   中英

原因:NoSuchBeanDefinitionException:xxx類型的合格bean至少應包含1個符合自動裝配候選條件的bean。

[英]Caused by: NoSuchBeanDefinitionException: No qualifying bean of type xxx expected at least 1 bean which qualifies as autowire candidate

我有下面的代碼如下:

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:創建文件[/home/x/workspace/botshop-backend-java/target/classes/far/botshop/backend/storage/FileSystemStorageService.class]中定義的名稱為'fileSystemStorageService'的bean時出錯:通過構造函數參數表示的不滿意依賴性0; 嵌套的異常是org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有類型為'far.botshop.backend.storage.StorageProperties'的合格Bean:至少應有1個有資格作為自動裝配候選的Bean。

任何想法?

在StorageProperties類上添加@Component注釋。

您正在嘗試

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

但您尚未將StorageService定義為Bean。

因此,您應該在StorageService類上添加@Component批注或等效項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM