javascript
Spring Boot 注解配置文件自动映射到属性和实体类
官網(wǎng)給出的配置文件大全:
https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#common-application-properties
?
1.配置文件自動(dòng)映射到屬性
步驟1:Controller上面配置 @PropertySource({"classpath:xxxx.properties"})
步驟2:屬性上增加注解
承接上文的例子:https://www.cnblogs.com/jwen1994/p/11184566.html
我們通過配置文件設(shè)置上傳文件存放的地址,而不是通過硬編碼的方式
注意加粗的部分
@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(); 圖片大小進(jìn)行判斷 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("轉(zhuǎn)換后的名稱:"+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.實(shí)體類配置文件
步驟1:添加 @Component 注解。(為了讓 Spring Boot 掃描成一個(gè) Bean)
步驟2:使用 @PropertySource 注解指定配置文件位置。
步驟3:使用 @ConfigurationProperties 注解,設(shè)置相關(guān)屬性。
步驟4:必須通過注入 IOC 對(duì)象,才能在類中使用獲取的配置文件值。
類文件如下:
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;//服務(wù)器配置 @Component @PropertySource({"classpath:application.properties"}) @ConfigurationProperties public class ServerSettings {@Value("${test.appname}")private String name;@Value("${test.domain}")private String domain;//省略getter、setter方法 }@ConfigurationProperties 可以設(shè)置前綴,如果設(shè)置了前綴,@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;//服務(wù)器配置 @Component @PropertySource({"classpath:application.properties"}) @ConfigurationProperties(prefix="test")//設(shè)置前綴 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;//服務(wù)器配置 @Component @PropertySource({"classpath:application.properties"}) @ConfigurationProperties public class ServerSettings {private String name;private String domain;//省略getter、setter方法 }?獲取這個(gè)對(duì)象信息
@Autowired//必須通過IOC依賴注入,才能獲取這個(gè)對(duì)象的值 private ServerSettings serverSettings;@GetMapping("/v1/test_properties") public Object testPeroperties(){return serverSettings; }?
常見問題:
1)配置文件注入失敗,Could not resolve placeholder
解決:根據(jù) Spring Boot 啟動(dòng)流程,會(huì)有自動(dòng)掃描包沒有掃描到相關(guān)注解,,默認(rèn) Spring 框架實(shí)現(xiàn)會(huì)從聲明 @ComponentScan 所在的類的 package 進(jìn)行掃描,來自動(dòng)注入,因此啟動(dòng)類最好放在根路徑下面,或者指定掃描包范圍?Spring Boot 掃描啟動(dòng)類對(duì)應(yīng)的目錄和子目錄
2)注入 Bean 的方式,屬性名稱和配置文件里面的 key一一對(duì)應(yīng),就不用加@Value 這個(gè)注解,如果不一樣,就要加 @value("${XXX}")
?
轉(zhuǎn)載于:https://www.cnblogs.com/jwen1994/p/11185514.html
總結(jié)
以上是生活随笔為你收集整理的Spring Boot 注解配置文件自动映射到属性和实体类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大白菜u盘怎么启动asus 大白菜U盘如
- 下一篇: 优盘是只读怎么设置 优盘如何设置为只读模