SpringBoot实现yml配置文件为变量赋值
程序员文章站
2022-06-03 23:02:45
目录yml配置文件为变量赋值1. 创建person类和car类2. 为person类创建yml配置文件3.创建启动类在yml文件中配置变量例如:二维码的内容yml配置文件为变量赋值1. 创建perso...
yml配置文件为变量赋值
1. 创建person类和car类
在person类上加注释 @configurationproperties(prefix = "person"),表明这个类的成员变量的值从配置类注入。
注意这里的person类的成员变量需要有get/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类创建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.创建启动类
加上注释@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; } }
测试结果:
在yml文件中配置变量
在开发中很多内容不能写死在代码中
就需要动态的配置
例如:二维码的内容
yml文件里增加变量配置
qrcode: content: http://192.168.1.1:8081
在代码里获取信息的时候
@value("${qrcode.content}") private string content;
这样就可以获取yml文件里配置的内容了
降低了代码的耦合
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。