无法使用@ConfigurationProperties

时间:2017-06-14 15:06:01

标签: java spring spring-boot

我正在尝试使用Spring yaml配置文件中的复杂密钥将映射解组为使用spring boot和 @ConfigurationProperties 注释的java.util.Map。关于具有简单键的地图有很多例子,比如

map: 
  key: value

甚至是带有简单键和复杂值的地图,例如

map:
  key: {firstPartOfComplexValue: alpha, secondPartOfComplexValue: beta}

我已经测试了上述两个例子 - 效果很好。

现在我需要地图中的复杂键:

map:
  ? {firstPartOfAKey: someValue1, secondPartOfAKey: someValue2}: value

这种解组的结果是一张空地图。 你能不能请我告诉我我做错了什么 提前致谢

有我的代码:

application.yml

custom:
  users:
    ? {firstPartOfAKey: hello, secondPartOfAKey: world} : tom

bean to unmarshall

@Component
@ConfigurationProperties("custom")
  public class MyBean {
    private Map<Key, String> users = new HashMap<>();

    public Map<Key, String> getUsers() {
      return users;
    }

    public void setUsers(Map<Key, String> users) {
      this.users = users;
    }

    @Override
    public String toString() {
      return users.toString();
    }

    public static class Key {
      private String firstPartOfAKey;
      private String secondPartOfAKey;

      @Override
      public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Key key = (Key) o;
        return Objects.equals(firstPartOfAKey, key.firstPartOfAKey) &&
                Objects.equals(secondPartOfAKey, key.secondPartOfAKey);
      }

      @Override
      public int hashCode() {
        return Objects.hash(firstPartOfAKey, secondPartOfAKey);
      }

      public String getFirstPartOfAKey() {
        return firstPartOfAKey;
      }

      public void setFirstPartOfAKey(String firstPartOfAKey) {
        this.firstPartOfAKey = firstPartOfAKey;
      }

      public String getSecondPartOfAKey() {
        return secondPartOfAKey;
      }

      public void setSecondPartOfAKey(String secondPartOfAKey) {
        this.secondPartOfAKey = secondPartOfAKey;
      }

      @Override
      public String toString() {
        return String.format("firsPartOfKey: '%s', secondPartOfKey: '%s'", firstPartOfAKey, secondPartOfAKey);
      }
    }
}

java config (它是空的)

@Configuration
@ComponentScan(basePackages = {"com"})
@EnableAutoConfiguration
@EnableConfigurationProperties
public class Config {
}

单元测试

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Config.class})
public class TestProps {

    @Autowired
    private MyBean myBean;

    @Test
    public void testYamlPropsLoad() {
      System.out.println(myBean);
    }
}

仅针对具有复杂键的地图打印“{}”。其他地图(使用简单的键)效果很好。

1 个答案:

答案 0 :(得分:1)

It seems the binding of your MyBean is not processed at all if your test is executed without any exception, usually there should be an exception like:

Binding to target {} failed:

Property: custom.null Value: {{firstPartOfAKey=hello, secondPartOfAKey=world}=tom} Reason: Failed to convert property value of type 'java.lang.String' to required type 'com.example.MyBean$Key' for property 'null'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.example.MyBean$Key': no matching editors or conversion strategy found

Obviously the parser is not able to process complex keys and is interpreting the key null and value {firstPartOfAKey=hello, secondPartOfAKey=world}=tom.

Maybe you can find another way to process your config by implementing a custom editor / converter.