av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

瀏覽:30日期: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 { //通過(guò)@Value注解讀取fileName的值 @Value('${fileName}') private String fileName; public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; }}

測(cè)試

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());//結(jié)果輸出:configName assert 'configName'.equals(propertiesConfig.getFileName()); }}二、@ConfigurationProperties讀取多個(gè)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; }}

測(cè)試

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());//結(jié)果輸出: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; }}

測(cè)試

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());//結(jié)果輸出:configName assert 'root'.equals(propertySourceConfig.getUserName()); System.out.println(propertySourceConfig.getUserName());//結(jié)果輸出:root assert 'root'.equals(propertySourceConfig.getPassWord()); System.out.println(propertySourceConfig.getPassWord());//結(jié)果輸出:root }}

完整代碼鏈接:read-config-file項(xiàng)目地址

到此這篇關(guān)于詳解SpringBoot注解讀取配置文件的方式的文章就介紹到這了,更多相關(guān)SpringBoot注解讀取配置文件內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 牛牛热在线视频 | 成人免费在线电影 | 91久久精 | 国产一区二区三区网站 | 精品久久久久久 | 99r在线 | 中文字幕av高清 | 91精品国产一区二区三区 | 国产精品jizz在线观看老狼 | 亚洲精品国产电影 | 日韩精品在线一区二区 | 99国产精品99久久久久久 | a级片在线 | 欧美色综合| 国产四虎 | 欧美黑人又粗大 | 久久精品国产免费高清 | 精品国产精品一区二区夜夜嗨 | 亚洲欧美激情四射 | 日韩一区精品 | 99色在线视频 | 国产精品久久久久aaaa | 亚洲夜夜爽 | 国产精品视频导航 | 99久久亚洲 | 狠狠操天天操 | 国产免费a视频 | 日韩欧美国产精品综合嫩v 一区中文字幕 | 精品一区二区久久 | 亚洲一区二区久久 | 欧美综合国产精品久久丁香 | 一级全黄视频 | 久久九九影视 | 欧美日韩在线精品 | 中文一区 | 久久高潮| 综合色久 | 亚洲成人网在线播放 | 国产成人艳妇aa视频在线 | 成人在线中文字幕 | 成人在线观看免费爱爱 |