ip696
ip696

Reputation: 7084

I can not inject Map<String, String> from YAML file

I have this properties in my YAML file:

request-topic:
  topics:
    IMPORT_CHARGES: topic-name-1
    IMPORT_PAYMENTS: topic-name-2
    IMPORT_CATALOGS: topic-name-3

And this class:

@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "topic-properties")
public class TopicProperties {
    private Map<String, String> topics = new HashMap<>();

    public String getTopicNameByType(String type){
        return topics.get(type);
    }
}

But when I autowire this properies I get empty Map:

@Service
public class TopicRouterImpl implements TopicRouter {

    private final TopicProperties topics;

    public TopicRouterImpl(TopicProperties topics) {
        this.topics = topics;
    }

    @PostConstruct
    public void init(){
        topics.getTopicNameByType("IMPORT_CHARGES");
    }

    @Override
    public String getTopicName(MessageType messageType) {
        return topics.getTopicNameByType(messageType.name());
    }
}

Upvotes: 0

Views: 229

Answers (1)

Abdelghani Roussi
Abdelghani Roussi

Reputation: 2817

This is due to the name mismatch in your yaml file it should be equals to the specified prefix : topic-properties. Like this :

topic-properties:
  topics:
    IMPORT_CHARGES: topic-name-1
    IMPORT_PAYMENTS: topic-name-2
    IMPORT_CATALOGS: topic-name-3

Upvotes: 1

Related Questions