简体   繁体   中英

Spring Boot - Injecting a map from a YAML file

I tried several versions but none work. What's the correct way to have this injected as a map:

application.yml

alias:
  name: title
  desc: description
  content: body

I tried using @Value annotation:

NamingService.kt

@Value("\${alias}")
private var alias: Map<String, String> = emptyMap()

I get:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'alias' in value "${alias}"

kotlinVersion = '1.2.31'; springBootVersion = '2.0.1.RELEASE'

We have something similar in our code. This is how we solved it.

application.yml

validation:
  synonyms:
    Doctor: Dr.
    Sanct: St.

Config

@Component
@ConfigurationProperties("validation")
public class ValidationConfig {

    private Map<String, String> synonyms;
    // ...
}

You can find more information for this topic in the documentation: Spring Boot Externalized Configuration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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