SpringBoot實現(xiàn)yml配置文件為變量賦值
yml配置文件為變量賦值
1. 創(chuàng)建person類和Car類
在person類上加注釋 @ConfigurationProperties(prefix = "person"),表明這個類的成員變量的值從配置類注入。
注意這里的person類的成員變量需要有g(shù)et/set方法。
import org.springframework.boot.context.properties.ConfigurationProperties; import java.util.Date; import java.util.List; import java.util.Map; @ConfigurationProperties(prefix = "person") public class Person { 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; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public boolean isMarriage() { return isMarriage; } public void setMarriage(boolean marriage) { isMarriage = marriage; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } public List<String> getHobbit() { return hobbit; } public void setHobbit(List<String> hobbit) { this.hobbit = hobbit; } public Map<String, Object> getMaps() { return maps; } public void setMaps(Map<String, Object> maps) { this.maps = maps; } public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } private String name; private Integer age; private double salary; private boolean isMarriage; private Car car; private List<String> hobbit; private Map<String, Object> maps; private Date birthDate; }
public class Car { private String carName; private String carBrand; public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } public String getCarBrand() { return carBrand; } public void setCarBrand(String carBrand) { this.carBrand = carBrand; } }
2. 為person類創(chuàng)建yml配置文件
person: ? name: zhangsan ? age: 18 ? salary: 8888.88 ? car: ? ? carName: 奧迪A6L ? ? carBrand: 奧迪 ? hobbit: ? ? - 籃球 ? ? - rap ? ? - 唱歌 ? ? - 保健 ? maps: ? ? k1: v1 ? ? k2: v2 ? birthDate: 1991/08/21 ? marriage: true
3.創(chuàng)建啟動類
加上注釋@EnableConfigurationProperties(Person.class),啟動的時候提醒Person這個class的成員變量是可以從配置文件注入的。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableConfigurationProperties(Person.class) @RestController public class Tulingspc01SpringbootPropertiesMappingApplication { @Autowired private Person person; public static void main(String[] args) { SpringApplication.run(Tulingspc01SpringbootPropertiesMappingApplication.class, args); } @RequestMapping("/getPersonInfo") public Person getPersonInfo() { return person; } }
測試結(jié)果:
在yml文件中配置變量
在開發(fā)中很多內(nèi)容不能寫死在代碼中
就需要動態(tài)的配置
例如:二維碼的內(nèi)容
yml文件里增加變量配置
QrCode: ? content: http://192.168.1.1:8081
在代碼里獲取信息的時候
@Value("${QrCode.content}") private String content;
這樣就可以獲取yml文件里配置的內(nèi)容了
降低了代碼的耦合
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
springmvc無法訪問/WEB-INF/views下的jsp的解決方法
本篇文章主要介紹了springmvc無法訪問/WEB-INF/views下的jsp的解決方法,非常具有實用價值,需要的朋友可以參考下2017-10-10剖析Spring WebFlux反應(yīng)式編程設(shè)計及工作原理
這篇文章主要為大家介紹了Spring WebFlux反應(yīng)式編程模型工作原理的剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-02-02Spring?@bean和@component注解區(qū)別
本文主要介紹了Spring?@bean和@component注解區(qū)別,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01Java中MyBatis傳入?yún)?shù)parameterType問題
這篇文章主要介紹了Java中MyBatis傳入?yún)?shù)parameterType問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12Java中對象?和?json?互轉(zhuǎn)四種方式?json-lib、Gson、FastJson、Jackson
這篇文章主要介紹了Java中對象?和?json?互轉(zhuǎn)?四種方式?json-lib、Gson、FastJson、Jackson,需要的朋友可以參考下2023-11-11