詳解Spring-boot中讀取config配置文件的兩種方式
了解過(guò)spring-Boot這個(gè)技術(shù)的,應(yīng)該知道Spring-Boot的核心配置文件application.properties,當(dāng)然也可以通過(guò)注解自定義配置文件的信息。
Spring-Boot讀取配置文件的方式:
一.讀取核心配置文件信息application.properties的內(nèi)容
核心配置文件是指在resources根目錄下的application.properties或application.yml配置文件,讀取這兩個(gè)配置文件的方法有兩種,都比較簡(jiǎn)單。
核心配置文件application.properties內(nèi)容如下:
test.msg=Hello World SpringBoot
方式一:使用@Value方式(常用)
package Solin.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class WebController { @Value("${test.msg}") private String msg; @RequestMapping("/index1") public String index1(){ return "方式一:"+msg; } }
注意:在@Value的${}中包含的是核心配置文件中的鍵名。在Controller類(lèi)上加@RestController表示將此類(lèi)中的所有視圖都以JSON方式顯示,類(lèi)似于在視圖方法上加@ResponseBody。
訪問(wèn):http://localhost:8088/index1時(shí)得到:"方式一:Hello World SpringBoot"
方式二:使用Environment方式
package Solin.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class WebController { @Autowired private Environment env; @RequestMapping("/index2") public String index2(){ return "方式二:"+env.getProperty("test.msg"); } }
注意:這種方式是依賴(lài)注入Evnironment來(lái)完成,在創(chuàng)建的成員變量private Environment env上加上@Autowired注解即可完成依賴(lài)注入,然后使用env.getProperty("鍵名")即可讀取出對(duì)應(yīng)的值。
訪問(wèn):http://localhost:8088/index2時(shí)得到:"方式二:Hello World SpringBoot"
二.讀取自定義配置文件信息,例如:author.properties
為了不破壞核心文件的原生態(tài),但又需要有自定義的配置信息存在,一般情況下會(huì)選擇自定義配置文件來(lái)放這些自定義信息,這里在resources目錄下創(chuàng)建配置文件author.properties
resources/author.properties內(nèi)容如下:
author.name=Solin author.age=22
創(chuàng)建管理配置的實(shí)體類(lèi):
package Solin.controller; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; //加上注釋@Component,可以直接在其他地方使用@Autowired來(lái)創(chuàng)建其實(shí)例對(duì)象 @Component @ConfigurationProperties(prefix = "author",locations = "classpath:author.properties") public class MyWebConfig{ private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
注意:
在@ConfigurationProperties注釋中有兩個(gè)屬性:
- locations:指定配置文件的所在位置
- prefix:指定配置文件中鍵名稱(chēng)的前綴(我這里配置文件中所有鍵名都是以author.開(kāi)頭)
使用@Component是讓該類(lèi)能夠在其他地方被依賴(lài)使用,即使用@Autowired注釋來(lái)創(chuàng)建實(shí)例。
創(chuàng)建測(cè)試Controller
package Solin.controller; 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; @Controller public class ConfigController { @Autowired private MyWebConfig conf; @RequestMapping("/test") public @ResponseBody String test() { return "Name:"+conf.getName()+"---"+"Age:"+conf.getAge(); } }
注意:由于在Conf類(lèi)上加了注釋@Component,所以可以直接在這里使用@Autowired來(lái)創(chuàng)建其實(shí)例對(duì)象。
訪問(wèn):http://localhost:8088/test時(shí)得到:"Name:Solin---Age:22"
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot攔截器實(shí)現(xiàn)項(xiàng)目防止接口重復(fù)提交
基于SpringBoot框架來(lái)開(kāi)發(fā)業(yè)務(wù)后臺(tái)項(xiàng)目時(shí),接口重復(fù)提交是一個(gè)常見(jiàn)的問(wèn)題,本文主要介紹了SpringBoot攔截器實(shí)現(xiàn)項(xiàng)目防止接口重復(fù)提交,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09Java使用HttpClient實(shí)現(xiàn)Post請(qǐng)求實(shí)例
本篇文章主要介紹了Java使用HttpClient實(shí)現(xiàn)Post請(qǐng)求實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02將Java項(xiàng)目打包成可執(zhí)行的jar包
這篇文章主要介紹了將Java項(xiàng)目打包成可執(zhí)行的jar包,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06MyBatis中獲取Mysql數(shù)據(jù)庫(kù)插入記錄的主鍵值的實(shí)現(xiàn)
本文主要介紹了MyBatis中獲取Mysql數(shù)據(jù)庫(kù)插入記錄的主鍵值的實(shí)現(xiàn),包含了三種實(shí)現(xiàn)方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06Spring + mybatis + mysql使用事物的幾種方法總結(jié)
這篇文章主要給大家總結(jié)介紹了關(guān)于Spring + mybatis + mysql使用事物的幾種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05Java不借助第三變量實(shí)現(xiàn)兩數(shù)交換的示例
本文主要介紹了Java不借助第三變量實(shí)現(xiàn)兩數(shù)交換的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02Presto支持Elasticsearch數(shù)據(jù)源配置詳解
這篇文章主要為大家介紹了Presto支持Elasticsearch數(shù)據(jù)源配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12