国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

springboot如何讀取application.yml文件

瀏覽:82日期:2023-04-04 15:05:07

現在開發主要使用微服務框架springboot,在springboot中經常遇到讀取application.yml文件的情形。

一、概述

開發過程中經常遇到要讀取application.yml文件中的屬性值,本文總結幾種讀取的方式,供參考。

二、詳述

我這里使用的是springboot-2.1.2.RELEASE版本,這里使用的是application.properties的配置方式,和使用application.yml的方式是一樣的。下面是application.properties文件的內容

cn.com.my.test1=test1cn.com.my.test2=test21、@Value注解

這種方式是spring最早提供的方式,通過@Value注解的方式,該注解用在屬性上,但是要求該屬性所在的類必須要被spring管理。

package com.example.demo.controller;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class TestController { @Value('${cn.com.my.test1}') private String test1; @Value('${cn.com.my.test2}') private String test2; @RequestMapping('/test1/test') @ResponseBody public String getTest(){ return 'hello:'+test1+',test2:'+test2; }}

在標記有@Controller類中使用了帶有@Value注解的test1和test2的屬性,首先標記有@Controller注解便可以使該類被spring管理。其次,使用@Value標記了屬性,則可以獲得application.properties(application.yml)文件中的屬性,這里使用${cn.com.my.test1},屬性的名稱必須是全部的名稱,測試結果如下,

springboot如何讀取application.yml文件

2、@ConfigurationProperties

@ConfigurationProperties注解是springboot提供的,在springboot中大量使用,下面看其用法,

使用@Component注解

這里需要定義一個類,

package com.example.demo.properties;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Component@ConfigurationProperties(prefix = 'cn.com.my')public class ApplicationPro { private String test1; private String test2; private String testName; //必須有set方法 public void setTest1(String test1) { this.test1 = test1; } //必須有set方法 public void setTest2(String test2) { this.test2 = test2; } public String getTest1() { return test1; } public String getTest2() { return test2; }public void setTestName(String testName) { this.testName = testName;}public String getTestName() { return testName;}}

該類上使用了@ConfigurationProperties注解,且配置了prefix屬性,指定了要獲取屬性的前綴,這里的前綴是cn.com.my,在類中定義的屬性名最好和application.properties文件中的一致,不過這種方式可以采用稀疏匹配,把application.properties修改為下面的內容,

cn.com.my.test1=test1cn.com.my.test2=test2cn.com.my.test-name='hello world'

另外,在ApplicationPro類上標記有@Component注解,標記該注解的意思是要把該類交給spring管理,也就是說要讓spring管理此類,其實也可以使用其他注解,如,@Service等

下面看測試類,

package com.example.demo.controller;import com.example.demo.properties.ApplicationPro;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class TestController3 { @Autowired private ApplicationPro ap; @RequestMapping('test3/test') @ResponseBody public String getTest(){ return ap.getTest1()+','+ap.getTest2()+','+ap.getTestName(); }}

看測試結果,

springboot如何讀取application.yml文件

從上面的結果可以看出已經獲得了application.properties文件中的值,并且獲得了test-name的值。具體匹配規則可以自行百度,這里強烈建議配置文件中的屬性和類中的保持一致。

使用@EnableConfigurationProperties注解使用該注解在ApplicationPro類中便不需要使用@Component注解,

package com.example.demo.properties;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;//@Component@ConfigurationProperties(prefix = 'cn.com.my')public class ApplicationPro { private String test1; private String test2; private String testName; //必須有set方法 public void setTest1(String test1) { this.test1 = test1; } //必須有set方法 public void setTest2(String test2) { this.test2 = test2; } public String getTest1() { return test1; } public String getTest2() { return test2; } public void setTestName(String testName) { this.testName = testName; } public String getTestName() { return testName; }}

再看啟動類,在啟動類上標記了@EnableConfigurationProperties({ApplicationPro.class}),也就是使@ConfigurationProperties注解生效,并標記了標有@ConfigurationProperties注解的類Application.class

package com.example.demo;import com.example.demo.properties.ApplicationPro;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.context.properties.EnableConfigurationProperties;@SpringBootApplication@EnableConfigurationProperties({ApplicationPro.class})public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}

下面看測試結果,

springboot如何讀取application.yml文件

3、Environment對象

使用Environment對象,該對象是spring提供的一個對象,且是spring內部創建的對象,

package com.example.demo.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.core.env.Environment;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class TestController2 { @Autowired private Environment environment; @RequestMapping('/test2/test') @ResponseBody public String getTest(){ return 'hello,'+environment.getProperty('cn.com.my.test1')+','+'test2:'+environment.getProperty('cn.com.my.test2'); }}

可以看到,可以直接注入該對象的實例,通過其getProperty方法獲得相應的屬性值。

三、總結

本文總結了,在使用springboot的過程中獲取配置文件中的幾種方式,

@Value

@ConfigurationProperties

Environment對象

有不當之處,歡迎指正,謝謝。

以上就是springboot如何讀取application.yml文件的詳細內容,更多關于springboot 讀取application.yml文件的資料請關注好吧啦網其它相關文章!

標簽: Spring
相關文章:
主站蜘蛛池模板: 中文字幕成人免费视频 | 欧美亚洲国产成人综合在线 | 欧美视频久久久 | 色天天躁夜夜躁天干天干 | 免费一级做a爰片久久毛片 免费一级做a爰片性色毛片 | 日本综合欧美一区二区三区 | 成人国产在线视频在线观看 | 曰本美女高清在线观看免费 | 亚洲国产欧美视频 | 日韩精品福利视频一区二区三区 | 黄色片亚洲 | 精品视频一区二区三区 | 国产精品九九九久久九九 | 亚洲人成高清 | 久久精品中文字幕一区 | 成人18免费网站在线观看 | 日韩美女网站 | 亚洲精品日韩中文字幕久久久 | 国产女人在线观看 | 波多野结衣中文在线播放 | 毛片直接看 | 成人免费午间影院在线观看 | 波多野结衣中文在线播放 | 色综合久久88一加勒比 | 一级a毛片 | 成人毛片免费免费 | 欧美扣逼视频 | 欧美日韩一 | 毛片视频网址 | 一级一黄在线观看视频免费 | 久草在线视频免费 | a三级黄色片 | 免费a级毛片大学生免费观看 | 九草视频| 在线免费视频 | 久久九九亚洲精品 | 亚洲爽妇网 | 欧美—级v免费大片 | 黄色作爱视频 | 成人黄网大全在线观看 | 男人一进一出桶女人视频 |