iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot实现yml配置文件为变量赋值
  • 421
分享到

SpringBoot实现yml配置文件为变量赋值

2024-04-02 19:04:59 421人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录yml配置文件为变量赋值1. 创建person类和Car类2. 为person类创建yml配置文件3.创建启动类在yml文件中配置变量例如:二维码的内容yml配置文件为变量赋值

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文件里配置的内容了

降低了代码的耦合

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: SpringBoot实现yml配置文件为变量赋值

本文链接: https://www.lsjlt.com/news/139429.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作