前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot读取配置4种方式,建议收藏!

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

作者头像
田维常
发布2023-08-31 11:25:10
2130
发布2023-08-31 11:25:10
举报

你好,我是田

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

Value注解

application.properties配置文件配置项:

代码语言:javascript
复制
name=tian

在java代码中读取:

代码语言:javascript
复制
/**
 * @author tianwc 
 * @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

返回参数:

代码语言:javascript
复制
tian

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

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

ConfigurationProperties注解

application.properties配置文件配置项:

代码语言:javascript
复制
user.userName=tian1
user.age=21

在javadiam中读取:

代码语言:javascript
复制
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author tianwc  
 * @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配置文件配置项:

代码语言:javascript
复制
vip.userList[0].name=tian01
vip.userList[0].age=20
vip.userList[1].name=tian02
vip.userList[1].age=21

定义一个User类:

代码语言:javascript
复制
public class User {
    private String name;
    private Integer age;
    
    //set get toString 省略
}

配置读取类:

代码语言:javascript
复制
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author tianwc 
 * @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配置文件配置项:

代码语言:javascript
复制
realName=tian3

代码实现

代码语言:javascript
复制
@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作为分布式配置中心实战一次,然后,再自己手写一个分布式配置中心。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-07-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Java后端技术全栈 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Value注解
  • ConfigurationProperties注解
  • 也是ConfigurationProperties注解
  • PropertySource注解
相关产品与服务
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档