100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > SpringBoot使用@Value注解读取yaml文件中配置信息

SpringBoot使用@Value注解读取yaml文件中配置信息

时间:2021-04-25 23:49:59

相关推荐

SpringBoot使用@Value注解读取yaml文件中配置信息

背景:为了更好的测试,你就得更好地了解开发,为了更好的了解开发,你就知道开发常用框架,那就来吧,第一个springboot

目的:使用注解读取application.yml配置文件中信息

组网图:不涉及

工具:java version “1.8.0_65” ;Apache Maven 3.6.3;IDEA版本 .3 (准备步骤见本人其它博文)

步骤:

知识点一:使用@Value注解读取application.yml文件,如下

server:port: 8080servlet:context-path: /v1age: 25girl: 那个我喜欢的人

更改HelloConteoller

package com.hua.myfirst;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import java.math.BigDecimal;@RestControllerpublic class HelloConteoller {@Value("${age}")private BigDecimal age;@Value("${girl}")private String girl;@GetMapping("/hello")public String hello() {return "Her age is " + age + girl;}}

启动springboot

打开链接:http://127.0.0.1:8080/v1/hello

知识点二:yaml文件中参数的调用,application.yml文件,如下

server:port: 8080servlet:context-path: /v1age: 25girl: 那个我喜欢的人all: ${girl}${age}岁了

HelloConteoller如下:

package com.hua.myfirst;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import java.math.BigDecimal;@RestControllerpublic class HelloConteoller {@Value("${age}")private BigDecimal age;@Value("${girl}")private String girl;@Value("${all}")private String all;@GetMapping("/hello")public String hello() {return "Her age is " + age + girl + " " +all;}}

重新启动项目

打开链接:http://127.0.0.1:8080/v1/hello

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。