Spring Boot无法自动连线@ConfigurationProperties

时间:2019-09-16 04:27:39

标签: java spring spring-boot bean-validation spring-restcontroller

这是我的FileStorageProperties班:

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

这使我说:未通过@enableconfigurationproperties注册或标记为spring组件

这是我的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);
    }
}
}

这给了我一个错误,说:无法自动装配未找到类型的bean

这是我的项目结构:

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。


我该如何解决?

3 个答案:

答案 0 :(得分:3)

这是可以预期的,因为IP Match Condition不会使类成为Spring @ConfigurationProperties。用Component标记该类,它应该可以工作。请注意,仅当类为@Component时才能注入。

答案 1 :(得分:1)

在FileStorageProperties类中添加以下注释:

  

@Component

答案 2 :(得分:1)

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

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