SpringBoot配置文件值獲取


1、這里有不同類型屬性的賦值,包括普通屬性,對象,list,map

  首先創建一個Person類包含一個Dog類,所以再創建一個Dog類

package com.athome.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* 1、@ConfigurationProperties(prefix = "person") 說明這個類的屬性要從配置文件里取值,並且名字是person,默認從全局配置文件里取值
* 可以使用@PropertySource( value ="classpath:person.properties" )指定配置文件
* 2、@Component:只有組件容器才能使用@ConfigurationProperties的功能
*/
@Component
@ConfigurationProperties(prefix = "person")
public class Person {

private String lastName;
private Integer age;
private Date birth;
private Boolean boss;
private Dog dog;
private List<String> lists;
private Map<String,Object> maps;

@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", birth=" + birth +
", boss=" + boss +
", dog=" + dog +
", lists=" + lists +
", maps=" + maps +
'}';
}

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 Date getBirth() {
return birth;
}

public void setBirth(Date birth) {
this.birth = birth;
}

public Boolean getBoss() {
return boss;
}

public void setBoss(Boolean boss) {
this.boss = boss;
}

public Dog getDog() {
return dog;
}

public void setDog(Dog dog) {
this.dog = dog;
}

public List<String> getLists() {
return lists;
}

public void setLists(List<String> lists) {
this.lists = lists;
}

public Map<String, Object> getMaps() {
return maps;
}

public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
}

 

package com.athome.bean;

public class Dog {
private String name;
private Integer age;

@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}
}
2、配置pom文件導入配置文件處理器,這樣當在配置文件內配置屬性的時候會有提示,只是注意,導入成功后需要重啟一下項目
<!--配置文件處理器,綁定數據時有提示信息-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
3、包括propertiest,yaml,兩種方式
  (1)properties文件按配置
#person配置
person.last-name=練習
person.age=24
person.birth=1995/06/10
person.boss=false
person.dog.name=大黑
person.dog.age=8
person.lists=list1,list2,list3
person.maps.key1=v1
person.maps.key2=v2

 (1)yaml文件按配置
person:
last-name: 練習2
age: 18
birth: 1995/06/10
boss: false
dog:
name: 大黑2
age: 8
lists:
- content1
- content2
maps:
key1: v1
key2: v2
3、創建一個測試類可以測試一下,就成功了

 

 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM