我嘗試實現一個 REST 服務,如本教程中所示: 教程但我收到了這個錯誤: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服務等級:@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; }}該應用程序也注釋如下:@SpringBootApplication@EnableConfigurationProperties({FileStorageProperties.class})public class TestApp implements InitializingBean {我認為唯一不同的是我使用 application.yml 而不是 application.properties,但我定義的屬性與教程類似,但只是采用 yml 樣式。我不知道為什么FileStorageProperties沒有注入,因此FileStorageService無法創建。@Import(FileStoroageProperties.class)我已經嘗試使用其他一些依賴注入方式(例如字段注入)來注釋應用程序。
1 回答

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