最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)控

    這篇文章主要介紹了Spring?Boot?+?Canal實現(xiàn)數據庫實時監(jiān)控,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-08-08
  • Java實現(xiàn)經典游戲復雜迷宮

    Java實現(xiàn)經典游戲復雜迷宮

    這篇文章主要介紹了如何利用java語言實現(xiàn)經典《復雜迷宮》游戲,文中采用了swing技術進行了界面化處理,感興趣的小伙伴可以動手試一試
    2022-02-02
  • 如何基于sqlite實現(xiàn)kafka延時消息詳解

    如何基于sqlite實現(xiàn)kafka延時消息詳解

    這篇文章主要給大家介紹了關于如何基于sqlite實現(xiàn)kafka延時消息的相關資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2022-01-01
  • SpringBoot yml配置文件讀取方法詳解

    SpringBoot yml配置文件讀取方法詳解

    這篇文章主要介紹了SpringBoot yml配置文件讀取方法,項目開發(fā)中難免要讀取配置文件,本文結合開發(fā)經驗介紹幾種使用過的讀取配置文件的方法
    2022-10-10
  • java進行文件讀寫操作詳解

    java進行文件讀寫操作詳解

    這篇文章主要介紹了java進行文件讀寫操作詳解的相關資料,需要的朋友可以參考下
    2014-10-10
  • 在springboot中使用注解將值注入參數的操作

    在springboot中使用注解將值注入參數的操作

    這篇文章主要介紹了在springboot中使用注解將值注入參數的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • Java詳解Swing中的幾種常用按鈕的使用

    Java詳解Swing中的幾種常用按鈕的使用

    這篇文章主要介紹了怎么用Java來創(chuàng)建和使用Swing中的幾種常用按鈕,按鈕是我們經常要用的工具,但是你有想過自己怎么去實現(xiàn)它嗎,感興趣的朋友跟隨文章往下看看吧
    2022-04-04
  • 如何使用Java實現(xiàn)指定概率的抽獎

    如何使用Java實現(xiàn)指定概率的抽獎

    這篇文章主要給大家介紹了關于如何使用Java實現(xiàn)指定概率的抽獎的相關資料,Java抽獎程序的基本原理是通過隨機數生成器來實現(xiàn)隨機抽獎的功能,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-07-07
  • 解決springboot 實體類String轉Date類型的坑

    解決springboot 實體類String轉Date類型的坑

    這篇文章主要介紹了解決springboot 實體類String轉Date類型的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • 告別無盡等待:Java中的輪詢終止技巧

    告別無盡等待:Java中的輪詢終止技巧

    在Java中,輪詢是一種常見的處理方式,用于檢查某個條件是否滿足,直到滿足條件或達到一定的時間限制,本文將介紹Java中常用的輪詢結束方式,包括使用循環(huán)、定時器和線程池等方法,需要的朋友可以參考下
    2023-10-10

最新評論

宁南县| 贺兰县| 城口县| 冕宁县| 阿拉尔市| 贡嘎县| 大同县| 常德市| 荃湾区| 扶绥县| 容城县| 祁东县| 科技| 虹口区| 安仁县| 南丹县| 临泽县| 邢台市| 平昌县| 湘潭市| 曲阜市| 深圳市| 望江县| 修水县| 和田市| 陆良县| 山阴县| 新郑市| 肇源县| 常德市| 丹凤县| 平度市| 沧州市| 砚山县| 花莲县| 京山县| 南安市| 上林县| 乌拉特后旗| 兴化市| 青河县|