亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2023年11月17日 14:29:38   作者:deelless  
這篇文章主要為大家詳細(xì)介紹了java配置文件取值的多種方式,包括一般項(xiàng)目,國際化項(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提供讀取配置文件工具類
	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.國際化項(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("國際化配置文件中的值是:"+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,再依賴注入取值

創(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è)試注解需要的依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>

單元測(cè)試,依賴注入

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)代理,依賴注入

動(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;
	}
}

依賴注入

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)目只寫了一種方法,都是用法層面,沒有涉及原理。這些方法都經(jīng)過測(cè)試,拿過去是可以直接使用。從配置文件中取值的方法還有很多,在實(shí)現(xiàn)功能的基礎(chǔ)上大家可以自己查查資料。

以上就是java配置文件取值的多種方式總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于java配置文件取值的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 使用Spring?Cloud?Stream處理Java消息流的操作流程

    使用Spring?Cloud?Stream處理Java消息流的操作流程

    Spring?Cloud?Stream是一個(gè)用于構(gòu)建消息驅(qū)動(dòng)微服務(wù)的框架,能夠與各種消息中間件集成,如RabbitMQ、Kafka等,今天我們來探討如何使用Spring?Cloud?Stream來處理Java消息流,需要的朋友可以參考下
    2024-08-08
  • Spring Boot 整合持久層之JdbcTemplate

    Spring Boot 整合持久層之JdbcTemplate

    持久層是 Java EE 中訪問數(shù)據(jù)庫的核心操作,Spring Boot 中對(duì)常見的持久層框架都提供了自動(dòng)化配置,例如 JdbcTemplate 、 JPA 等,Mybatis 的自動(dòng)化配置則是 Mybatis 官方提供的
    2022-08-08
  • SpringBoot集成Aviator實(shí)現(xiàn)參數(shù)校驗(yàn)的示例代碼

    SpringBoot集成Aviator實(shí)現(xiàn)參數(shù)校驗(yàn)的示例代碼

    在實(shí)際開發(fā)中,參數(shù)校驗(yàn)是保障系統(tǒng)穩(wěn)定和數(shù)據(jù)可靠性的重要措施,Aviator 是一個(gè)高性能的表達(dá)式引擎,它能夠簡化復(fù)雜的邏輯判斷并提升參數(shù)校驗(yàn)的靈活性,本文將介紹如何在 Spring Boot 中集成 Aviator,并利用它來實(shí)現(xiàn)靈活的參數(shù)校驗(yàn),需要的朋友可以參考下
    2025-02-02
  • springboot @validated List校驗(yàn)失效問題

    springboot @validated List校驗(yàn)失效問題

    這篇文章主要介紹了springboot @validated List校驗(yàn)失效問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 如何在 Linux 上搭建 java 部署環(huán)境(安裝jdk/tomcat/mysql) + 將程序部署到云服務(wù)器上的操作)

    如何在 Linux 上搭建 java 部署環(huán)境(安裝jdk/tomcat/mys

    這篇文章主要介紹了如何在 Linux 上搭建 java 部署環(huán)境(安裝jdk/tomcat/mysql) + 將程序部署到云服務(wù)器上的操作),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-01-01
  • SpringBoot+Vue項(xiàng)目部署實(shí)現(xiàn)傳統(tǒng)方式

    SpringBoot+Vue項(xiàng)目部署實(shí)現(xiàn)傳統(tǒng)方式

    我們?cè)谶M(jìn)行前后端分離開發(fā)的時(shí)候,一般是將前端項(xiàng)目部署到nginx服務(wù)器上,與后端項(xiàng)目分開部署,這篇文章主要給大家介紹了關(guān)于SpringBoot+Vue項(xiàng)目部署實(shí)現(xiàn)傳統(tǒng)方式的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • 探索Java分布式限流技術(shù)

    探索Java分布式限流技術(shù)

    探索Java分布式限流技術(shù),讓你的系統(tǒng)遠(yuǎn)離流量過載的煩惱,本指南將帶你了解如何使用Java實(shí)現(xiàn)高效的限流策略,幫助你輕松應(yīng)對(duì)高并發(fā)場景,讓我們一起開啟這段精彩的技術(shù)之旅,打造更加穩(wěn)定可靠的系統(tǒng),需要的朋友可以參考下
    2024-03-03
  • SpringBoot項(xiàng)目中使用OkHttp獲取IP地址的示例代碼

    SpringBoot項(xiàng)目中使用OkHttp獲取IP地址的示例代碼

    OkHttp?是一個(gè)由?Square?開發(fā)的高效、現(xiàn)代的?HTTP?客戶端庫,用于?Android?和?Java?應(yīng)用程序,它支持?HTTP/2?和?SPDY?等現(xiàn)代網(wǎng)絡(luò)協(xié)議,并提供了多種功能和優(yōu)化,本文給大家介紹了SpringBoot項(xiàng)目中如何獲取IP地址,需要的朋友可以參考下
    2024-08-08
  • 詳解SpringBoot配置連接池

    詳解SpringBoot配置連接池

    本篇文章主要詳解SpringBoot配置連接池,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • 從匯編碼分析java對(duì)象的創(chuàng)建過程(推薦)

    從匯編碼分析java對(duì)象的創(chuàng)建過程(推薦)

    這篇文章主要介紹了從匯編碼分析java對(duì)象的創(chuàng)建過程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03

最新評(píng)論