SpringBoot中读取application.yml项目配置文件内容

本文介绍了如何在SpringBoot应用中读取并使用application.yml配置文件的内容,包括单个资源变量引用、配置文件中直接使用配置以及以对象形式赋值的方法。详细步骤包括配置文件的定义、变量引用、对象注入等。

单个资源变量引用

第一步:在springBoot的配置文件application.yml中,定义变量

server:
  port: 8081
  servlet:
    context-path: /springboot
  tomcat:
    uri-encoding: UTF-8

#单个变量引用
singleValue: 1

第二步:引用定义的变量

@Controller
public class HelloSpringBoot {

    @Value("${singleValue}")
    private BigDecimal singleValue;

    @RequestMapping(value = "/hello")
    @ResponseBody
    public String helloWorld(){
       return "单个变量引用singleValue:"+singleValue;
    }
}

第三步:执行结果

配置文件中使用配置

第一步:配置文件中定义变量值

server:
  port: 8081
  servlet:
    context-path: /springboot
  tomcat:
    uri-encoding: UTF-8

#单个变量引用
singleValue: 1

#配置中引用配置
desc: 单个变量singleValue的配置值为${singleValue}

第二步:验证输出结果

@Controller
public class HelloSpringBoot {

    @Value("${singleValue}")
    private BigDecimal singleValue;

    @Value("${desc}")
    private String desc;

    @RequestMapping(value = "/hello")
    @ResponseBody
    public String helloWorld(){
       return "单个变量引用singleValue:"+singleValue+
               "配置中使用配置desc:"+desc;
    }
}

配置文件中以对象形式赋值

第一步:引入maven配置

<!--        引用资源文件配置-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

第二步:定义配置对象,需使用注解@ConfigurationProperties(prefix = "program"),program为资源配置文件中定义的资源对象名

package com.test.springboot;

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

import java.math.BigDecimal;

@Component
@ConfigurationProperties(prefix = "program")
@PropertySource(value = "classpath:application.yml")
public class Resource {

    private BigDecimal math;

    private BigDecimal chinese;

    private BigDecimal english;

    private String desc;

    public BigDecimal getMath() {
        return math;
    }

    public void setMath(BigDecimal math) {
        this.math = math;
    }

    public BigDecimal getChinese() {
        return chinese;
    }

    public void setChinese(BigDecimal chinese) {
        this.chinese = chinese;
    }

    public BigDecimal getEnglish() {
        return english;
    }

    public void setEnglish(BigDecimal english) {
        this.english = english;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    @Override
    public String toString() {
        return "ProgramController{" +
                "math=" + math +
                ", chinese=" + chinese +
                ", english=" + english +
                ", desc='" + desc + '\'' +
                '}';
    }
}

第三步:配置文件中定义变量值(注意事项;节点名应与资源对象注解中的prefix一致,变量名应保持名称一致)

server:
  port: 8081
  servlet:
    context-path: /springboot
  tomcat:
    uri-encoding: UTF-8

#单个变量引用
singleValue: 1

#配置中引用配置
desc: 单个变量singleValue的配置值为${singleValue}

#多个变量【对象】引用
program:
  math: 80
  chinese: 100
  english: 50
  desc: 数学考试分数${program.math},语文考试分数${program.chinese}

第四步:项目中引用,使用注解@Autowired注入

@Controller
public class HelloSpringBoot {

    @Value("${singleValue}")
    private BigDecimal singleValue;

    @Value("${desc}")
    private String desc;

    @Autowired
    private Resource resource;

    @RequestMapping(value = "/hello")
    @ResponseBody
    public String helloWorld(){
        return "资源对象引入program:"+resource.toString();
    }
}

第五步:查看执行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值