參考博客: @ConfigurationProperties 註解使用姿勢,這一篇就夠了
一、註解的作用:
@ConfigurationProperties註解的作用就是獲取我們配置的參數值,這些參數一般配置在application.properties或者application.yml中。
二、使@ConfigurationProperties生效的幾種方式
1.使用在配置類上,使用@Configuration或者@Component註解,讓component scan掃描到。
2.在java配置類中,使用@Bean返回被@ConfigurationProperties標註的配置類。
3.使用@EnableConfigurationProperties
三、代碼:
application.yml配置:
server: port: 8080 servlet: context-path: /helloSpring website: name: bianchengbang url: www.biancheng.net pets: - dog - cat - pig #將這些person屬性全部映射到配置文件PersonProperties.java上 person: lastName: 張三 age: 18 boss: true pets: - dog - cat - pig student: name: 王子奇 age: 12 teacher: lesson: mathematics gender: female
PersonProperties.java:
package com.cy.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import java.util.List; /** * 將配置文件中配置的每一個屬性的值,映射到這個組件中 */ @Configuration @ConfigurationProperties(prefix = "person") public class PersonProperties { private String lastName; private Integer age = 10; //會被application.yml中配置的屬性覆蓋 private Boolean boss = Boolean.FALSE; //會被application.yml中配置的屬性覆蓋 private List<String> pets; public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Boolean getBoss() { return boss; } public void setBoss(Boolean boss) { this.boss = boss; } public List<String> getPets() { return pets; } public void setPets(List<String> pets) { this.pets = pets; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } }
StudentProperties.java:
package com.cy.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; @Data @ConfigurationProperties(prefix = "student") public class StudentProperties { private String name; private Integer age; }
TeacherProperties.java:
package com.cy.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; @Data @ConfigurationProperties(prefix = "teacher") public class TeacherProperties { private String lesson; private String gender; }
PropertiesConfig.java:
package com.cy.config; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 1.使用 @EnableConfigurationProperties 註解讓我們的類(TeacherProperties)被 Spring Boot 所知道, * 激活@ConfigurationProperties */ @Configuration @EnableConfigurationProperties(TeacherProperties.class) public class PropertiesConfig { /** * 通過 Spring 的 Java Configuration 特性實現 * 將StudentProperties配置屬性@ConfigurationProperties激活 */ @Bean public StudentProperties studentProperties(){ return new StudentProperties(); } }
測試代碼,HelloController.java:
package com.cy.controller; import com.cy.config.PersonProperties; import com.cy.config.StudentProperties; import com.cy.config.TeacherProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap; import java.util.Map; @Controller public class HelloController { @Autowired private PersonProperties personProperties; @Autowired private StudentProperties studentProperties; @Autowired private TeacherProperties teacherProperties; /** * value: 只支持基本數據類型的封裝,例如字符串、布爾值、整數等類型。 */ @Value("${website.name}") private String webSiteName; @Value("${website.url}") private String webSiteUrl; @ResponseBody @RequestMapping("/hello") public String hello() { return "Hello World!"; } /** * 將personProperties javabean配置屬性打印出來 * @return */ @ResponseBody @RequestMapping("/personProperties") public Map<String, Object> personProperties() { Map<String, Object> map = new HashMap<>(); map.put("lastName", personProperties.getLastName()); map.put("age", personProperties.getAge()); map.put("isBoss", personProperties.getBoss()); map.put("pets", personProperties.getPets()); return map; } @ResponseBody @RequestMapping("/getStudentProps") public Map<String, Object> getStudentProps() { Map<String, Object> map = new HashMap<>(); map.put("student name", studentProperties.getName()); map.put("student age", studentProperties.getAge()); return map; } @ResponseBody @RequestMapping("/getTeacherProps") public Map<String, Object> getTeacherProps() { Map<String, Object> map = new HashMap<>(); map.put("teacher lesson", teacherProperties.getLesson()); map.put("teacher gender", teacherProperties.getGender()); return map; } @ResponseBody @RequestMapping("/getWebSiteProps") public String getWebSiteProps() { return "webSiteName: " + webSiteName + " webSiteUrl: " + webSiteUrl; } }
測試結果:
輸入 http://localhost:8080/helloSpring/personProperties
{"pets":["dog","cat","pig"],"lastName":"張三","isBoss":true,"age":18}
輸入 http://localhost:8080/helloSpring/getStudentProps
{"student age":12,"student name":"王子奇"}
輸入 http://localhost:8080/helloSpring/getTeacherProps
{"teacher lesson":"mathematics","teacher gender":"female"}
輸入 http://localhost:8080/helloSpring/getWebSiteProps
webSiteName: bianchengbang webSiteUrl: www.biancheng.net
四、小記
1.這些註解的使用,代碼中的例子比較簡單。更多詳細使用可見看考博客。
[email protected]的使用例子中也有,一般是在單個屬性的使用中,可以看@Value和@ConfigurationProperties的區別。http://c.biancheng.net/spring_boot/config-bind.html
---