使用 Spring Boot 一次性读取多个属性文件? [英] Read multiple properties file in one go using Spring Boot?

查看:47
本文介绍了使用 Spring Boot 一次性读取多个属性文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览了链接:如何传递地图<字符串,字符串>多次使用 application.properties 和其他相关链接,但仍然无法正常工作.

I went through the link: How to pass a Map<String, String> with application.properties and other related links multiple times, but still its not working.

我正在使用 Spring Boot 和 Spring REST 示例.链接问题:如何在 Spring Boot REST 中默认执行最新版本的端点?.

I'm using Spring Boot and Spring REST example. Link Question: How to by default execute the latest version of endpoint in Spring Boot REST?.

我已经创建了这样的映射,只需阅读映射

I've created mapping something like this and simply read the mapping

get.customers={GET: '/app-data/customers', VERSION: 'v1'}
post.customers={POST: '/app-data/customers', VERSION: 'v1'}
get.customers.custId={GET: '/app-data/customers/{custId}', VERSION: 'v2'}

代码:

private String resolveLastVersion() {
   // read from configuration or something
    return "2";
}

代码:

@Component
@ConfigurationProperties
@PropertySource("classpath:restendpoint.properties")
public class PriorityProcessor {

    private final Map<String, String> priorityMap = new HashMap<>();

    public Map<String, String> getPriority() {
        return priorityMap;
    }
}

代码:

推荐答案

我建议如下实现:

@ConfigurationProperties(prefix="request")
public class ConfigurationProps {
    private List<Mapping> mapping;

    public List<Mapping> getMapping() {
        return mapping;
    }

    public void setMapping(List<Mapping> mapping) {
        this.mapping = mapping;
    }
}

类映射将表示关于单个映射的信息:

Class Mapping will denote the information about the single mapping:

public class Mapping {
    private String method;
    private String url;
    private String version;

    public Mapping(String method, String url, String version) {
        this.method = method;
        this.url = url;
        this.version = version;
    }

    public Mapping() {
    }

    // getters setters here
}

在 Configuration 或 spring boot 应用程序类上(有 main 方法的那个):@EnableConfigurationProperties(ConfigurationProps.class)

On the Configuration or spring boot application class (the one with main method): @EnableConfigurationProperties(ConfigurationProps.class)

在属性文件中输入:

request.mapping[0].method=get
request.mapping[0].url=/customers
request.mapping[0].version=1

request.mapping[1].method=post
request.mapping[1].url=/students
request.mapping[1].version=2

在过滤器中(我假设您遵循了链接问题中的建议):

In Filter (I assume you followed my suggestion from the linked question):

    @Component
    @Order(1)
    public class LatestVersionFilter implements Filter {

       private List<Mapping> mappings;

       public LatestVersionFilter(ConfigurationProps props) {
          this.mappings = props.getMapping();
       }
    }

这篇关于使用 Spring Boot 一次性读取多个属性文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆