gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:30:14 26 4
gpt4 key购买 nike

我关注了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 而且我的 Controller 在另一个子包中而不是在父包。我的存储类在 nu.pk.cv.storage 中,而我的 Controller 在 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);
}
}
}

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

最佳答案

根据 Baeldung article您还需要将 @Configuration 添加到您的类(class)中:

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

关于java - Spring - 服务中构造函数的错误参数 0 需要找不到类型为 Configuration 的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52180328/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com