简体   繁体   English

构造函数的参数 0 需要一个类型为“FileStoragePropertiesAedImages”的 bean

[英]Parameter 0 of constructor required a bean of type 'FileStoragePropertiesAedImages'

I have been on stack in several threads regarding this question although I still can not figure it out.我一直在关于这个问题的几个线程堆栈中,尽管我仍然无法弄清楚。

Parameter 0 of constructor in com.aed.demo.fileupload.FileStorageServiceAedImages required a bean of type 'com.aed.demo.fileupload.FileStoragePropertiesAedImages' that could not be found.

Action:

Consider defining a bean of type 'com.aed.demo.fileupload.FileStoragePropertiesAedImages' in your configuration.

I have created a FileStorageService and a FileStorageProperties files我创建了一个FileStorageService和一个FileStorageProperties文件

File Storage Properties文件存储属性

@ConfigurationProperties(prefix = "file")
public class FileStorageProperties {
    private String uploadDir;

    public String getUploadDir() {
        return uploadDir;
    }

    public void setUploadDir(String uploadDir) {
        this.uploadDir = uploadDir;
    }
}

File Storage Service文件存储服务


@Service
public class FileStorageService {

    private final Path fileStorageLocation;

    @Autowired
    public FileStorageService(FileStorageProperties fileStorageProperties) {
        
        
        this.fileStorageLocation = Paths.get(fileStorageProperties.getUploadDir())
                .toAbsolutePath().normalize();

        try {
            Files.createDirectories(this.fileStorageLocation);
        } catch (Exception ex) {
            throw new FileStorageException("Could not create the directory where the uploaded files will be stored.", ex);
        }
    }

   ...
}

the above set up works like a charm and creates the directory file.upload-dir=home/.. that it's defined in the application.properties上述设置就像一个魅力,并创建目录file.upload-dir=home/..它在application.properties定义

I tried duplicating these two configurations with another file upload directory in application.properties我尝试用application.properties另一个文件上传目录复制这两个配置

Second FileStorageProperties Class第二个 FileStorageProperties 类

@ConfigurationProperties(prefix = "aed")
public class FileStoragePropertiesAedImages {
    private String uploadDir;

    public String getUploadDir() {
        return uploadDir;
    }

    public void setUploadDir(String uploadDir) {
        this.uploadDir = uploadDir;
    }
}

Second FileStorageService第二个文件存储服务

@Service
public class FileStorageServiceAedImages {
    private final Path fileStorageLocation;

    @Autowired
    public FileStorageServiceAedImages(FileStoragePropertiesAedImages fileStoragePropertiesAED) {
        this.fileStorageLocation = Paths.get(fileStoragePropertiesAED.getUploadDir())
                .toAbsolutePath().normalize();

        try {
            Files.createDirectories(this.fileStorageLocation);
        } catch (Exception ex) {
            throw new FileStorageException("Could not create the directory where the uploaded files will be stored.", ex);
        }
    }
   ...
}

and then the above error is thrown.然后抛出上述错误。 I need to create 3 directories that I've defined like so in application.properties我需要创建 3 个我在application.properties定义的目录

file.upload-dir=home/..
mobile.upload-dir=mobile/..
aed.upload-dir=aed/..

prefixes are correct, why the second storage service can not be initialized and requires a bean type and the first one does not?前缀是正确的,为什么第二个存储服务不能被初始化并且需要一个 bean 类型而第一个没有?

what you are doing is constructor autowiring at FileStorageServiceAedImages which requires bean of type FileStoragePropertiesAedImages but you have not defined bean of the type.你在做什么是自动装配构造FileStorageServiceAedImages需要类型的豆FileStoragePropertiesAedImages但尚未定义的类型的豆。 You just added @ ConfigurationProperties to the FileStorageProperties .你刚才添加@ ConfigurationPropertiesFileStorageProperties Adding @Configuration to FileStorageProperties will create the bean you required at FileStoragePropertiesAedImages .@Configuration添加到FileStorageProperties将创建您在FileStoragePropertiesAedImages所需的 bean。

Add @Configuation annotation to FileStoragePropertiesAedImages class should solve the problem.FileStoragePropertiesAedImages类添加@Configuation注释应该可以解决问题。

@ConfigurationProperties(prefix = "aed")
@Configuation
public class FileStoragePropertiesAedImages {

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 "" 中构造函数的参数 0 需要一个无法找到的类型为 "" 的 bean - Parameter 0 of constructor in "" required a bean of type "" that could not be found “ ”中构造函数的参数 0 需要找不到类型为“ ”的 bean - Parameter 0 of constructor in ' ' required a bean of type ' ' that could not be found Service 中构造函数的参数 0 需要一个 Repository 类型的 bean - Parameter 0 of constructor in Service required a bean of Repository type 构造函数中的参数 3 需要一个无法找到的“包”类型的 bean - Parameter 3 of constructor in required a bean of type 'package' that could not be found Controller 中构造函数的参数 0 需要一个找不到的 Service class 类型的 bean - Parameter 0 of constructor in Controller required a bean of type Service class that could not be found 服务中构造函数的参数 0 需要无法找到类型存储库的 bean - Parameter 0 of constructor in service required a bean of type repository that could not be found “服务”中构造函数的参数 0 需要找不到类型为“存储库”的 bean - Parameter 0 of constructor in 'Service' required a bean of type 'Repository' that could not be found *Service 中构造函数的参数 0 需要找不到类型为“*Repository”的 bean - Parameter 0 of constructor in *Service required a bean of type '*Repository' that could not be found B类中构造函数的参数0需要一个找不到A类类型的Bean - Parameter 0 of constructor in Class B required a bean of type Class A that could not be found ... 中构造函数的参数 5 需要找不到类型为“...Mapper”的 bean - Parameter 5 of constructor in … required a bean of type '…Mapper' that could not be found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM