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
Is an Array a Primitive Type or an Object (Or Something Else Entirely)
Collection to Stream to a New Collection
How Can Non-Ascii Characters Be Removed from a String
How to Read PDF Files Using Java
How Do Hashcode() and Identityhashcode() Work at the Back End
How to Catch Out of Memory Exception in Java
How to Use "." as the Delimiter with String.Split() in Java
Java - How Would I Dynamically Add Swing Component to Gui on Click
Why Do I Get Java.Lang.Abstractmethoderror When Trying to Load a Blob in the Db
Connect 4 Check for a Win Algorithm
Putting Char into a Java String for Each N Characters
Struts2: Updating the Values of a "List of Objects" Inside a Map
Behaviour of Final Static Method
Java Equivalent to C# Extension Methods
No Suitable Driver Found for 'Jdbc:Mysql://Localhost:3306/Mysql