【问题标题】:Spring - Error Parameter 0 of constructor in Service required a bean of type Configuration that could not be foundSpring - Service 中构造函数的错误参数 0 需要无法找到 Configuration 类型的 bean
【发布时间】:2019-02-10 07:34:41
【问题描述】:

我关注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";
    }
}

【问题讨论】:

    标签: java spring spring-boot spring-config


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

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

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

      参考:Spring blog

      【讨论】:

      • 这对我有帮助。我没有意识到我跳过将@Component 添加到我的处理程序...
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-23
        • 2020-08-26
        • 2021-12-10
        • 2019-05-30
        • 2019-10-20
        • 2023-02-05
        • 1970-01-01
        • 2018-12-06
        相关资源
        最近更新 更多