弹簧靴无法自动连接@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);
    }
}
}

这给了我错误说:无法自动连接没有找到类型的豆子

这是我的项目结构:

Spring Boot can't autowire @ConfigurationProperties - project structure

当我尝试运行它时,它给了我:


应用程序启动失败

描述:

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。


如何解决此问题?


答案 1

这是意料之中的,因为不会使课程成为春季。用 标记类,它应该可以工作。请注意,类只有在 .@ConfigurationPropertiesComponent@ComponentComponent

编辑:从 Spring 2.2+参考) 扫描 现在可以通过类路径扫描找到注释的类,作为使用 或 的替代方法。@ConfigurationPropertiesScan添加到应用程序以启用扫描。@ConfigurationProperties@ConfigurationProperties@EnableConfigurationProperties@Component


答案 2

尝试使用@ConfigurationProperties@Component进行注释

在这里,Spring Boot 是外部化配置的注释。如果您尝试将属性值从属性文件注入到类中,则可以在类级别添加构造型注释,例如 或 添加到方法中。 @ConfigurationProperties@ConfigurationProperties@Component@ConfigurationProperties@Bean


推荐