我嘗試實(shí)現(xiàn)一個(gè) REST 服務(wù),如本教程中所示: 教程但我收到了這個(gè)錯(cuò)誤: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ù)等級(jí):@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); } }}配置類(lèi)別:@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,但我定義的屬性與教程類(lèi)似,但只是采用 yml 樣式。我不知道為什么FileStorageProperties沒(méi)有注入,因此FileStorageService無(wú)法創(chuàng)建。@Import(FileStoroageProperties.class)我已經(jīng)嘗試使用其他一些依賴(lài)注入方式(例如字段注入)來(lái)注釋?xiě)?yīng)用程序。
1 回答

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