简体   繁体   English

Micronaut:我如何 map HashMap 中的所有属性值?

[英]Micronaut: How do I map all the properties values in HashMap?

I have been learning micronaut for sake of learning and for a small scale project and I have been stuck on a problem.为了学习和一个小规模的项目,我一直在学习 micronaut,但我一直被困在一个问题上。

So assume I have this data in application.yml file所以假设我在application.yml文件中有这些数据

output:
    -file:
        name: PDF
        size: 50000
    -file:
        name: DOCX
        size: 35000

So my goal was to map this exact data in HashMap which I can use further.所以我的目标是 map 这个确切的数据在 HashMap 中,我可以进一步使用。 I want my code to have all the data independent of any condition.我希望我的代码拥有独立于任何条件的所有数据。 If I add another file type it should automatically map data for that too.如果我添加另一种文件类型,它也应该自动 map 数据。 So in a nutshell at the end I want to have a Map<name, path> with all the values available in yaml.所以简而言之,最后我想要一个包含 yaml 中所有可用值的Map<name, path> I tried with EachProperty.我尝试使用 EachProperty。

https://guides.micronaut.io/micronaut-configuration/guide/index.html#eachProperty but I'd have to pass 'name' as argument. https://guides.micronaut.io/micronaut-configuration/guide/index.html#eachProperty但我必须将“名称”作为参数传递。

Any help is much appreciated.任何帮助深表感谢。

The micronaut @EachProperty allows to drive Bean (s) creation out of application properties, thus it will create beans ( Singleton POJOs) with properties derived from the nested configuration key / values. micronaut @EachProperty允许从应用程序属性中创建Bean ,因此它将创建具有从嵌套配置键/值派生的属性的 bean ( Singleton POJO)。

An important note to take into consideration when using @EachProperty configuration is that it only binds to a top level configuration key ie and that the created beans will be named after the nested top higher property which then should be unique.使用@EachProperty配置时要考虑的一个重要注意事项它只绑定到顶级配置键,即创建的bean 将以嵌套的top Higher 属性命名,该属性应该是唯一的。

Your configuration then won't work unless changed to the following:除非更改为以下内容,否则您的配置将不起作用:

output:
  # Below config will drive a bean named "pdf", more details below
  pdf:
    size: 50000
  # Below config will drive a bean named "docx"
  docx:
    size: 35000

Note that in above configuration, the name attribute is omitted as it can be derived from the bean configured name itself:请注意,在上面的配置中, name属性被省略,因为它可以从 bean 配置的名称本身派生:

@EachProperty(value = "output")
public class FileType {

    private String name;

    private int size;

    public FileType(@Parameter("name") String name) {
        this.name = name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getSize() {
        return size;
    }

    @Override
    public String toString() {
        return "FileType{" +
                "name='" + name + '\'' +
                ", size=" + size +
                '}';
    }
}

The FileType constructor will have the configured bean name (derived from the configuration key) injected and the above type will produce two bean at application runtime: FileType构造函数将注入配置的 bean 名称(从配置键派生),上述类型将在应用程序运行时生成两个 bean:

FileType{name='pdf', size=50000} FileType{name='docx', size=35000} FileType{name='pdf', size=50000} FileType{name='docx', size=35000}

As those beans are already handled by the micronaut core container, you can have them injected into any other bean.由于这些 bean 已经由micronaut核心容器处理,您可以将它们注入任何其他 bean。

Otherwise, if you wish to have your beans mapped to a particular configuration format such as a Map [NAME -> SIZE] , you can:否则,如果您希望将 bean 映射到特定的配置格式,例如Map [NAME -> SIZE] ,您可以:

  1. Create another Singleton bean as a configuration wrapper创建另一个Singleton bean 作为配置包装器
  2. Inject the FileType beans注入FileType bean
  3. Map the injected FileType beans to your custom format Map 将注入的FileType bean 转换为您的自定义格式
  4. Make this custom format accessible your configuration wrapper使这种自定义格式可访问您的配置包装器

Here down a sample configuration wrapper:下面是一个示例配置包装器:

@Singleton
public class FileTypeConfiguration {

    private final Map<String, Integer> fileTypes;

    @Inject
    public FileTypeConfiguration(List<FileType> fileTypes) {
        this.fileTypes = fileTypes.stream()
                .collect(
                        Collectors.toMap(FileType::getName, FileType::getSize)
                );
    }

    public Map<String, Integer> getFileTypes() {
        return fileTypes;
    }
}

where you have your configuration accessed through FileTypeConfiguration#getFileTypes ( FileTypeConfiguration must be @Inject ed or accessed through ApplicationContext ).您可以通过FileTypeConfiguration#getFileTypes访问您的配置( FileTypeConfiguration必须是@Inject或通过ApplicationContext访问)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM