我有以下代码:
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:使用name创建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个豆,有资格作为autowire候选人。
有什么想法吗?
答案 0 :(得分:2)
答案 1 :(得分:1)
你正试图
@Autowired
public FileUploadController(StorageService storageService) {
this.storageService = storageService;
}
但您尚未将StorageService定义为Bean。
因此,您应该在StorageService类上添加@Component
注释或等效项。