springboot配置檔案繫結實現解析
這篇文章主要介紹了springboot配置檔案繫結實現解析,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
先建立一個peron類,然後需要註解configurationProperties(prefix ="person")<br data-filtered="filtered">然後需要加一個@component<br data-filtered="filtered">因為只有在springboot的容器才能提供容器提供的@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會提示需要在pom.xml中匯入元件處理器。
<!-- 配置檔案的處理器,配置檔案進行繫結就會有提示--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> </dependencies>
然後建立配置檔案,有兩種方式,一個時yml檔案,另一個時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中自動轉為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的單元測試
@RunWith(SpringRunner.class) @SpringBootTest class Helloworld01QuickApplicationTests { @Autowired Person person; @Test void contextLoads() { System.out.println(person); } }
執行,會看到得到配置檔案中的資料
在獲取配置檔案中注入值得時候,可以使用@value,也可以使用@configurationProperties;
如果只是在邏輯中獲取一下配置檔案中得值,那麼就使用@value
在配置檔案注入值得時候也可以校驗
在類加入註解@validate
配置檔案注入資料校驗
@validate public class person{ @Email private String last-name; .... }
@PropertySource("classpath:person.properties") :載入指定的配置檔案
@ImportResource(“classpath:beans.xml”):匯入spring配置檔案,讓配置檔案生效;
springboot推薦給容器增加元件
1.配置類--》spring配置檔案
2.使用@bean給容器中增加元件;
配置檔案佔位符
1.隨機數
${random.value}、${random.int}、${random.long} ${random.int(10)}、${random.int[1024,65536]}
2.配置檔案中找不到屬性時的預設值。
${app.name:金毛}來指定找不到屬性時的預設值。
profile
1.多個profile檔案
Profile是Spring對不同環境提供不同配置功能的支援,可以通過啟用、指定引數等方式快速切換環境
一般我們在開發的時候有測試環境,開發環境等。
我們在編寫多個配置檔案的時候,檔名字是application-(profile).properties/yml(這二種格式的都行)。
預設使用application.properties.
2.yml支援多文件塊方式
application.yml
#三個橫線屬於一個文件塊 #啟用哪個環境 spring: profiles: active: test #測試環境 --- server: port: 8081 spring: profiles: test #開發環境 --- server: port: 8082 spring: profiles: dev
3.啟用指定profile
在配置檔案中指定spring.profiles.active =dev
springboot配置檔案載入位置
這些配置都會載入,然後進行互補配置。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。