弹簧靴无法自动连接@ConfigurationProperties
这是我的课程:FileStorageProperties
@Data
@ConfigurationProperties(prefix = "file")
public class FileStorageProperties {
private String uploadDir;
}
这让我说:没有通过@enableconfigurationproperties注册或标记为弹簧组件。
这是我的:FileStorageService
@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);
}
}
public String storeFile(MultipartFile file) {
// Normalize file name
String fileName = StringUtils.cleanPath(file.getOriginalFilename());
try {
// Check if the file's name contains invalid characters
if(fileName.contains("..")) {
throw new FileStorageException("Sorry! Filename contains invalid path sequence " + fileName);
}
// Copy file to the target location (Replacing existing file with the same name)
Path targetLocation = this.fileStorageLocation.resolve(fileName);
Files.copy(file.getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING);
return fileName;
} catch (IOException ex) {
throw new FileStorageException("Could not store file " + fileName + ". Please try again!", ex);
}
}
public Resource loadFileAsResource(String fileName) {
try {
Path filePath = this.fileStorageLocation.resolve(fileName).normalize();
Resource resource = new UrlResource(filePath.toUri());
if(resource.exists()) {
return resource;
} else {
throw new MyFileNotFoundException("File not found " + fileName);
}
} catch (MalformedURLException ex) {
throw new MyFileNotFoundException("File not found " + fileName, ex);
}
}
}
这给了我错误说:无法自动连接没有找到类型的豆子。
这是我的项目结构:
当我尝试运行它时,它给了我:
应用程序启动失败
描述:
com.mua.cse616.Service.FileStorageService 中构造函数的参数 0 需要一个类型为 “com.mua.cse616.Property.FileStorageProperties” 的 Bean,但找不到。
注射点具有以下注释: - @org.springframework.beans.factory.annotation.Autowired(required=true)
行动:
请考虑在配置中定义类型为'com.mua.cse616.Property.FileStorageProperties'的bean。
如何解决此问题?