打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Spring Boot读取配置4种方式,建议收藏!
userphoto

2023.07.02 广东

关注

你好,我是田哥

在Spring Boot项目中基本上都会涉及到读取配置文件内容,本文就来聊聊常见的读取配置文件的几种方式。

Value注解

application.properties配置文件配置项:

name=tian

在java代码中读取:

/**
 * @author tianwc  公众号:java后端技术全栈、面试专栏
 * @version 1.0.0
 * @date 2023年07月02日 21:00
 * 博客地址:<a href="http://woaijava.cc/">博客地址</a>
 */

@RestController
@RequestMapping("/configuration")
public class ConfigurationController {

    @Value("${name}")
    private String name;

    @GetMapping("/index")
    public String test() {
        return name;
    }
}

验证:

GET

http://localhost:8089/configuration/index

返回参数:

tian

这类通常都是没有前缀,比较单一的配置项会采用这么读取。

如果有同一的前缀配置,那么我们可以使用下面这种方法。

ConfigurationProperties注解

application.properties配置文件配置项:

user.userName=tian1
user.age=21

在javadiam中读取:

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

/**
 * @author tianwc  公众号:java后端技术全栈、面试专栏
 * @version 1.0.0
 * @date 2023年07月02日 21:07
 * 博客地址:<a href="http://woaijava.cc/">博客地址</a>
 */

@Component
@ConfigurationProperties(prefix = "user")
public class PreConfiguration {
    private String userName;
    private Integer age;

    //set get 省略
}

验证:

GET

http://localhost:8089/preconfiguration/index

我们通常是把一类的配置项设置为前缀,统一处理,面向对象化。

但,如果是读取多个的如何处理(数组类型)?

我们可以采用下面这种方式。

也是ConfigurationProperties注解

application.properties配置文件配置项:

vip.userList[0].name=tian01
vip.userList[0].age=20
vip.userList[1].name=tian02
vip.userList[1].age=21

定义一个User类:

public class User {
    private String name;
    private Integer age;
    
    //set get toString 省略
}

配置读取类:

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

import java.util.List;

/**
 * @author tianwc  公众号:java后端技术全栈、面试专栏
 * @version 1.0.0
 * @date 2023年07月02日 21:24
 * 博客地址:<a href="http://woaijava.cc/">博客地址</a>
 */

@Component
@ConfigurationProperties(prefix = "vip")
public class UserListConfiguration {
    private List<User> userList;
    // set get 省略
}

如果我们自定义一个custom.properties配置文件,那又该如何读取呢?

PropertySource注解

application.properties配置文件配置项:

realName=tian3

代码实现

@RestController
@RequestMapping("/propertySource")
@PropertySource("classpath:custom.properties")
public class PropertySourceController {

    @Value("${realName}")
    private String realName;
    @GetMapping("/index")
    public String test() {
        return realName;
    }
}

验证:

GET

http://localhost:8089/propertySource/index

返回参数:tian3

那么,问题又来了,我们可能会因为一个配置项需要改,或者是上线后发现配置项不对,这时候就需要重启服务了,为了避免这样重启服务,我们可以引入分布式配置中心

分布式配置中心有很多实现方案,比如Nacos、Zookeeper、Qconf、disconf、Apache Commons Configuration、Spring Cloud Config等。

下期使用Nacos作为分布式配置中心实战一次,然后,再自己手写一个分布式配置中心。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Spring Boot 最核心的 25 个注解
spring-boot 配置从使用到原理
SpringBoot-配置文件
Spring Boot第二弹,配置文件怎么造?
Spring Boot读取配置文件的几种方式
@configurationProperties注解时 idea弹出 Spring Boot Annotion processor not found in classpath
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服