Spring - 服务中构造函数的错误参数 0 需要找不到类型为 Configuration 的 bean

Spring - Error Parameter 0 of constructor in Service required a bean of type Configuration that could not be found

我按照 tutorial 上传文件,但最后出现以下错误:

Parameter 0 of constructor in nu.pk.cv.storage.FileSystemStorageService required a bean of type 'nu.pk.cv.storage.StorageProperties' that could not be found.


Action:

Consider defining a bean of type 'nu.pk.cv.storage.StorageProperties' in your configuration

我知道我所做的唯一区别是我使用 @RestController 而不是仅 @Controller 并且我的控制器在另一个子包中而不是在父包中。我的存储 类 在 nu.pk.cv.storage 而我的控制器在 nu.pk.cv.cv.

存储属性

package nu.pk.cv.storage;

@ConfigurationProperties("storage")
public class StorageProperties {

    private String location = "/tmp/cv-gen";

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

}

文件系统存储服务

package nu.pk.cv.storage;

@Service
public class FileSystemStorageService implements StorageService {

    private final Path rootLocation;

    @Autowired
    public FileSystemStorageService(StorageProperties properties) {
        this.rootLocation = Paths.get(properties.getLocation());
    }

    @Override
    public void store(MultipartFile file) {
        String filename = StringUtils.cleanPath(file.getOriginalFilename());
        try {
            if (file.isEmpty()) {
                throw new StorageException("Failed to store empty file " + filename);
            }
            if (filename.contains("..")) {
                // This is a security check
                throw new StorageException(
                        "Cannot store the file with relative path outside the current directory "
                                + filename);
            }
            try (InputStream inputStream = file.getInputStream()) {
                Files.copy(inputStream, this.rootLocation.resolve(filename),
                    StandardCopyOption.REPLACE_EXISTING);
            }
        }
        catch (IOException e) {
            throw new StorageException("Failed to store file " + filename, e);
        }
    }

    @Override
    public Stream<Path> loadAll() {
        try {
            return Files.walk(this.rootLocation, 1)
                .filter(path -> !path.equals(this.rootLocation))
                .map(this.rootLocation::relativize);
        }
        catch (IOException e) {
            throw new StorageException("Failed to read stored files", e);
        }

    }

    @Override
    public Path load(String filename) {
        return rootLocation.resolve(filename);
    }

    @Override
    public Resource loadAsResource(String filename) {
        try {
            Path file = load(filename);
            Resource resource = new UrlResource(file.toUri());
            if (resource.exists() || resource.isReadable()) {
                return resource;
            }
            else {
                throw new StorageFileNotFoundException(
                        "Could not read file: " + filename);

            }
        }
        catch (MalformedURLException e) {
            throw new StorageFileNotFoundException("Could not read file: " + filename, e);
        }
    }

    @Override
    public void deleteAll() {
        FileSystemUtils.deleteRecursively(rootLocation.toFile());
    }

    @Override
    public void init() {
        try {
            Files.createDirectories(rootLocation);
        }
        catch (IOException e) {
            throw new StorageException("Could not initialize storage", e);
        }
    }
}

我的控制器

package nu.pk.cv.cv;

@RestController
public class CV {
    @Autowired
    private StorageService storageService;

    @PostMapping("/api/cv/generate")
    public String generate(@RequestParam("files") MultipartFile file) {

        storageService.store(file);

        return "Mjau";
    }
}

根据 Baeldung article,您还需要将 @Configuration 添加到您的 class:

@Configuration
@ConfigurationProperties("storage")
public class StorageProperties

在 StorageProperties 添加注释 @Configuration 或 @Component class。

    @Configuration
    @ConfigurationProperties("storage")
    public class StorageProperties {

@Component
@ConfigurationProperties("storage")
public class StorageProperties {

如果您收到类似 "a bean not found or a bean of type configuration" 的错误,则必须检查相关 classes 中的 @Component、@Service 或 @Repository 注释。

参考:Spring blog

如果你想要构造函数注入(例如对于 lombok 或缺少的 setter),你必须在类型级别或在特定构造函数(如果有多个)上用 @ConstructorBinding 声明它。

@Configuration
@ConfigurationProperties("storage")
@ConstructorBinding
public class StorageProperties{

or

@Configuration
@ConfigurationProperties("storage")
public class StorageProperties{
  public StorageProperties(){...}

  @ConstructorBinding
  public StorageProperties(String location){this.location = location; ...}