java配置文件取值的多種方式總結
更新時間:2023年11月17日 14:29:38 作者:deelless
這篇文章主要為大家詳細介紹了java配置文件取值的多種方式,包括一般項目,國際化項目,springboot項目,文中的示例代碼講解詳細,需要的可以參考下
1.一般項目
1.1demo結構如下

1.2取值
import java.io.InputStream;
import java.util.Properties;
public class JavaConfigTest {
private static final String CONFIG_FILE= "config.properties";
//java jdk提供讀取配置文件工具類
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測試結果

2.國際化項目
2.1demo結構

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("國際化配置文件中的值是:"+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測試結果

3.SpringBoot項目
3.1demo結構

3.2 取值
springboot項目基于動態(tài)代理創(chuàng)建bean,再依賴注入取值
創(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 //動態(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單元測試注解需要的依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
單元測試,依賴注入
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結果是:"+sentinel);
System.out.println("配置文件輸出的aaa結果是:"+aaa);
}
}
3.3測試結果

4.SpringBoot項目yml文件取值
4.1demo結構

4.2取值
也是分兩步,基于注解;動態(tài)代理,依賴注入
動態(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;
}
}
依賴注入
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結果是:"+sentinel);
System.out.println("配置文件輸出的aaa結果是:"+aaa);
}
}
4.3測試結果

總結
每個項目只寫了一種方法,都是用法層面,沒有涉及原理。這些方法都經過測試,拿過去是可以直接使用。從配置文件中取值的方法還有很多,在實現(xiàn)功能的基礎上大家可以自己查查資料。
以上就是java配置文件取值的多種方式總結的詳細內容,更多關于java配置文件取值的資料請關注腳本之家其它相關文章!
相關文章
Spring?Boot?+?Canal?實現(xiàn)數據庫實時監(jiān)控
這篇文章主要介紹了Spring?Boot?+?Canal實現(xiàn)數據庫實時監(jiān)控,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08
解決springboot 實體類String轉Date類型的坑
這篇文章主要介紹了解決springboot 實體類String轉Date類型的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10

