簡體   English   中英

Java 屬性 - 加載到 Map <string, abstractmap.simpleentry<string, string> &gt;</string,>

[英]Java properties - Load into Map<String, AbstractMap.SimpleEntry<String, String>>

在 Spring 引導項目中,我有一個Map<String, AbstractMap.SimpleEntry<String, String>> ,並像這樣使用它:

testData.put("key1", new AbstractMap.SimpleEntry<>("myKey1", "myValue1");
testData.put("key2", new AbstractMap.SimpleEntry<>("myKey2", "myValue2");
...

我想把數據放到一個Java屬性文件中,更靈活的修改它而不改變代碼,但是如何在屬性文件中構造數據,將map放到Z46F3EA056CAA3126B91F3F70BEEA0上呢?

我會有一個 class 像:

@Component
@PropertySource("classpath:testdata.properties")
@ConfigurationProperties(prefix = "test.data")
public class TestDataProperties {
    private Map<String, AbstractMap.SimpleEntry<String, String>> testData;

    // Getter/Setter
}

testData.properties中構建時,例如:

test.data.testData.key1=myKey1,myValue1

並在需要時使用它:

@Autowired
TestDataProperties testDataProperties;

我得到例外:

org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'test.data.testdata.key1' to java.util.AbstractMap$SimpleEntry<java.lang.String, java.lang.String>

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [java.util.AbstractMap$SimpleEntry<java.lang.String, java.lang.String>]

顯然需要一個轉換器,但它的外觀如何以及如何將其連接到 Map?

這里有幾個問題 -

  • 首先 spring 屬性不允許在屬性文件中使用駝峰式大小寫,讀取包含駝峰式大小寫的屬性文件時會拋出異常。
  • 您無法在自定義轉換器中捕獲屬性鍵, spring 將嘗試在您的屬性 class TestDataProperties中找到匹配的 keyName ,對於第一個鍵key1 ,它將嘗試查找具有 key1 名稱的字段。

現在程序加載 testData.properties 並在解析屬性文件時嘗試查找名稱 key1 但沒有 key1,而是有 testData 屬性,因此 testData 在您的代碼中始終是堅果。

由於 spring 不允許駱駝外殼屬性,因此您可以像這樣更新testData.properties (約定是每次您想使用兩個以上的詞作為屬性名稱時都使用連字符)-

test.data.test-data.key1=myKey1,myValue1

然后您可以通過以下方式更改您的代碼 -

TestDataProperties.java

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.AbstractMap;

@Component
@PropertySource(value = "classpath:testData.properties")
@ConfigurationProperties(prefix = "test.data.test-data")
public class TestDataProperties {

    private AbstractMap.SimpleEntry<String, String> key1;

    TestDataProperties(){

    }

    public AbstractMap.SimpleEntry<String, String> getKey1() {
        return key1;
    }

    public void setKey1(AbstractMap.SimpleEntry<String, String> key1) {
        this.key1 = key1;
    }
}

其次,在解析屬性文件時創建自定義轉換器來處理數據轉換 -

CustomConverter.java

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

import java.util.AbstractMap;

@Component
@ConfigurationPropertiesBinding
public class CustomConverter implements Converter<String, AbstractMap.SimpleEntry<String, String>> {


    @Override
    public AbstractMap.SimpleEntry<String, String> convert(String data) {
        if(data != null && !data.isEmpty()){
            String[] values = data.trim().split(",");
            if(values.length == 2){
                return new AbstractMap.SimpleEntry<>(values[0], values[1]);
            }
        }
        return null;
    }
}

最后,您可以將您的TestDataProperties class 自動連接為

@Autowired
private TestDataProperties testDataProperties; 

然后你可以訪問這樣的屬性 -

testDataProperties.getKey1().getKey();
testDataProperties.getKey1().getValue();

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM