java配置文件取值的几种方式(一般项目,国际化项目,springboot项目)

1.一般项目

1.1 :demo结构如下:
在这里插入图片描述

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.1 :demo结构
在这里插入图片描述
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.1 demo结构
在这里插入图片描述
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.1 :demo结构
在这里插入图片描述
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 :测试结果
在这里插入图片描述

总结:每个项目只写了一种方法,都是用法层面,没有涉及原理。这些方法都经过测试,拿过去是可以直接使用。从配置文件中取值的方法还有很多,在实现功能的基础上大家可以自己查查资料。
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值