java配置文件取值的多種方式總結(jié)
1.一般項(xiàng)目
1.1demo結(jié)構(gòu)如下

1.2取值
import java.io.InputStream;
import java.util.Properties;
public class JavaConfigTest {
private static final String CONFIG_FILE= "config.properties";
//java jdk提供讀取配置文件工具類(lèi)
private static Properties props=new Properties();
public static void main(String[] args){
String value = getConfigValue("aaa");
System.out.println("配置文件中的值是:"+value);
}
public static String getConfigValue(String key){
String value=null;
try {
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(CONFIG_FILE);
if(in!=null){
props.load(in);
value = props.getProperty(key);
in.close();
}
}catch (Exception e){
e.printStackTrace();
}
return value;
}
}
1.3測(cè)試結(jié)果

2.國(guó)際化項(xiàng)目
2.1demo結(jié)構(gòu)

2.2取值
import java.util.Locale;
import java.util.ResourceBundle;
public class I18NConfigTest {
public static void main(String[] args){
String value = GetI18nConfigValue("ccc");
System.out.println("國(guó)際化配置文件中的值是:"+value);
}
public static String GetI18nConfigValue(String key){
Locale currentLocale = new Locale("en","US");
ResourceBundle bundle = ResourceBundle.getBundle("messages_en", currentLocale);
String value = bundle.getString(key);
return value;
}
}
2.3測(cè)試結(jié)果

3.SpringBoot項(xiàng)目
3.1demo結(jié)構(gòu)

3.2 取值
springboot項(xiàng)目基于動(dòng)態(tài)代理創(chuàng)建bean,再依賴(lài)注入取值
創(chuàng)建bean
package com.example.demo.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component //動(dòng)態(tài)代理
public class SpringBootCongigTest {
//配置文件中的key
@Value("${sentinel}")
private String sentinel;
@Value("${aaa}")
private String aaa;
public String getSentinel() {
return sentinel;
}
public void setSentinel(String sentinel) {
this.sentinel = sentinel;
}
public String getAaa() {
return aaa;
}
public void setAaa(String aaa) {
this.aaa = aaa;
}
}
springboot單元測(cè)試注解需要的依賴(lài):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
單元測(cè)試,依賴(lài)注入
import com.example.demo.DemoApplication;
import com.example.demo.config.SpringBootCongigTest;
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)
@SpringBootTest(classes = DemoApplication.class)
public class TestConfig {
@Autowired
private SpringBootCongigTest springBootCongigTest;
@Test
public void testSpringBootConfig(){
String aaa = springBootCongigTest.getAaa();
String sentinel = springBootCongigTest.getSentinel();
System.out.println("配置文件輸出的sentinel結(jié)果是:"+sentinel);
System.out.println("配置文件輸出的aaa結(jié)果是:"+aaa);
}
}
3.3測(cè)試結(jié)果

4.SpringBoot項(xiàng)目yml文件取值
4.1demo結(jié)構(gòu)

4.2取值
也是分兩步,基于注解;動(dòng)態(tài)代理,依賴(lài)注入
動(dòng)態(tài)代理:
package com.example.demo.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "redis")
public class SpringBootCongigTest {
private String sentinel;
private String aaa;
public String getSentinel() {
return sentinel;
}
public void setSentinel(String sentinel) {
this.sentinel = sentinel;
}
public String getAaa() {
return aaa;
}
public void setAaa(String aaa) {
this.aaa = aaa;
}
}
依賴(lài)注入
import com.example.demo.DemoApplication;
import com.example.demo.config.SpringBootCongigTest;
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)
@SpringBootTest(classes = DemoApplication.class)
public class TestConfig {
@Autowired
private SpringBootCongigTest springBootCongigTest;
@Test
public void testSpringBootConfig(){
String aaa = springBootCongigTest.getAaa();
String sentinel = springBootCongigTest.getSentinel();
System.out.println("配置文件輸出的sentinel結(jié)果是:"+sentinel);
System.out.println("配置文件輸出的aaa結(jié)果是:"+aaa);
}
}
4.3測(cè)試結(jié)果

總結(jié)
每個(gè)項(xiàng)目只寫(xiě)了一種方法,都是用法層面,沒(méi)有涉及原理。這些方法都經(jīng)過(guò)測(cè)試,拿過(guò)去是可以直接使用。從配置文件中取值的方法還有很多,在實(shí)現(xiàn)功能的基礎(chǔ)上大家可以自己查查資料。
以上就是java配置文件取值的多種方式總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于java配置文件取值的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring?Boot?+?Canal?實(shí)現(xiàn)數(shù)據(jù)庫(kù)實(shí)時(shí)監(jiān)控
這篇文章主要介紹了Spring?Boot?+?Canal實(shí)現(xiàn)數(shù)據(jù)庫(kù)實(shí)時(shí)監(jiān)控,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
Java實(shí)現(xiàn)經(jīng)典游戲復(fù)雜迷宮
這篇文章主要介紹了如何利用java語(yǔ)言實(shí)現(xiàn)經(jīng)典《復(fù)雜迷宮》游戲,文中采用了swing技術(shù)進(jìn)行了界面化處理,感興趣的小伙伴可以動(dòng)手試一試2022-02-02
如何基于sqlite實(shí)現(xiàn)kafka延時(shí)消息詳解
這篇文章主要給大家介紹了關(guān)于如何基于sqlite實(shí)現(xiàn)kafka延時(shí)消息的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01
在springboot中使用注解將值注入?yún)?shù)的操作
這篇文章主要介紹了在springboot中使用注解將值注入?yún)?shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
如何使用Java實(shí)現(xiàn)指定概率的抽獎(jiǎng)
這篇文章主要給大家介紹了關(guān)于如何使用Java實(shí)現(xiàn)指定概率的抽獎(jiǎng)的相關(guān)資料,Java抽獎(jiǎng)程序的基本原理是通過(guò)隨機(jī)數(shù)生成器來(lái)實(shí)現(xiàn)隨機(jī)抽獎(jiǎng)的功能,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07
解決springboot 實(shí)體類(lèi)String轉(zhuǎn)Date類(lèi)型的坑
這篇文章主要介紹了解決springboot 實(shí)體類(lèi)String轉(zhuǎn)Date類(lèi)型的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10

