亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

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

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

瀏覽:20日期: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
相關文章:
主站蜘蛛池模板: 日韩免费精品一级毛片 | 99re热久久精品这里都是精品 | 中国日本欧美韩国18 | 特级黄毛片 | 欧美日韩一区二区综合在线视频 | 免费欧美日韩 | 色片网站在线观看 | 特黄特色大片免费播放 | 欧美亚洲精品一区二三 | 久久一er精这里有精品 | 日本aaaa级毛片在线看 | 欧美一级刺激毛片 | 免费看的黄色网址 | 亚洲国产青草 | 国产精品1区2区 | 国产91av视频在线观看 | 精品一区二区三区色花堂 | 国产三级观看久久 | 久在草视频 | 色多多视频成人影院 | 国产欧美日韩在线观看 | 久久亚洲精品国产精品777777 | α毛片| 欧美久久精品一级c片片 | 成年人免费网站视频 | 黄色片免费 | 国产精品色图 | 国产精品青草久久久久婷婷 | 亚洲伊人精品综合在合线 | 美国免费高清一级毛片 | 欧美日韩免费在线视频 | 亚洲福利秒拍一区二区 | 国产亚洲情侣久久精品 | 日韩精品一区二区三区高清 | 日韩毛片在线免费观看 | 伊人久久婷婷 | 日本护士xxxx爽爽爽 | 国产超级乱淫片中文 | 欧美三级视频在线观看 | 青青草国产免费久久久91 | 国产成人精品免费影视大全 |