Parameter 0 of constructor required a bean of type ‘FileStoragePropertiesAedImages’

huangapple go评论63阅读模式
英文:

Parameter 0 of constructor required a bean of type 'FileStoragePropertiesAedImages'

问题

以下是您提供的内容的翻译部分:

我在几个线程中关于这个问题进行了讨论,尽管如此,我仍然无法弄清楚。

构造函数中的第0个参数在com.aed.demo.fileupload.FileStorageServiceAedImages中需要一个类型为'com.aed.demo.fileupload.FileStoragePropertiesAedImages'的bean,但找不到该bean。

操作:

考虑在您的配置中定义一个类型为'com.aed.demo.fileupload.FileStoragePropertiesAedImages'的bean。

我已经创建了一个 FileStorageService 和一个 FileStorageProperties 文件。

文件存储属性

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

    public String getUploadDir() {
        return uploadDir;
    }

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

文件存储服务

@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("无法创建上传文件的目录。", ex);
        }
    }

   ...
}

以上的设置非常有效,可以在 application.properties 中定义的 file.upload-dir=home/.. 创建目录。

我尝试使用另一个文件上传目录在 application.properties 中复制这两个配置。

第二个 FileStorageProperties 类

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

    public String getUploadDir() {
        return uploadDir;
    }

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

第二个 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("无法创建上传文件的目录。", ex);
        }
    }
   ...
}

然后出现上述错误。我需要创建三个目录,如在 application.properties 中定义:

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

前缀是正确的,为什么第二个存储服务无法初始化并且需要一个bean类型,而第一个不需要呢?

英文:

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

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

I tried duplicating these two configurations with another file upload directory in application.properties

Second FileStorageProperties Class

@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

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?

答案1

得分: 1

你正在进行的是构造函数自动装配,位于FileStorageServiceAedImages,它需要类型为FileStoragePropertiesAedImages的bean,但是你尚未定义该类型的bean。你只是在FileStorageProperties上添加了@ConfigurationProperties注解。将@Configuration注解添加到FileStorageProperties将会创建你在FileStoragePropertiesAedImages中所需的bean。

FileStoragePropertiesAedImages类上添加@Configuration注解应该可以解决这个问题。

@ConfigurationProperties(prefix = "aed")
@Configuration
public class FileStoragePropertiesAedImages {
英文:

what you are doing is constructor autowiring at FileStorageServiceAedImages which requires bean of type FileStoragePropertiesAedImages but you have not defined bean of the type. You just added @ ConfigurationProperties to the FileStorageProperties. Adding @Configuration to FileStorageProperties will create the bean you required at FileStoragePropertiesAedImages.

Add @Configuation annotation to FileStoragePropertiesAedImages class should solve the problem.

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

huangapple
  • 本文由 发表于 2020年10月10日 15:40:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64291147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定