日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot 注解配置文件自动映射到属性和实体类

發布時間:2024/10/12 javascript 85 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot 注解配置文件自动映射到属性和实体类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

官網給出的配置文件大全:

https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#common-application-properties

?

1.配置文件自動映射到屬性

步驟1:Controller上面配置 @PropertySource({"classpath:xxxx.properties"})

步驟2:屬性上增加注解

@Value("${test.name}") private String name;

承接上文的例子:https://www.cnblogs.com/jwen1994/p/11184566.html

我們通過配置文件設置上傳文件存放的地址,而不是通過硬編碼的方式

注意加粗的部分

@Controller @PropertySource({"classpath:application.properties"}) public class FileController {@Value("${web.file.path}")private static final String filePath = "F:/IdeaProjects/springbootDemo/src/main/resources/static/images/";@RequestMapping(value = "upload")@ResponseBodypublic JsonData upload(@RequestParam("head_img") MultipartFile file, HttpServletRequest request) {//file.isEmpty(); 判斷圖片是否為空//file.getSize(); 圖片大小進行判斷 String name = request.getParameter("name");System.out.println("用戶名:"+name);// 獲取文件名String fileName = file.getOriginalFilename(); System.out.println("上傳的文件名為:" + fileName);// 獲取文件的后綴名,比如圖片的jpeg,pngString suffixName = fileName.substring(fileName.lastIndexOf("."));System.out.println("上傳的后綴名為:" + suffixName);// 文件上傳后的路徑fileName = UUID.randomUUID() + suffixName;System.out.println("轉換后的名稱:"+fileName);File dest = new File(filePath + fileName);try {file.transferTo(dest);return new JsonData(0, fileName);} catch (IllegalStateException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return new JsonData(-1, "fail to save ", null);}}

?

2.實體類配置文件

步驟1:添加 @Component 注解。(為了讓 Spring Boot 掃描成一個 Bean)

步驟2:使用 @PropertySource 注解指定配置文件位置。

步驟3:使用 @ConfigurationProperties 注解,設置相關屬性。

步驟4:必須通過注入 IOC 對象,才能在類中使用獲取的配置文件值。

類文件如下:

import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component;//服務器配置 @Component @PropertySource({"classpath:application.properties"}) @ConfigurationProperties public class ServerSettings {@Value("${test.appname}")private String name;@Value("${test.domain}")private String domain;//省略getter、setter方法 }

@ConfigurationProperties 可以設置前綴,如果設置了前綴,@Value 就可以不寫前綴,修改后的代碼如下所示。

import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component;//服務器配置 @Component @PropertySource({"classpath:application.properties"}) @ConfigurationProperties(prefix="test")//設置前綴 public class ServerSettings {@Value("${appname}")private String name;@Value("${domain}")private String domain;//省略getter、setter方法 }

如果不寫@Value,那么屬性值就得和配置文件里的 key 一樣。

類文件如下:

import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component;//服務器配置 @Component @PropertySource({"classpath:application.properties"}) @ConfigurationProperties public class ServerSettings {private String name;private String domain;//省略getter、setter方法 }?

獲取這個對象信息

@Autowired//必須通過IOC依賴注入,才能獲取這個對象的值 private ServerSettings serverSettings;@GetMapping("/v1/test_properties") public Object testPeroperties(){return serverSettings; }

?

常見問題:

1)配置文件注入失敗,Could not resolve placeholder

解決:根據 Spring Boot 啟動流程,會有自動掃描包沒有掃描到相關注解,,默認 Spring 框架實現會從聲明 @ComponentScan 所在的類的 package 進行掃描,來自動注入,因此啟動類最好放在根路徑下面,或者指定掃描包范圍?Spring Boot 掃描啟動類對應的目錄和子目錄

2)注入 Bean 的方式,屬性名稱和配置文件里面的 key一一對應,就不用加@Value 這個注解,如果不一樣,就要加 @value("${XXX}")

?

轉載于:https://www.cnblogs.com/jwen1994/p/11185514.html

總結

以上是生活随笔為你收集整理的Spring Boot 注解配置文件自动映射到属性和实体类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。