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

java配置文件取值的多種方式總結(jié)

 更新時(shí)間:2023年11月17日 14:29:38   作者:deelless  
這篇文章主要為大家詳細(xì)介紹了java配置文件取值的多種方式,包括一般項(xiàng)目,國(guó)際化項(xiàng)目,springboot項(xiàng)目,文中的示例代碼講解詳細(xì),需要的可以參考下

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

    這篇文章主要介紹了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實(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í)消息詳解

    如何基于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 yml配置文件讀取方法詳解

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

    這篇文章主要介紹了SpringBoot yml配置文件讀取方法,項(xiàng)目開(kāi)發(fā)中難免要讀取配置文件,本文結(jié)合開(kāi)發(fā)經(jīng)驗(yàn)介紹幾種使用過(guò)的讀取配置文件的方法
    2022-10-10
  • java進(jìn)行文件讀寫(xiě)操作詳解

    java進(jìn)行文件讀寫(xiě)操作詳解

    這篇文章主要介紹了java進(jìn)行文件讀寫(xiě)操作詳解的相關(guān)資料,需要的朋友可以參考下
    2014-10-10
  • 在springboot中使用注解將值注入?yún)?shù)的操作

    在springboot中使用注解將值注入?yún)?shù)的操作

    這篇文章主要介紹了在springboot中使用注解將值注入?yún)?shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • Java詳解Swing中的幾種常用按鈕的使用

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

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

    如何使用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)型的坑

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

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

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

最新評(píng)論

临城县| 连州市| 延寿县| 阆中市| 马边| 渝中区| 泸西县| 南江县| 彭阳县| 迁西县| 板桥市| 班戈县| 长寿区| 鹰潭市| 怀来县| 同德县| 柞水县| 通城县| 宣化县| 五华县| 包头市| 门源| 花垣县| 慈溪市| 阿克| 永城市| 从化市| 双牌县| 城步| 南部县| 砀山县| 方山县| 北辰区| 浏阳市| 潜山县| 贵南县| 新余市| 开远市| 留坝县| 荥经县| 郸城县|