springboot學習筆記(四)
阿新 • • 發佈:2018-12-13
springboot配置檔案及yml的使用
1.配置檔案
作用:springboot自動配置是基於約定的,可以使用配置檔案對預設的配置或約定進行修改
預設的全域性配置檔案:
①application.properties :
寫法:k=v
示例:
server.port = 8880
②application.yml :yml不是一個標記文件
寫法:k:空格v
示例:
server:
port: 8880
path: a\b\c
yml裡面預設可以不寫引號,“”(雙引號)會將其中的轉義符轉義,其他不轉義
xml是一個標記文件:
<server>
<port>8080</port>
<path>a\b\c</path>
</server>
2.yml的使用
①建立一個student類(Student.class)
package com.example.bean; import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.Map; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component //將javabean放入spring容器內 @ConfigurationProperties(prefix = "student")//spring-boot 提供@ConfigurationProperties註解將配置檔案的值對映到類上使用 public class Student { private String name; 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(); } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", sex=" + sex + ", birthday=" + birthday + ", habbies=" + Arrays.toString(habbies) + ", skills=" + skills + ", pet=" + pet + "]"; } }
②建立Pet類(Pet.class)
package com.example.bean; public class Pet { private String nickname; private String strain; public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public String getStrain() { return strain; } public void setStrain(String strain) { this.strain = strain; } @Override public String toString() { return "Pet [nickname=" + nickname + ", strain=" + strain + "]"; } public Pet(String nickname, String strain) { super(); this.nickname = nickname; this.strain = strain; } public Pet() { super(); } }
③編寫yml檔案(application.yml)
student:
#簡單型別
name: djk
age: 20
sex: true
birthday: 2000/07/15
#map型別:
location:
#寫法2:
province: sd
city: wf
zone: sg
#寫法1:{province: sd,city: wf,zone: sg} 行內寫法
#陣列型別:
habbies:
[籃球,兵乓球,書法] #行內寫法
#- 籃球
#- 兵乓球
#- 書法
#集合型別:
skills:
[計算機,程式設計,springboot] #行內寫法
#- 計算機
#- 程式設計
#- springboot
#類 型別
Pet:
#寫法2
{nickname: xiaobai,strain: jiwawa}
#寫法1
#nickname: xiaobai
#strain: jiwawa
④測試
package com.example.SpringbootDemo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.example.bean.Student;
@RunWith(SpringRunner.class)
@SpringBootTest
@EnableConfigurationProperties(Student.class)//3.通過@Autowired標籤即可訪問到該物件,不過在使用之前必須在使用類上面增加註解@EnableConfigurationProperties
public class SpringbootDemoApplicationTests {
@Autowired
Student student;
@Test
public void contextLoads() {
System.out.println(student);
}
}
註解
1. @EnableConfigurationProperties(Student.class)
通過@Autowired標籤即可訪問到該物件,不過在使用之前必須在使用類上面增加註解@EnableConfigurationProperties
2. @ConfigurationProperties(prefix = "student")
spring-boot 提供@ConfigurationProperties註解將配置檔案的值對映到類上使用