SpringBoot注解把配置文件自動映射到屬性和實體類實戰
簡介:講解使用@value注解配置文件自動映射到屬性和實體類
1、配置文件加載
方式一
1、Controller上面配置
@PropertySource({"classpath:resource.properties"})
2、增加屬性
@Value("${test.name}")
private String name;
文件上傳修改示例:
FileController.java:
1 package net.xdclass.demo.controller; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.util.UUID; 6 7 import javax.servlet.http.HttpServletRequest; 8 9 import net.xdclass.demo.domain.JsonData; 10 11 import org.springframework.beans.factory.annotation.Value; 12 import org.springframework.context.annotation.PropertySource; 13 import org.springframework.stereotype.Controller; 14 import org.springframework.web.bind.annotation.RequestMapping; 15 import org.springframework.web.bind.annotation.RequestParam; 16 import org.springframework.web.bind.annotation.ResponseBody; 17 import org.springframework.web.multipart.MultipartFile; 18 19 /** 20 * 功能描述:文件測試 21 * 22 * <p> 創建時間:Apr 22, 2018 11:22:29 PM </p> 23 */ 24 @Controller 25 @PropertySource({"classpath:application.properties"}) 26 public class FileController { 27 28 @RequestMapping(value = "/api/v1/gopage") 29 public Object index() { 30 return "index"; 31 } 32 33 //private static final String filePath = "L:/Workspaces/Eclipse_Neon/txkt/SpringBootClass/xdclass_springboot/src/main/resources/static/images/";//末尾需要加/,這樣才能寫進來 34 //private static final String filePath = "L:/images/";//末尾需要加/,這樣才能寫進來 35 36 @Value("${web.file.path}") 37 private String filePath; 38 39 @RequestMapping(value = "upload") 40 @ResponseBody 41 public JsonData upload(@RequestParam("head_img") MultipartFile file, HttpServletRequest request) { 42 43 // file.isEmpty(); 判斷圖片是否為空 44 // file.getSize(); 圖片大小進行判斷 45 46 System.out.println("配置注入打印,文件路徑為:" + filePath); 47 48 String name = request.getParameter("name"); 49 System.out.println("用戶名:" + name); 50 51 // 獲取文件名 52 String fileName = file.getOriginalFilename(); 53 System.out.println("上傳的文件名為:" + fileName); 54 55 // 獲取文件的后綴名,比如圖片的jpeg,png 56 String suffixName = fileName.substring(fileName.lastIndexOf(".")); 57 System.out.println("上傳的后綴名為:" + suffixName); 58 59 // 文件上傳后的路徑 60 fileName = UUID.randomUUID() + suffixName; 61 System.out.println("轉換后的名稱:" + fileName); 62 63 File dest = new File(filePath + fileName); 64 65 try { 66 file.transferTo(dest); 67 68 return new JsonData(0, fileName); 69 } catch (IllegalStateException e) { 70 e.printStackTrace(); 71 } catch (IOException e) { 72 e.printStackTrace(); 73 } 74 return new JsonData(-1, "fail to save ", null); 75 } 76 77 }
application.properties:
1 web.images-path=L:/images 2 spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/test/,file:${web.upload-path} 3 #指定某些文件不進行監聽,即不會進行熱加載devtool(重啟后不會監聽下面這個文件) 4 #spring.devtools.restart.exclude=application.properties 5 6 #通過觸發器,去控制什么時候進行熱加載部署新的文件 7 spring.devtools.restart.trigger-file=trigger.txt 8 9 server.port=8083 10 11 #文件上傳路徑配置 12 web.file.path=L:/images 13 14 #測試配置文件注入 15 test.name=www.xdclass.net 16 test.domain=springboot
控制台結果:
配置注入打印,文件路徑為:L:/images
用戶名:123
上傳的文件名為:hongmi6.jpg
上傳的后綴名為:.jpg
轉換后的名稱:ee4b60bd-35b4-4df6-bee6-ed62dd5e4a00.jpg
瀏覽器返回值:
{"code":0,"data":"ee4b60bd-35b4-4df6-bee6-ed62dd5e4a00.jpg","msg":null}
方式二:實體類配置文件
步驟:
1、添加 @Component 注解;
2、使用 @PropertySource 注解指定配置文件位置;
3、使用 @ConfigurationProperties 注解,設置相關屬性;
4、必須 通過注入IOC對象Resource 進來 , 才能在類中使用獲取的配置文件值。
@Autowired
private ServerSettings serverSettings;
例子:
@Configuration
@ConfigurationProperties(prefix="test")
@PropertySource(value="classpath:resource.properties")
public class ServerConstant {
代碼示例:
ServerSettings.java:
1 package net.xdclass.demo.domain; 2 3 import org.springframework.boot.context.properties.ConfigurationProperties; 4 import org.springframework.context.annotation.PropertySource; 5 import org.springframework.stereotype.Component; 6 7 //服務器配置 8 @Component 9 @PropertySource({"classpath:application.properties"}) 10 //@ConfigurationProperties 11 //自動加入前綴,無需寫入test. 12 @ConfigurationProperties(prefix="test") 13 //若不想使用prefix="test",前提是該類中定義的名稱要與application.properties中定義的名稱一致,即一一對應 14 public class ServerSettings { 15 16 //名稱 17 //使用prefix="test"后,無需再使用Value注解 18 //@Value("${name}") 19 private String name; 20 21 //域名地址 22 //@Value("${domain}") 23 private String domain; 24 25 public String getName() { 26 return name; 27 } 28 29 public void setName(String name) { 30 this.name = name; 31 } 32 33 public String getDomain() { 34 return domain; 35 } 36 37 public void setDomain(String domain) { 38 this.domain = domain; 39 } 40 41 42 }
application.properties同上一個示例
GetController.java部分代碼:
1 @Autowired 2 private ServerSettings serverSettings; 3 @GetMapping("/v1/test_properties") 4 public Object testProperties(){ 5 return serverSettings; 6 }
瀏覽器測試:
地址欄輸入:http://localhost:8083/v1/test_properties
結果:{"name":"www.xdclass.net","domain":"springboot"}
常見問題:
1、配置文件注入失敗,Could not resolve placeholder
解決:根據springboot啟動流程,會有自動掃描包沒有掃描到相關注解,
默認Spring框架實現會從聲明@ComponentScan所在的類的package進行掃描,來自動注入,
因此啟動類最好放在根路徑下面,或者指定掃描包范圍
spring-boot掃描啟動類對應的目錄和子目錄
2、注入bean的方式,屬性名稱和配置文件里面的key一一對應,就不用加@Value 這個注解
如果不一樣,就要加@value("${XXX}")
3、SpringBoot注解把配置文件自動映射到屬性和實體類實戰簡介:講解使用@value注解配置文件自動映射到屬性和實體類1、配置文件加載方式一1、Controller上面配置 @PropertySource({"classpath:resource.properties"})2、增加屬性 @Value("${test.name}") private String name;
方式二:實體類配置文件步驟:1、添加 @Component 注解;2、使用 @PropertySource 注解指定配置文件位置;3、使用 @ConfigurationProperties 注解,設置相關屬性;
4、必須 通過注入IOC對象Resource 進來 , 才能在類中使用獲取的配置文件值。@Autowired private ServerSettings serverSettings;
例子: @Configuration@ConfigurationProperties(prefix="test")@PropertySource(value="classpath:resource.properties")public class ServerConstant {
常見問題:1、配置文件注入失敗,Could not resolve placeholder解決:根據springboot啟動流程,會有自動掃描包沒有掃描到相關注解, 默認Spring框架實現會從聲明@ComponentScan所在的類的package進行掃描,來自動注入,因此啟動類最好放在根路徑下面,或者指定掃描包范圍spring-boot掃描啟動類對應的目錄和子目錄2、注入bean的方式,屬性名稱和配置文件里面的key一一對應,就用加@Value 這個注解如果不一樣,就要加@value("${XXX}")