SpringBoot读取配置文件的五种方法总结

 更新时间:2022年08月17日 09:29:58   作者:Java中文社群  
这篇文章主要为大家详细介绍了SpringBoot读取配置文件的五种方法,文中的示例代码讲解详细,对我们学习SpringBoot有一定帮助,需要的可以参考一下

Spring Boot 中读取配置文件有以下 5 种方法:

  • 使用 @Value 读取配置文件。
  • 使用 @ConfigurationProperties 读取配置文件。
  • 使用 Environment 读取配置文件。
  • 使用 @PropertySource 读取配置文件。
  • 使用原生方式读取配置文件。

它们的具体使用方法如下,为了方便测试,我们在 Spring Boot 配置文件 application.properties 添加以下内容:

profile.name=Spring Boot Profile
profile.desc=Spring Boot Profile Desc.

1.使用 @Value 读取配置文件

使用 @Value 可以读取单个配置项,如下代码所示:

@SpringBootApplication
public class DemoApplication implements InitializingBean {
    @Value("${profile.name}")
    private String name;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("My Profile Name:" + name);
    }
}

以上程序的执行结果如下图所示:

2.使用 @ConfigurationProperties 读取配置文件

@ConfigurationProperties 和 @Value 的使用略微不同,@Value 是读取单个配置项的,而 @ConfigurationProperties 是读取一组配置项的,我们可以使用 @ConfigurationProperties 加实体类读取一组配置项,如下代码所示:

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "profile")
@Data
public class Profile {
    private String name;
    private String desc;
}

其中 prefix 表示读取一组配置项的根 name,相当于 Java 中的类名,最后再把此配置类,注入到某一个类中就可以使用了,如下代码所示:

@SpringBootApplication
public class DemoApplication implements InitializingBean {
    @Autowired
    private Profile profile;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Profile Object:" + profile);
    }
}

以上程序的执行结果如下图所示:

3.使用 Environment 读取配置文件

Environment 是 Spring Core 中的一个用于读取配置文件的类,将此类使用 @Autowired 注入到类中就可以使用它的 getProperty 方法来获取某个配置项的值了,如下代码所示:

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;

@SpringBootApplication
public class DemoApplication implements InitializingBean {

    @Autowired
    private Environment environment;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Profile Name:" + environment.getProperty("profile.name"));
    }
}

以上程序的执行结果如下图所示:

4.使用 @PropertySource 读取配置文件

使用 @PropertySource 注解可以用来指定读取某个配置文件,比如指定读取 application.properties 配置文件的配置内容,具体实现代码如下:

@SpringBootApplication
@PropertySource("classpath:application.properties")
public class DemoApplication implements InitializingBean {
    @Value("${profile.name}")
    private String name;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Name:" + name);
    }
}

以上程序的执行结果如下图所示:

中文乱码

如果配置文件中出现中文乱码的情况,可通过指定编码格式的方式来解决中文乱码的问题,具体实现如下:

@PropertySource(value = "dev.properties", encoding = "utf-8")

注意事项

@PropertySource 注解默认是只支持 properties 格式配置文件的读取的。

5.使用原生方式读取配置文件

我们还可以使用最原始的方式 Properties 对象来读取配置文件,如下代码所示:

import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

@SpringBootApplication
public class DemoApplication implements InitializingBean {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Properties props = new Properties();
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(
                    this.getClass().getClassLoader().getResourceAsStream("application.properties"),
                    StandardCharsets.UTF_8);
            props.load(inputStreamReader);
        } catch (IOException e1) {
            System.out.println(e1);
        }
        System.out.println("Properties Name:" + props.getProperty("profile.name"));
    }
}

以上程序的执行结果如下图所示:

总结

在 Spring Boot 中读取配置文件有以下 5 种方法:

  • 使用 @Value 读取配置文件。
  • 使用 @ConfigurationProperties 读取配置文件。
  • 使用 @PropertySource 读取配置文件。
  • 使用 Environment 读取配置文件。
  • 使用原生方式读取配置文件。

其中最常用的是前 3 种,如果读取某一个配置项可使用 @Value,如果读取一组配置项可使用 @ConfigurationProperties,如果要指定读取某一个具体的配置文件可使用 @PropertySource 来指定。

到此这篇关于SpringBoot读取配置文件的五种方法总结的文章就介绍到这了,更多相关SpringBoot读取配置文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Kafka 安装与配置详细过程

    Kafka 安装与配置详细过程

    本节详细介绍 Kafka 运行环境的搭建,为了节省篇幅,本节的内容以 Linux CentOS 作为安装演示的操作系统,其他 Linux 系列的操作系统也可以参考本节的内容,对Kafka 安装与配置相关知识感兴趣的朋友一起看看吧
    2021-11-11
  • 基于Spring Security的Oauth2授权实现方法

    基于Spring Security的Oauth2授权实现方法

    这篇文章主要介绍了基于Spring Security的Oauth2授权实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09
  • Java 超详细讲解字符流

    Java 超详细讲解字符流

    字符流就是在字节流的基础上,加上编码,形成的数据流,字符流出现的意义是因为字节流在操作字符时,可能会有中文导致的乱码,所以由字节流引申出了字符流
    2022-04-04
  • 自定义log4j日志文件命名规则说明

    自定义log4j日志文件命名规则说明

    这篇文章主要介绍了自定义log4j日志文件命名规则说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • Java后台接口开发初步实战教程

    Java后台接口开发初步实战教程

    下面小编就为大家分享一篇 Java后台接口开发初步实战教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01
  • Java多线程实现聊天客户端和服务器

    Java多线程实现聊天客户端和服务器

    这篇文章主要为大家详细介绍了Java多线程聊天客户端和服务器实现代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • Java+mysql本地图片上传数据库及下载示例

    Java+mysql本地图片上传数据库及下载示例

    本篇文章主要介绍了Java+mysql本地图片上传数据库及下载示例,具有一定的参加价值,有兴趣的可以了解一下。
    2017-01-01
  • java模拟斗地主发牌功能

    java模拟斗地主发牌功能

    这篇文章主要为大家详细介绍了java模拟斗地主发牌,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-10-10
  • Spring Cloud EureKa Ribbon 服务注册发现与调用

    Spring Cloud EureKa Ribbon 服务注册发现与调用

    这篇文章主要介绍了Spring Cloud EureKa Ribbon 服务注册发现与调用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • selenium操作隐藏的元素(python+Java)

    selenium操作隐藏的元素(python+Java)

    这篇文章主要介绍了selenium操作隐藏的元素(python+Java),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07

最新评论