久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

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

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

瀏覽:22日期: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
相關文章:
主站蜘蛛池模板: 黄色一级大片在线免费看产 | 91p在线观看 | 美女午夜影院 | 国产婷婷在线视频 | 亚洲欧美日韩电影 | 99久久夜色精品国产亚洲1000部 | 互换娇妻呻吟hd中文字幕 | 国产视频二 | 日韩中文一区二区三区 | 99成人| 欧美黄色一区 | 91久久久久久久久久久久久久 | 五月婷婷天 | 欧洲尺码日本国产精品 | 精品欧美一区二区三区 | 国产91色在线 | 亚洲 | 国产黄色影视 | 999精品免费| 国产成年免费视频 | 亚洲精品久久久久一区二区三区 | 人人草天天草 | 1000部精品久久久久久久久 | 精品国产乱码久久久久久蜜柚 | 99精品欧美一区二区三区综合在线 | 激情久久av一区av二区av三区 | 综合精品久久久 | 欧日韩免费 | 国产一区二区av | 日韩欧美国产成人一区二区 | 日日夜夜草 | 高清久久 | 亚洲精品综合中文字幕 | 日韩午夜电影在线观看 | 狠狠综合| 中文字幕在线第一页 | 亚洲一区中文 | 日韩欧美一区二区三区免费观看 | 亚洲成人免费在线 | 免费av电影观看 | 欧美 亚洲 一区 | 国产精品视频一区二区三区四 |