java配置文件取值的多種方式總結
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測試結果

總結
每個項目只寫了一種方法,都是用法層面,沒有涉及原理。這些方法都經(jīng)過測試,拿過去是可以直接使用。從配置文件中取值的方法還有很多,在實現(xiàn)功能的基礎上大家可以自己查查資料。
以上就是java配置文件取值的多種方式總結的詳細內(nèi)容,更多關于java配置文件取值的資料請關注腳本之家其它相關文章!
相關文章
使用Spring?Cloud?Stream處理Java消息流的操作流程
Spring?Cloud?Stream是一個用于構建消息驅(qū)動微服務的框架,能夠與各種消息中間件集成,如RabbitMQ、Kafka等,今天我們來探討如何使用Spring?Cloud?Stream來處理Java消息流,需要的朋友可以參考下2024-08-08
Spring Boot 整合持久層之JdbcTemplate
持久層是 Java EE 中訪問數(shù)據(jù)庫的核心操作,Spring Boot 中對常見的持久層框架都提供了自動化配置,例如 JdbcTemplate 、 JPA 等,Mybatis 的自動化配置則是 Mybatis 官方提供的2022-08-08
SpringBoot集成Aviator實現(xiàn)參數(shù)校驗的示例代碼
在實際開發(fā)中,參數(shù)校驗是保障系統(tǒng)穩(wěn)定和數(shù)據(jù)可靠性的重要措施,Aviator 是一個高性能的表達式引擎,它能夠簡化復雜的邏輯判斷并提升參數(shù)校驗的靈活性,本文將介紹如何在 Spring Boot 中集成 Aviator,并利用它來實現(xiàn)靈活的參數(shù)校驗,需要的朋友可以參考下2025-02-02
springboot @validated List校驗失效問題
這篇文章主要介紹了springboot @validated List校驗失效問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
如何在 Linux 上搭建 java 部署環(huán)境(安裝jdk/tomcat/mys
這篇文章主要介紹了如何在 Linux 上搭建 java 部署環(huán)境(安裝jdk/tomcat/mysql) + 將程序部署到云服務器上的操作),本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-01-01
SpringBoot+Vue項目部署實現(xiàn)傳統(tǒng)方式
我們在進行前后端分離開發(fā)的時候,一般是將前端項目部署到nginx服務器上,與后端項目分開部署,這篇文章主要給大家介紹了關于SpringBoot+Vue項目部署實現(xiàn)傳統(tǒng)方式的相關資料,需要的朋友可以參考下2024-01-01
SpringBoot項目中使用OkHttp獲取IP地址的示例代碼
OkHttp?是一個由?Square?開發(fā)的高效、現(xiàn)代的?HTTP?客戶端庫,用于?Android?和?Java?應用程序,它支持?HTTP/2?和?SPDY?等現(xiàn)代網(wǎng)絡協(xié)議,并提供了多種功能和優(yōu)化,本文給大家介紹了SpringBoot項目中如何獲取IP地址,需要的朋友可以參考下2024-08-08

