单个资源变量引用
第一步:在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();
}
}
第五步:查看执行结果

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

2322

被折叠的 条评论
为什么被折叠?



