springboot配置文件綁定實(shí)現(xiàn)解析
這篇文章主要介紹了springboot配置文件綁定實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
先創(chuàng)建一個(gè)peron類,然后需要注解configurationProperties(prefix ="person")<br data-filtered="filtered">然后需要加一個(gè)@component<br data-filtered="filtered">因?yàn)橹挥性趕pringboot的容器才能提供容器提供的@configurationProperties<br data-filtered="filtered">@Component @ConfigurationProperties(prefix = "person") public class Person { private String lastName; private Integer age; private boolean boss; private Date birth; private Map<String,Object> maps; private List<Object> lists; private Dog dog; public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public boolean isBoss() { return boss; } public void setBoss(boolean boss) { this.boss = boss; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } public Map<String, Object> getMaps() { return maps; } public void setMaps(Map<String, Object> maps) { this.maps = maps; } public List<Object> getLists() { return lists; } public void setLists(List<Object> lists) { this.lists = lists; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } @Override public String toString() { return "Person [lastName=" + lastName + ", age=" + age + ", boss=" + boss + ", birth=" + birth + ", maps=" + maps + ", lists=" + lists + ", dog=" + dog + "]"; } }
dog類
public class Dog { private String Name; private Integer age; public String getName() { return Name; } public void setName(String name) { Name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Dog [Name=" + Name + ", age=" + age + "]"; } }
寫完后,ide會(huì)提示需要在pom.xml中導(dǎo)入組件處理器。
<!-- 配置文件的處理器 ,配置文件進(jìn)行綁定就會(huì)有提示--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> </dependencies>
然后創(chuàng)建配置文件,有兩種方式,一個(gè)時(shí)yml文件,另一個(gè)時(shí)properties
1,application.yml
person: last-name: zhangsan age: 24 boss: false birth: 2017/12/5 maps: {k1: v1,k2: v2} lists: [lisi, zhangsan] dog: Name: xiaohei age: 4
2.application.properties
中文字,在eclipse中自動(dòng)轉(zhuǎn)為unicode碼
person.age=24 person.last-name=\u5F20\u4E09 person.birth=2000/1/1 person.boss=false person.maps.k1=value1 person.maps.k2=12 person.dog.name=\u5C0F\u9ED1 person.dog.age=2
在test中使用spring boot的單元測(cè)試
@RunWith(SpringRunner.class) @SpringBootTest class Helloworld01QuickApplicationTests { @Autowired Person person; @Test void contextLoads() { System.out.println(person); } }
運(yùn)行,會(huì)看到得到配置文件中的數(shù)據(jù)
在獲取配置文件中注入值得時(shí)候,可以使用@value,也可以使用@configurationProperties;
如果只是在邏輯中獲取一下配置文件中得值,那么就使用@value
在配置文件注入值得時(shí)候也可以校驗(yàn)
在類加入注解@validate
配置文件注入數(shù)據(jù)校驗(yàn)
@validate public class person{ @Email private String last-name; .... }
@PropertySource("classpath:person.properties") :加載指定的配置文件
@ImportResource(“classpath:beans.xml”):導(dǎo)入spring配置文件,讓配置文件生效;
springboot推薦給容器增加組件
1.配置類--》spring配置文件
2.使用@bean給容器中增加組件;
配置文件占位符
1.隨機(jī)數(shù)
${random.value}、${random.int}、${random.long} ${random.int(10)}、${random.int[1024,65536]}
2.配置文件中找不到屬性時(shí)的默認(rèn)值。
${app.name:金毛}來指定找不到屬性時(shí)的默認(rèn)值。
profile
1.多個(gè)profile文件
Profile是Spring對(duì)不同環(huán)境提供不同配置功能的支持,可以通過激活、指定參數(shù)等方式快速切換環(huán)境
一般我們?cè)陂_發(fā)的時(shí)候有測(cè)試環(huán)境,開發(fā)環(huán)境等。
我們?cè)诰帉懚鄠€(gè)配置文件的時(shí)候,文件名字是application-(profile).properties/yml(這二種格式的都行)。
默認(rèn)使用application.properties.
2.yml支持多文檔塊方式
application.yml
#三個(gè)橫線屬于一個(gè)文檔塊 #激活哪個(gè)環(huán)境 spring: profiles: active: test #測(cè)試環(huán)境 --- server: port: 8081 spring: profiles: test #開發(fā)環(huán)境 --- server: port: 8082 spring: profiles: dev
3.激活指定profile
在配置文件中指定spring.profiles.active =dev
springboot配置文件加載位置
這些配置都會(huì)加載,然后進(jìn)行互補(bǔ)配置。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis詳解如何實(shí)現(xiàn)Dao層接口
MyBatis允許只聲明一個(gè)dao接口,而無需寫dao實(shí)現(xiàn)類的方式實(shí)現(xiàn)數(shù)據(jù)庫操作。前提是必須保證Mapper文件中的<mapper>標(biāo)簽的namespace屬性值必須要和dao接口的類路徑一致,MyBatis容器會(huì)自動(dòng)通過動(dòng)態(tài)代理生成接口的實(shí)現(xiàn)類2022-04-04Java實(shí)現(xiàn)一個(gè)簡單的定時(shí)器代碼解析
這篇文章主要介紹了Java實(shí)現(xiàn)一個(gè)簡單的定時(shí)器代碼解析,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12springboot統(tǒng)一異常處理(返回json)并格式化異常
這篇文章主要介紹了springboot統(tǒng)一異常處理(返回json)并格式化異常,對(duì)spring boot的默認(rèn)異常處理方式進(jìn)行修改,要統(tǒng)一返回?cái)?shù)據(jù)格式,優(yōu)雅的數(shù)據(jù)交互,優(yōu)雅的開發(fā)應(yīng)用,需要的朋友可以參考下2023-07-07mybatis-plus讀取JSON類型的方法實(shí)現(xiàn)
這篇文章主要介紹了mybatis-plus讀取JSON類型的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09springboot項(xiàng)目配置swagger2示例詳解
Swagger是一款RESTful接口的文檔在線自動(dòng)生成、功能測(cè)試功能框架。本文重點(diǎn)給大家介紹springboot項(xiàng)目配置swagger2示例代碼詳解,需要的朋友參考下吧2021-09-09