使用 Spring Boot 一次读取多个属性文件?

Read multiple properties file in one go using Spring Boot?

我多次查看 link: How to pass a Map<String, String> with application.properties 和其他相关的 link,但仍然没有用。

我正在使用 Spring Boot 和 Spring REST 示例。 Link 问题:

我创建了类似这样的映射并简单地阅读了映射

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 表示单个映射的信息:

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
}

在配置或 spring 引导应用程序上 class(具有 main 方法的那个): @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

在过滤器中(我假设你听从了我在链接问题中的建议):

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

       private List<Mapping> mappings;

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