色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

詳解SpringBoot注解讀取配置文件的方式

瀏覽:16日期:2023-03-25 11:10:08
一、@Value讀取application.properties配置文件中的值

application.properties配置文件

fileName=configName

PropertiesConfig類文件

import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;@Componentpublic class PropertiesConfig { //通過@Value注解讀取fileName的值 @Value('${fileName}') private String fileName; public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; }}

測試

import com.model.PropertiesConfig;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTestpublic class PropertiesConfigTest { @Autowired private PropertiesConfig propertiesConfig; @Test public void test(){ System.out.println(propertiesConfig.getFileName());//結果輸出:configName assert 'configName'.equals(propertiesConfig.getFileName()); }}二、@ConfigurationProperties讀取多個application.properties配置文件中的值

application.properties文件

database.username=rootdatabase.password=root

DatabaseConfig類文件

import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Component@ConfigurationProperties('database')public class DatabaseConfig { private String userName; private String passWord; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; }}

測試

import com.model.DatabaseConfig;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTestpublic class DatabaseConfigTest { @Autowired private DatabaseConfig databaseConfig; @Test public void test(){ System.out.println('username = ' + databaseConfig.getUserName() +', password = '+databaseConfig.getPassWord());//結果輸出:username = root, password = root assert 'root'.equals(databaseConfig.getUserName()); assert 'root'.equals(databaseConfig.getPassWord()); }}三、@PropertySource讀取任意配置文件

新建property-source.properties配置文件

fileName=configNamedatabase.username=rootdatabase.password=root

PropertySourceConfig類文件

import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component;@Component@PropertySource(value = {'classpath:property-source.properties'})@ConfigurationProperties('database')public class PropertySourceConfig { @Value('${fileName}') private String fileName; private String userName; private String passWord; public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; }}

測試

import com.model.PropertySourceConfig;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTestpublic class PropertySourceConfigTest { @Autowired private PropertySourceConfig propertySourceConfig; @Test public void test(){ assert 'configName'.equals(propertySourceConfig.getFileName()); System.out.println('fileName = ' + propertySourceConfig.getFileName());//結果輸出:configName assert 'root'.equals(propertySourceConfig.getUserName()); System.out.println(propertySourceConfig.getUserName());//結果輸出:root assert 'root'.equals(propertySourceConfig.getPassWord()); System.out.println(propertySourceConfig.getPassWord());//結果輸出:root }}

完整代碼鏈接:read-config-file項目地址

到此這篇關于詳解SpringBoot注解讀取配置文件的方式的文章就介紹到這了,更多相關SpringBoot注解讀取配置文件內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 欧美日本一区亚洲欧美一区 | 2021国产精品自在拍在线播放 | 亚洲成人手机在线观看 | 中文字幕乱码视频32 | 九九99精品 | 美女双腿打开让男人桶爽网站 | 成人在线免费视频 | avtt天堂网永久资源手机版 | 欧美日韩亚洲另类 | 国产精品久久久久影院色老大 | 精品91一区二区三区 | 中文字幕国产专区 | 亚洲视频在线免费 | 美女流白浆网站 | 99精品久久精品一区二区 | 国产精品久久一区一区 | 成年视频国产免费观看 | 一a一级片| 欧美一区二区视频 | 欧美视频在线一区 | 成人久久免费视频 | 国产97视频在线 | 国产午夜小视频 | 精品一区二区三区视频在线观看 | 国产亚洲一区二区三区 | 99国产精品视频久久久久 | 国产成人狂喷潮在线观看2345 | 欧美视频一区二区三区精品 | 依人久久 | 亚洲精品国产成人7777 | 久久精品全国免费观看国产 | 久草国产在线观看 | 黄色成人毛片 | 国产亚洲精品yxsp | 性欧洲精品videos' | 欧美一级毛片欧美一级成人毛片 | 成人影视免费 | 中国国产一级毛片视频 | 99国产在线播放 | 国产经典一区 | 欧美日韩高清在线观看一区二区 |