Using ConfigurationProperties to fill Map in generic way

前端 未结 4 1072
礼貌的吻别
礼貌的吻别 2020-12-13 08:49

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

Assuming there are a bunch of properties like

names         


        
4条回答
  •  囚心锁ツ
    2020-12-13 09:14

    I was going nuts trying to understand why @Andy's answer wasn't working for me (as in, the Map was remaining empty) just to realize that I had Lombok's @Builder annotation getting in the way, which added a non-empty constructor. I'm adding this answer to emphasize that in order for @ConfigurationProperties to work on Map, the value type must have a No-Arguments constructor. This is also mentioned in Spring's documentation:

    Such arrangement relies on a default empty constructor and getters and setters are usually mandatory ...

    I hope this will save someone else some time.

提交回复
热议问题