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项目基于动态代理创建bean,再依赖注入取值

创建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    //动态代理
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取值

也是分两步,基于注解;动态代理,依赖注入

动态代理:

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测试结果

总结

每个项目只写了一种方法,都是用法层面,没有涉及原理。这些方法都经过测试,拿过去是可以直接使用。从配置文件中取值的方法还有很多,在实现功能的基础上大家可以自己查查资料。

以上就是java配置文件取值的多种方式总结的详细内容,更多关于java配置文件取值的资料请关注脚本之家其它相关文章!

相关文章

  • Java后端WebSocket的Tomcat实现

    Java后端WebSocket的Tomcat实现

    这篇文章主要介绍了Java后端WebSocket的Tomcat实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • idea创建spring boot项目时javaversion只能选择17和21解决办法

    idea创建spring boot项目时javaversion只能选择17和21解决办法

    这篇文章主要给大家介绍了关于idea创建spring boot项目时javaversion只能选择17和21的解决办法,文中通过代码介绍的非常详细,对大家学习或者工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2024-01-01
  • SpringBoot定时任务参数运行代码实例解析

    SpringBoot定时任务参数运行代码实例解析

    这篇文章主要介绍了SpringBoot定时任务运行代码实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • 浅谈Maven环境隔离应用

    浅谈Maven环境隔离应用

    这篇文章主要介绍了浅谈Maven环境隔离应用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09
  • Java老手该当心的13个错误

    Java老手该当心的13个错误

    这篇文章主要介绍了Java老手该当心的13个错误,需要的朋友可以参考下
    2015-04-04
  • Spring Boot启动流程分析

    Spring Boot启动流程分析

    本文给大家介绍spring boot是怎样启动和启动做了哪些事情。具体内容详情大家通过本文详细学习吧
    2017-09-09
  • Java实现大文件的分片上传与下载(springboot+vue3)

    Java实现大文件的分片上传与下载(springboot+vue3)

    这篇文章主要为大家详细介绍了java基于springboot+vue3如何大文件的分片上传与下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2023-06-06
  • Intellij IDEA连接Navicat数据库的方法

    Intellij IDEA连接Navicat数据库的方法

    这篇文章主要介绍了Intellij IDEA连接Navicat数据库的方法,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借价值,需要的朋友可以参考下
    2021-03-03
  • Java8深入学习系列(一)lambda表达式介绍

    Java8深入学习系列(一)lambda表达式介绍

    Java8最值得学习的特性就是Lambda表达式和Stream API,所以我们学习java8的第一课就是学习lambda表达式,下面这篇文章主要给大家介绍了关于Java8学习之lambda的相关资料,文中介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-08-08
  • springboot简单接入websocket的操作方法

    springboot简单接入websocket的操作方法

    这篇文章主要介绍了springboot简单接入websocket的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-05-05

最新评论