Spring Boot @ConfigurationProperties - 更改属性键 [英] Spring Boot @ConfigurationProperties - Change property key

查看:73
本文介绍了Spring Boot @ConfigurationProperties - 更改属性键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有趣的用例,在用 @ConfigurationProperties 注释的类中使用的字段名称应该与 (yaml) 配置文件中使用的相应键不同:

I have an interesting use case where the field name that is used in a class annotated with @ConfigurationProperties should be different from the corresponding key used in the (yaml) configuration file:

@ConfigurationProperties("foo")
class ConfProps {

    private List<SomePojo> bar = new ArrayList<>();

    // getter, setter

}

这将寻找"foo.bar.是否可以将字段 bar 映射到不同的属性键?

This will "look out for" foo.bar. Is it possible to map the field bar to a different property key?

我阅读了文档和一些相关文章,但什么都没有......

I read the the docs and some related articles, but nothing ...

在我看来,要么是因为它绝对是微不足道的,要么是某种非目标.

To me it seems that either it's because it is absolutely trivial or this is some kind of non-goal.

提前致谢!

推荐答案

你不能有不同的配置键和映射属性名称.这就是 spring 解决自动映射的方式.

Well you can't have different config key and mapping property name. That's how spring resolves the auto-mapping.

但是,如果拥有不同的属性字段对您来说非常重要,那么您可以进行破解.

However, if having a different property field is so essential for you have can have a hack.

像这样放置一个虚拟二传手.

Put a dummy setter like this.

属性键:foo.bar

配置类:

@ConfigurationProperties("foo")
class ConfProps {

    private List<SomePojo> differentlyNamedList = new ArrayList<>();

    // getter, setter

    public void setBar(List<SomePojo> bar){
       this.differentlyNamedList = bar;
    }
}

这篇关于Spring Boot @ConfigurationProperties - 更改属性键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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