使用ConfigurationProperties以通用方式填充Map [英] Using ConfigurationProperties to fill Map in generic way

查看:1862
本文介绍了使用ConfigurationProperties以通用方式填充Map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,是否有一种通用的方法可以用您只知道前缀的属性来填充地图.

I'm wondering, if there is a generic way to fill a map with properties you just know the prefix.

假设有很多类似的属性

namespace.prop1=value1
namespace.prop2=value2
namespace.iDontKnowThisNameAtCompileTime=anothervalue

我想有一种通用的方法来填充地图中的此属性,例如

I'd like to have a generic way to fill this property inside a map, something like

@Component
@ConfigurationProperties("namespace")
public class MyGenericProps {
    private Map<String, String> propmap = new HashMap<String, String>();

    // setter and getter for propmap omitted

    public Set<String> returnAllKeys() {
        return propmap.keySet();
    }
}

还是有另一种便捷的方法来收集具有特定前缀的所有属性,而不是遍历环境中的所有PropertySource?

Or is there another convenient way to collect all properties with a certain prefix, instead of iterating over all PropertySources in the environment?

谢谢 汉斯约格

推荐答案

只要您很高兴将每个属性(而不只是仅是您不知道的属性)添加到地图中,就可以做到这一点@ConfigurationProperties.如果要获取namespace下的所有内容,则需要使用空前缀并为名为namespace的地图提供吸气剂:

As long as you're happy having every property added into the map, rather than just those that you don't know in advance, you can do this with @ConfigurationProperties. If you want to grab everything that's beneath namespace then you need to use an empty prefix and provide a getter for a map named namespace:

@ConfigurationProperties("")
public class CustomProperties {

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

    public Map<String, String> getNamespace() {
        return namespace;
    }

}

Spring Boot使用getNamespace方法检索地图,以便它可以向其中添加属性.具有以下属性:

Spring Boot uses the getNamespace method to retrieve the map so that it can add the properties to it. With these properties:

namespace.a=alpha
namespace.b=bravo
namespace.c=charlie

namespace映射将包含三个条目:

The namespace map will contain three entries:

{a=alpha, b=bravo, c=charlie}

如果属性嵌套得更深,例如:

If the properties were nested more deeply, for example:

namespace.foo.bar.a=alpha
namespace.foo.bar.b=bravo
namespace.foo.bar.c=charlie

然后您将使用namespace.foo作为前缀,并将CustomProperties上的namespacegetNamespace分别重命名为bargetBar.

Then you'd use namespace.foo as the prefix and rename namespace and getNamespace on CustomProperties to bar and getBar respectively.

请注意,应将@EnableConfigurationProperties应用于配置,以启用对@ConfigurationProperties的支持.然后,您可以使用该批注引用要处理的任何bean,而不是为它们提供@Bean方法,或者使用@Component通过组件扫描发现它们:

Note that you should apply @EnableConfigurationProperties to your configuration to enable support for @ConfigurationProperties. You can then reference any beans that you want to be processed using that annotation, rather than providing an @Bean method for them, or using @Component to have them discovered by component scanning:

@SpringBootApplication
@EnableConfigurationProperties(CustomProperties.class)
public class YourApplication {
    // …
}

这篇关于使用ConfigurationProperties以通用方式填充Map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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