Spring Boot - Inject Map from Application.Yml

Spring Boot - inject map from application.yml

You can have a map injected using @ConfigurationProperties:

import java.util.HashMap;
import java.util.Map;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@EnableConfigurationProperties
public class MapBindingSample {

public static void main(String[] args) throws Exception {
System.out.println(SpringApplication.run(MapBindingSample.class, args)
.getBean(Test.class).getInfo());
}

@Bean
@ConfigurationProperties
public Test test() {
return new Test();
}

public static class Test {

private Map<String, Object> info = new HashMap<String, Object>();

public Map<String, Object> getInfo() {
return this.info;
}
}
}

Running this with the yaml in the question produces:

{build={artifact=${project.artifactId}, version=${project.version}, name=${project.name}, description=${project.description}}}

There are various options for setting a prefix, controlling how missing properties are handled, etc. See the javadoc for more information.

Spring Boot inject List of Maps from application.yml

This was a "new" for me. I got this working by making cacheConfigs one level deeper and the used the new top level name as the @ConfigurationProperties param. Like this:

cache-configs-map:
cacheConfigs:
- cacheOne:
test: test1
- cacheTwo:
test: test2
- cacheThree:
test: test3
- cacheFor:
test: test4

Now, your configuration class looks like this:

@Configuration
public class Config{
@NoArgsConstructor @AllArgsConstructor( staticName = "of" )
@Getter @Setter
public static class C{
private List<Map<String, String>> cacheConfigs;
}

@Bean
@ConfigurationProperties(prefix = "cache-configs-map")
public C getC() {
return new C();
}
}

How to inject a map from application.yml into a field in Spring Boot?

the only way I know to do so, is to define the value part in JSON format:

app.collection.map.string.to.integer={one:"1", two:"2", three:"3"}

then inject with

@Value("#{${app.collection.map.string.to.integer}}")
private Map<String, Integer> mapStringToInteger;

from https://relentlesscoding.com/2018/09/09/spring-basics-dynamically-inject-values-with-springs-value/ in section Inject Maps With Spring’s @Value

Spring Boot - Injecting a map from a YAML file

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

Spring Boot Kotlin - Injecting a map from a YAML file

You are missing the @ContructorBinding annotation (required as of Spring Boot 2.2.0). Please see this answer:


@ConstructorBinding
@ConfigurationProperties("")
data class PropertyConfig(
val edit: Map<String,String>
)

If you wanna use a non-standard yml file (not called application.yml or derivate), like in the example you provided, then you need to add also the @PropertySource annotation to your Configuration data class.


@ConstructorBinding
@ConfigurationProperties("")
@PropertySource(value = "classpath:test.yml")
data class PropertyConfig(
val edit: Map<String,String>
)


Related Topics



Leave a reply



Submit