...中的构造函数的参数0需要一个类型为...的Bean,无法找到

时间:2018-11-16 20:52:54

标签: file spring-boot

我正在浏览pivotal 'guide' project,这是我第一次在Spring中上传文件。

我已经按照指南的指定完全实现了代码,但是仍然出现以下错误:

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

Consider defining a bean of type 'hello.storage.StorageProperties' in your configuration.

我已经审查了许多此类文章,似乎共同的话题是将我的“应用程序”类移至根软件包,但该软件包已经存在。这是我的项目: UploadingFiles1Application.java

    package hello;
    // multiple imports
    @SpringBootApplication
    public class UploadingFiles1Application {

    public static void main(String[] args) {
        SpringApplication.run(UploadingFiles1Application.class, args);
    }

    @Bean
    CommandLineRunner init(StorageService storageService) {
        return (args) -> {
            storageService.deleteAll();
            storageService.init();
        };
    }
}

这是我的服务

package hello.storage;

import org.springframework.core.io.Resource;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Path;
import java.util.stream.Stream;

public interface StorageService {

    void init();

    void store(MultipartFile file);

    Stream<Path> loadAll();

    Path load(String filename);

    Resource loadAsResource(String filename);

    void deleteAll();

}

及其实现:

package hello.storage;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.stream.Stream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Service;
import org.springframework.util.FileSystemUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

@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 file with relative path outside 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);
        }
    }
}

我想念什么? 预先感谢。

1 个答案:

答案 0 :(得分:0)

首先,我不知道您的StorageProperties是spring组件还是简单的java类! 我将假设它是一个弹簧组件:

@Component
public class StorageProperties {
...
}

在FileSystemStorageService中,将构造函数替换为postConstruct方法:

@Service
public class FileSystemStorageService implements StorageService {

    private Path rootLocation;

    @Autowired
    StorageProperties properties;

    @PostConstruct
    public void FileSystemStorageService() {
        this.rootLocation = Paths.get(properties.getLocation());
    ....