SpringBoot自定義配置項過程
Spring Boot自定義配置項
配置文件
在application.properties
文件添加需要的配置
比如:
file.path=D:\\flies\\springboot\\
@ConfigurationProperties 注解
使用注解@ConfigurationProperties
將配置項和實體Bean
關(guān)聯(lián)起來
實現(xiàn)配置項和實體類字段的關(guān)聯(lián),讀取配置文件數(shù)據(jù)
import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Data @Component @ConfigurationProperties(prefix = "file") public class FileConfig { private String path; }
使用
獲取配置信息
FileConfig fileConfig = new FileConfig(); // 文件保存目錄 String filePath = fileConfig.getPath();
@PostMapping("/upload/") @ResponseBody public Response upload(MultipartFile file) { // 驗證是否有文件 if(file == null || file.isEmpty()){ return Response.newFail("Upload failed, please select file",400); } FileConfig fileConfig = new FileConfig(); // 文件保存目錄 String filePath = fileConfig.getPath(); // 驗證文件夾 File folder = new File(filePath); if (!folder.exists()) { folder.mkdirs(); } // 文件名 String fileName = UUID.randomUUID() + file.getOriginalFilename(); filePath = filePath + fileName; File saveFile = new File(filePath); try { file.transferTo(saveFile); return Response.newSuccess("Upload successful"); } catch (IOException e) { e.printStackTrace(); return Response.newFail("Upload failed",50001); } }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
如何修改HttpServletRequest中header中的信息
這篇文章主要介紹了如何修改HttpServletRequest中header中的信息,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02SpringBoot Admin2.0 集成Arthas的實現(xiàn)步驟
這篇文章主要介紹了SpringBoot Admin2.0 集成Arthas的實現(xiàn)步驟,幫助大家更好的理解和學(xué)習(xí)使用SpringBoot框架,感興趣的朋友可以了解下2021-04-04java計算任意位水仙花數(shù)示例(回文數(shù))
這篇文章主要介紹了java計算任意位水仙花數(shù)示例(回文數(shù)),需要的朋友可以參考下2014-05-05利用MyBatis進(jìn)行不同條件的like模糊查詢的方法
這篇文章主要介紹了利用MyBatis進(jìn)行不同條件的like模糊查詢,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08SpringBoot+MyBatis實現(xiàn)登錄案例
前端時間在網(wǎng)上看到有朋友在學(xué)習(xí)springboot項目的搭建過程,今天就抽空給大家分享一個案例幫助大家學(xué)習(xí)SpringBoot+MyBatis實現(xiàn)登錄功能,具體實現(xiàn)代碼跟隨小編一起看看吧2021-06-06SpringBoot?快速實現(xiàn)?api?接口加解密功能
在項目中,為了保證數(shù)據(jù)的安全,我們常常會對傳遞的數(shù)據(jù)進(jìn)行加密,Spring?Boot接口加密,可以對返回值、參數(shù)值通過注解的方式自動加解密,這篇文章主要介紹了SpringBoot?快速實現(xiàn)?api?接口加解密功能,感興趣的朋友一起看看吧2023-10-10