[英]Caused by: NoSuchBeanDefinitionException: No qualifying bean of type xxx expected at least 1 bean which qualifies as autowire candidate
I have the following code bellow: 我有下面的代码如下:
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;
}
And have the following class created: 并创建以下类:
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;
}
}
I believe StorageProperties should be easily found, but for some reason I'm getting this error: 我认为应该容易找到StorageProperties,但是由于某些原因,我遇到了此错误:
UnsatisfiedDependencyException: Error creating bean with name 'fileSystemStorageService' defined in file [/home/x/workspace/botshop-backend-java/target/classes/far/botshop/backend/storage/FileSystemStorageService.class]: Unsatisfied dependency expressed through constructor parameter 0; UnsatisfiedDependencyException:创建文件[/home/x/workspace/botshop-backend-java/target/classes/far/botshop/backend/storage/FileSystemStorageService.class]中定义的名称为'fileSystemStorageService'的bean时出错:通过构造函数参数表示的不满意依赖性0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'far.botshop.backend.storage.StorageProperties' available: expected at least 1 bean which qualifies as autowire candidate. 嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为'far.botshop.backend.storage.StorageProperties'的合格Bean:至少应有1个有资格作为自动装配候选的Bean。
Any idea? 任何想法?
在StorageProperties类上添加@Component注释。
You are trying to 您正在尝试
@Autowired
public FileUploadController(StorageService storageService) {
this.storageService = storageService;
}
but you haven't defined StorageService as Bean. 但您尚未将StorageService定义为Bean。
So you should add @Component
annotation or equivalent over the StorageService class. 因此,您应该在StorageService类上添加@Component
批注或等效项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.