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

隐身守侯 提交于 2020-05-30 02:17:12

问题


I followed tutorial for uploading files but I end up with the following error:

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

The only difference that I know that I have done is that I use @RestController instead of only @Controller And that I have my controller in another subpackage and not in the parent package. My storage classes are in nu.pk.cv.storage while my controller is in nu.pk.cv.cv.

StorageProperties

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

My controller

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

回答1:


According to Baeldung article you need to add also @Configuration to your class:

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



回答2:


add annotation @Configuration or @Component at StorageProperties class.

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

or

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

if you receive an Error like "a bean not found or a bean of type configuration", then you have to check your @Component, @Service or @Repository annotations in the related classes.

ref: Spring blog



来源:https://stackoverflow.com/questions/52180328/spring-error-parameter-0-of-constructor-in-service-required-a-bean-of-type-con

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!