我嘗試實(shí)現(xiàn)一個 REST 服務(wù),如本教程中所示: 教程但我收到了這個錯誤:Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fileStorageService' defined in file: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.airgnb.service.FileStorageService]: Constructor threw exception; nested exception is java.lang.NullPointerExceptionCaused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.airgnb.service.FileStorageService]: Constructor threw exception; nested exception is java.lang.NullPointerExceptionCaused by: java.lang.NullPointerException服務(wù)等級:@Servicepublic class FileStorageService { private final Path fileStorageLocation; @Autowired public FileStorageService(FileStorageProperties fileStorageProperties) { this.fileStorageLocation = Paths.get(fileStorageProperties.getUploadDir()) .toAbsolutePath().normalize(); try { Files.createDirectories(this.fileStorageLocation); } catch (Exception ex) { throw new FileStorageServiceException("Could not create the directory where the uploaded files will be stored.", ex); } }}配置類別:@Configuration@ConfigurationProperties(prefix = "file")public class FileStorageProperties { private String uploadDir; public String getUploadDir() { return uploadDir; } public void setUploadDir(String uploadDir) { this.uploadDir = uploadDir; }}該應(yīng)用程序也注釋如下:@SpringBootApplication@EnableConfigurationProperties({FileStorageProperties.class})public class TestApp implements InitializingBean {我認(rèn)為唯一不同的是我使用 application.yml 而不是 application.properties,但我定義的屬性與教程類似,但只是采用 yml 樣式。我不知道為什么FileStorageProperties沒有注入,因此FileStorageService無法創(chuàng)建。@Import(FileStoroageProperties.class)我已經(jīng)嘗試使用其他一些依賴注入方式(例如字段注入)來注釋應(yīng)用程序。
1 回答

蕭十郎
TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個贊
我認(rèn)為沒有FileStorageProperties
被注射。該錯誤表明未FileStorageProperties
找到該類型的 Bean。你只有一個NullPointerException
構(gòu)造函數(shù)內(nèi)部。
我想fileStorageProperties.getUploadDir()
回來了null
。
file.uploadDir
您是否在 application.yml 或 application.properties 中設(shè)置了該屬性?
添加回答
舉報
0/150
提交
取消