springboot学习笔记(五)
?
一丶注值方式
? ? ? ? 1.在application.properties文件中注值
? ? ? ? ? ? ? ?首先我們將application.yml中的學生名字和年齡給注釋掉,來驗證在applic.properties的注值方式。
student: #簡單類型#name: djk#age: 20sex: truebirthday: 2000/07/15#map類型:location: #寫法2:province: sdcity: wfzone: sg#寫法1:{province: sd,city: wf,zone: sg} 行內寫法#數組類型:habbies: [籃球,兵乓球,書法] #行內寫法#- 籃球#- 兵乓球#- 書法#集合類型:skills: [計算機,編程,springboot] #行內寫法#- 計算機#- 編程#- springboot#類 類型Pet:#寫法2{nickname: xiaobai,strain: jiwawa} #寫法1#nickname: xiaobai#strain: jiwawa server.port=8088 #spring.http.encoding.charset= #debug=true student.name = fmd student.age = 18然后我們來運行,可以看到,值已經被注入進去了。
總結:
兩種綁值方式是互補的,application.properties的優先級比application.yml的優先級要高。
? ? ? ? 2.在application.yml文件中注值
? ? ? ? ? ? ? ? 在我的上一篇springboot學習筆記(四)中已經介紹
二丶綁值方式
? ? ? ? @ConfigurationProperties
? ? ? ? ? ? ? ? ?在我的上一篇springboot學習筆記(四)中已經介紹
? ? ? ? @Value
? ? ? ? ? ? ? ? ?該方式只能單個注值!
package com.example.bean;import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.Map;import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component //將javabean放入spring容器內 @ConfigurationProperties(prefix = "student")//spring-boot 提供@ConfigurationProperties注解將配置文件的值映射到類上使用 public class Student {@Value("www")private String name;@Value("555")private int age;private boolean sex;private Date birthday;private Map<String,Object> location;private String[] habbies;private List<String> skills;private Pet pet;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public boolean isSex() {return sex;}public void setSex(boolean sex) {this.sex = sex;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public Map<String, Object> getLocation() {return location;}public void setLocation(Map<String, Object> location) {this.location = location;}public String[] getHabbies() {return habbies;}public void setHabbies(String[] habbies) {this.habbies = habbies;}public List<String> getSkills() {return skills;}public void setSkills(List<String> skills) {this.skills = skills;}public Pet getPet() {return pet;}public void setPet(Pet pet) {this.pet = pet;}public Student(String name, int age, boolean sex, Date birthday, Map<String, Object> location, String[] habbies,List<String> skills, Pet pet) {super();this.name = name;this.age = age;this.sex = sex;this.birthday = birthday;this.location = location;this.habbies = habbies;this.skills = skills;this.pet = pet;}public Student() {super();}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + ", sex=" + sex + ", birthday=" + birthday + ", habbies="+ Arrays.toString(habbies) + ", skills=" + skills + ", pet=" + pet + "]";}}? ? ? ? ?經運行, 可以看到綁值成功
? ? ? ? 松散語法
? ? ? ? ? ? ? ? 駝峰命名與松散語法可以互補;例如:nickName 等價于 nick-name
? ? ? ? ? ? ? ? 在application.yml文件中修改如下:
? ? ? ?
? ? ? ? ? ? 經運行測試,成功
? ? ? ?
注意:
當我們使用@ConfigurationProperties注解時,會有一個警告。
? ? ? ?我們只需要單擊就可以解決。可以發現,在pom文件中添加了該依賴(用于實現配置操作)
? ? ? ? 兩種綁值方式的異同及聯系
| ? | @ConfigurationProperties | ?@Value |
| 注值方式 | 批量注入 | 單個注入 |
| 松散語法 | 支持 | 不支持 |
| SpringEL | 不支持 | 支持 |
| JSR303數據校驗 | 支持 | 不支持 |
| 注入復雜類型 | 支持 | 不支持 |
聯系:@ConfigurationProperties與?@Value可以互補,@ConfigurationProperties的優先級較高。
SpringEL
示例:
application.yml文件中修改如下:
Student類中修改如下:
JSR303數據校驗
示例:
在Student類中添加一個email屬性
在application.yml文件中寫入一個錯誤的郵箱
運行發現:
?注入復雜類型
簡單類型:8個基本數據類型+String+Date
@PropertySource注解
作用:springboot默認會加載的配置文件是application.properties和application.yml中的數據
但是如果數據不在這兩個文件里面或者不適用這兩個默認配置文件中的數據,則需要該注解進行加載。
但是@PropertySource注解只能加載properties文件,而不能加載yml文件。
示例:
把兩個默認配置文件中的name注釋掉,然后新建conf.properties文件
修改Student類,如下:
綁值成功
?不知不覺,springboot已經學習了這么多的知識,加油+努力!
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的springboot学习笔记(五)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为什么商家数字化离不开交易平台
- 下一篇: B端可视化:图表设计