java配置文件取值的多种方式总结
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配置文件取值的资料请关注脚本之家其它相关文章!
相关文章
使用Spring Cloud Stream处理Java消息流的操作流程
Spring Cloud Stream是一个用于构建消息驱动微服务的框架,能够与各种消息中间件集成,如RabbitMQ、Kafka等,今天我们来探讨如何使用Spring Cloud Stream来处理Java消息流,需要的朋友可以参考下2024-08-08
Spring Boot 整合持久层之JdbcTemplate
持久层是 Java EE 中访问数据库的核心操作,Spring Boot 中对常见的持久层框架都提供了自动化配置,例如 JdbcTemplate 、 JPA 等,Mybatis 的自动化配置则是 Mybatis 官方提供的2022-08-08
SpringBoot集成Aviator实现参数校验的示例代码
在实际开发中,参数校验是保障系统稳定和数据可靠性的重要措施,Aviator 是一个高性能的表达式引擎,它能够简化复杂的逻辑判断并提升参数校验的灵活性,本文将介绍如何在 Spring Boot 中集成 Aviator,并利用它来实现灵活的参数校验,需要的朋友可以参考下2025-02-02
springboot @validated List校验失效问题
这篇文章主要介绍了springboot @validated List校验失效问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-07-07
如何在 Linux 上搭建 java 部署环境(安装jdk/tomcat/mys
这篇文章主要介绍了如何在 Linux 上搭建 java 部署环境(安装jdk/tomcat/mysql) + 将程序部署到云服务器上的操作),本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-01-01
SpringBoot项目中使用OkHttp获取IP地址的示例代码
OkHttp 是一个由 Square 开发的高效、现代的 HTTP 客户端库,用于 Android 和 Java 应用程序,它支持 HTTP/2 和 SPDY 等现代网络协议,并提供了多种功能和优化,本文给大家介绍了SpringBoot项目中如何获取IP地址,需要的朋友可以参考下2024-08-08


最新评论