官網給出的配置文件大全:
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") @ResponseBody public 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,png String 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}")