亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

創建 bean 時出錯 - 在服務中注入配置類不起作用

創建 bean 時出錯 - 在服務中注入配置類不起作用

Qyouu 2023-12-21 10:48:03
我嘗試實現一個 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 中設置了該屬性?


查看完整回答
反對 回復 2023-12-21
  • 1 回答
  • 0 關注
  • 251 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號