[英]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
文件
@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("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
另一个文件上传目录复制这两个配置
@ConfigurationProperties(prefix = "aed")
public class FileStoragePropertiesAedImages {
private String uploadDir;
public String getUploadDir() {
return uploadDir;
}
public void setUploadDir(String uploadDir) {
this.uploadDir = uploadDir;
}
}
@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
.你刚才添加@ ConfigurationProperties
到FileStorageProperties
。 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.