春季-服务中构造函数的错误参数0需要找不到配置类型的Bean

时间:2018-09-05 08:14:00

标签: java spring spring-boot spring-config

我遵循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;
    }

}

FileSystemStorageService

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";
    }
}

3 个答案:

答案 0 :(得分:0)

根据Baeldung article,您还需要向班级添加@Configuration

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

答案 1 :(得分:0)

在StorageProperties类中添加注释@Configuration或@Component。

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

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

如果收到诸如“未找到bean或类型配置bean”之类的错误,则必须在相关类中检查@ Component,@ Service或@Repository批注。

ref:this

答案 2 :(得分:0)

如果您想要构造函数注入(例如对于 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; ...}