先創建一個peron類,然后需要注解configurationProperties(prefix ="person")
然后需要加一個@component
因為只有在springboot的容器才能提供容器提供的@configurationProperties
@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:金毛}來指定找不到屬性時的默認值。
https://blog.csdn.net/baidu_36216018/article/details/79597923
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配置文件加載位置

這些配置都會加載,然后進行互補配置。


