SpringBoot是為了簡化Spring應用的創建、運行、調試、部署等一系列問題而誕生的產物,自動裝配的特性讓我們可以更好的關注業務本身而不是外部的XML配置,
我們只需遵循規范,引入相關的依賴就可以輕易的搭建出一個 WEB 工程
SpringBoot
雖然干掉了 XML 但未做到零配置,它體現出了一種約定優於配置,也稱作按約定編程,是一種軟件設計范式,旨在減少軟件開發人員需做決定的數量,
獲得簡單的好處,而又不失靈活性。一般情況下默認的配置足夠滿足日常開發所需,但在特殊的情況下,我們往往需要用到自定義屬性配置、自定義文件配置、多環境配置、
外部命令引導等一系列功能。不用擔心,這些SpringBoot
都替我們考慮好了,我們只需要遵循它的規則配置即可.
一.准備前提
為了讓SpringBoot
更好的生成數據,我們需要添加如下依賴(該依賴可以不添加,但是在 IDEA 和 STS 中不會有屬性提示,沒有提示的配置就跟你用記事本寫代碼一樣苦逼,出個問題弄哭你去),該依賴只會在編譯時調用,所以不用擔心會對生產造成影響…
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-configuration-processor</artifactId> 4 <optional>true</optional> 5 </dependency>
二.使用系統的application.properties屬性文件進行相關配置和值的注入
在application.properties
寫入如下配置內容
1 stu1.age=25 2 stu1.name=Luis
其次定義StudentProperties.java
文件,用來映射我們在application.properties
中的內容,這樣一來我們就可以通過操作對象的方式來獲得配置文件的內容了
1.創建StudentProperties.java
1 package cn.kgc.properties; 2 import org.springframework.boot.context.properties.ConfigurationProperties; 3 import org.springframework.stereotype.Component; 4 /** 5 * 注解Component: 標注傳遞數據的實體類 6 * 注解ConfigurationProperties:標注屬性文件的, 7 * prefix前綴則是屬性文件中屬性的前綴, 8 * 因為一個屬性文件中可能配置很多,可以通過前綴區分 9 */ 10 @Component 11 @ConfigurationProperties(prefix = "stu") 12 public class StudentProperties { 13 private int age; 14 private String name; 15 public int getAge() { 16 return age; 17 } 18 public void setAge(int age) { 19 this.age = age; 20 } 21 public String getName() { 22 return name; 23 } 24 public void setName(String name) { 25 this.name = name; 26 } 27 @Override 28 public String toString() { 29 return "StudentProperties{" + 30 "age=" + age + 31 ", name='" + name + '\'' + 32 '}'; 33 } 34 }
2.定義controller類來給StudentProperties類注入值
定義我們的PropertiesController
用來注入StudentProperties
測試我們編寫的代碼,值得注意的是Spring4.x
以后,推薦使用構造函數的形式注入屬性…
1 package cn.kgc.controller; 2 3 import cn.kgc.properties.StudentProperties; 4 import org.slf4j.Logger; 5 import org.slf4j.LoggerFactory; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.web.bind.annotation.GetMapping; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RestController; 10 /** 11 * Created by Administrator on 2018/10/16. 12 */ 13 @RequestMapping("/properties") 14 @RestController 15 public class PropertiesController { 16 //對本類做日志記錄 17 private static final Logger log = LoggerFactory.getLogger(PropertiesController.class); 18 //創建接受屬性文件的值的實體類 19 private final StudentProperties studentProperties; 20 @Autowired 21 public PropertiesController(StudentProperties studentProperties) { 22 this.studentProperties = studentProperties; 23 } 24 @GetMapping("/stuProperties") 25 public StudentProperties studentProperties() { 26 log.info("================================================================================================="); 27 log.info(studentProperties.toString()); 28 log.info("================================================================================================="); 29 return studentProperties; 30 } 31 }
3.運行開啟springBoot,在瀏覽器輸入:http://localhost:9090/springboot1/properties/stuProperties ,可以在控制台和瀏覽器看到我們的數據
三、使用自定義的屬性配置文件,進行值的相關注入
1. 定義一個名為teacher.properties
的資源文件,自定義配置文件的命名不強制application
開頭
2.定義實體類用來接受springboot將將屬性文件注入值
其次定義TeacherProperties.java
文件,用來映射我們在teacher.properties
中的內容。
1 package cn.kgc.properties; 2 import org.springframework.boot.context.properties.ConfigurationProperties; 3 import org.springframework.context.annotation.PropertySource; 4 import org.springframework.stereotype.Component; 5 /** 6 * Created by Administrator on 2018/10/16. 7 */ 8 @Component 9 @PropertySource("classpath:teacher.properties") 10 @ConfigurationProperties(prefix = "teacher") 11 public class TeacherProperties { 12 private int tid; 13 private String tname; 14 private String qq; 15 private String phone; 16 17 public TeacherProperties() { 18 } 19 20 public TeacherProperties(int tid, String tname, String qq, String phone) { 21 this.tid = tid; 22 this.tname = tname; 23 this.qq = qq; 24 this.phone = phone; 25 } 26 27 public int getTid() { 28 return tid; 29 } 30 31 public void setTid(int tid) { 32 this.tid = tid; 33 } 34 35 public String getTname() { 36 return tname; 37 } 38 39 public void setTname(String tname) { 40 this.tname = tname; 41 } 42 43 public String getQq() { 44 return qq; 45 } 46 47 public void setQq(String qq) { 48 this.qq = qq; 49 } 50 51 public String getPhone() { 52 return phone; 53 } 54 55 public void setPhone(String phone) { 56 this.phone = phone; 57 } 58 59 @Override 60 public String toString() { 61 return "TeacherProperties{" + 62 "tid=" + tid + 63 ", tname='" + tname + '\'' + 64 ", qq='" + qq + '\'' + 65 ", phone='" + phone + '\'' + 66 '}'; 67 } 68 }
3.在PropertiesController
用來注入TeacherProperties
測試我們編寫的代碼
1 package cn.kgc.controller; 2 3 import cn.kgc.properties.StudentProperties; 4 import cn.kgc.properties.TeacherProperties; 5 import org.slf4j.Logger; 6 import org.slf4j.LoggerFactory; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.web.bind.annotation.GetMapping; 9 import org.springframework.web.bind.annotation.RequestMapping; 10 import org.springframework.web.bind.annotation.RestController; 11 /** 12 * Created by Administrator on 2018/10/16. 13 */ 14 @RequestMapping("/properties") 15 @RestController 16 public class PropertiesController { 17 18 //對本類做日志記錄 19 private static final Logger log = LoggerFactory.getLogger(PropertiesController.class); 20 //創建接受屬性文件的值的實體類 21 private final StudentProperties studentProperties; 22 //創建接受屬性文件的值的實體類 23 private final TeacherProperties teacherProperties; 24 25 26 @Autowired 27 public PropertiesController(TeacherProperties teacherProperties, StudentProperties studentProperties) { 28 this.studentProperties = studentProperties; 29 this.teacherProperties = teacherProperties; 30 } 31 @GetMapping("/tecProperties") 32 public TeacherProperties teacherProperties() { 33 log.info("================================================================================================="); 34 log.info(teacherProperties.toString()); 35 log.info("================================================================================================="); 36 return teacherProperties; 37 } 38 //--- 39 40 @GetMapping("/stuProperties") 41 public StudentProperties studentProperties() { 42 log.info("================================================================================================="); 43 log.info(studentProperties.toString()); 44 log.info("================================================================================================="); 45 return studentProperties; 46 } 47 // 48 }
4.先啟動springBoot
5.在地址欄輸入地址:http://localhost:9090/springboot1/properties/tecProperties查看結果