简体   繁体   English

Spring 引导读取属性到 Map

[英]Spring boot read properties into Map

I am trying to create a Map<String, Product> from my.properties file in Spring boot 3.1.我正在尝试从 Spring boot 3.1 中的 my.properties 文件创建Map<String, Product> I have written my custom converter for this, but it seems that it is never executing.我为此编写了我的自定义转换器,但它似乎永远不会执行。 I haven't done custom conversions before and I am not sure if a converter is needed in this use case or if Spring boot is able to manage the conversion automagically (If I configure it correctly).我之前没有做过自定义转换,我不确定在这个用例中是否需要转换器,或者 Spring 引导是否能够自动管理转换(如果我配置正确的话)。

product-configurations.properties:产品配置.properties:

products.product[0].name=FirstProduct
products.product[0].productID=1234
products.product[0].availableOptions=Opt1
products.product[0].processing=Parallel

Product class:产品 class:

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Product {
    private String name;
    private String processing;
    private String availableOptions;
    private Integer productID;
}

Configuration class:配置 class:

import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import lombok.Getter;
import lombok.Setter;

@Configuration
@PropertySource("classpath:product-configurations.properties")
@ConfigurationProperties("products")
@Getter
@Setter
public class ProductProperties {

    private List<Product> product;
}

ProductConverter class:产品转换器class:

import java.util.List;
import java.util.Map;

import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import com.cb.clientfacingapp.model.Product;

@Component

@ConfigurationPropertiesBinding
public class ProductConverter implements Converter<List<Product>, Map<String, Product>> {

    @Override
    public Map<String, Product> convert(List<Product> source) {
        Product p = new Product();
        System.out.println("I am converting");
        return null;
    }

}

Can someone pls guide me on this?有人可以指导我吗?

Thanks in Advance.提前致谢。

Update:更新:

Sorry, I forgot to mention how the map should be organized.对不起,我忘了提到 map 应该如何组织。

Map<String, Product> where String is basically the name property of the product, and Product is the POJO class for that specific product, So each Product is mapped to its Name, basically. Map<String, Product>其中 String 基本上是产品的name属性,而Product是该特定产品的 POJO class,因此每个产品基本上都映射到其名称。

Seems like what you're trying to achieve can be done without conversion似乎您想要实现的目标无需转换即可完成

root.products.productName1.name=FirstProduct
root.products.productName1.productID=1234
root.products.productName1.availableOptions=Opt1
root.products.productName1.processing=Parallel

root.products.productName2.name=SecondProduct
root.products.productName2.productID=5678
root.products.productName2.availableOptions=Opt2
root.products.productName2.processing=Parallel

Therefore productName1 , productName2 would be your map key names.因此productName1 , productName2将是您的 map 密钥名称。

You can use it this way:你可以这样使用它:

@ConfigurationProperties("root")
@Getter
@Setter
public class ProductProperties {

    private Map<String, Product> products;
}

You could do what @Daria has suggested , which is the simplier way to do it.您可以按照@Daria的建议进行操作,这是更简单的方法。

In case you're still wondering how to convert a list into a Map, it can be done using Streams:如果您仍然想知道如何将列表转换为 Map,可以使用 Streams 完成:

import java.util.List;
import java.util.Map;

import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import com.cb.clientfacingapp.model.Product;

@Component
@ConfigurationPropertiesBinding
public class ProductConverter implements Converter<List<Product>, Map<String, Product>> {

    @Override
    public Map<String, Product> convert(List<Product> source) {
        return source.stream().collect(
            Collectors.toMap(Product::getName,Function.identity));

    }
}

It would generate a map with the product name as key value, and the product object as content value.它将生成一个 map,其中产品名称作为键值,产品 object 作为内容值。

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

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