Reputation: 6079
How can I inject values into a Map from the properties file using the @Value annotation in Spring?
My Spring Java class is and I tried using the $, but got the following error message:
Could not autowire field: private java.util.Map Test.standard; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'com.test.standard' in string value "${com.test.standard}"
@ConfigurationProperty("com.hello.foo")
public class Test {
@Value("${com.test.standard}")
private Map<String,Pattern> standard = new LinkedHashMap<String,Pattern>
private String enabled;
}
I have the following properties in a .properties file
com.test.standard.name1=Pattern1
com.test.standard.name2=Pattern2
com.test.standard.name3=Pattern3
com.hello.foo.enabled=true
Upvotes: 116
Views: 211851
Reputation: 26
Please pay attention, the property in the properties file is weak type. As Spring parses it, the cast type might be wrong.
Here is an example:
The property in the properties file.
delayedSeconds={default:300,004:900}
In your code:
@Value("#{${delayedSeconds}}")
private Map<String, Long> allDelayedSeconds;
Output the parsed config:
allDelayedSeconds={default=300, 4=900}
You need add quotation to the keys.
delayedSeconds={"default":300,"004":900}
Upvotes: 0
Reputation: 3051
To get this working with YAML, do this:
property-name: '{
key1: "value1",
key2: "value2"
}'
Upvotes: 23
Reputation: 3009
You can use below code.
Below code for application.yml
my:
mapValues:
dbData: '{
"connectionURL": "http://tesst:3306",
"userName": "myUser",
"password": "password123"
}'
to access these key and values using @Value
annotation use below java code.
@Value("#{${my.mapValues.dbData}}")
private Map<String,String> dbValues;
Upvotes: 5
Reputation: 181
Following worked for me:
SpingBoot 2.1.7.RELEASE
YAML Property (Notice value sourrounded by single quotes)
property:
name: '{"key1": false, "key2": false, "key3": true}'
In Java/Kotlin annotate field with (Notice use of #) (For java no need to escape '$' with '\')
@Value("#{\${property.name}}")
Upvotes: 18
Reputation: 241
I had a simple code for Spring Cloud Config
like this:
In application.properties
spring.data.mongodb.db1=mongodb://[email protected]
spring.data.mongodb.db2=mongodb://[email protected]
@Bean(name = "mongoConfig")
@ConfigurationProperties(prefix = "spring.data.mongodb")
public Map<String, Map<String, String>> mongoConfig() {
return new HashMap();
}
@Autowired
@Qualifier(value = "mongoConfig")
private Map<String, String> mongoConfig;
@Bean(name = "mongoTemplates")
public HashMap<String, MongoTemplate> mongoTemplateMap() throws UnknownHostException {
HashMap<String, MongoTemplate> mongoTemplates = new HashMap<>();
for (Map.Entry<String, String>> entry : mongoConfig.entrySet()) {
String k = entry.getKey();
String v = entry.getValue();
MongoTemplate template = new MongoTemplate(new SimpleMongoDbFactory(new MongoClientURI(v)));
mongoTemplates.put(k, template);
}
return mongoTemplates;
}
Upvotes: 21
Reputation: 420
Here is how we did it. Two sample classes as follow:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
@EnableKafka
@Configuration
@EnableConfigurationProperties(KafkaConsumerProperties.class)
public class KafkaContainerConfig {
@Autowired
protected KafkaConsumerProperties kafkaConsumerProperties;
@Bean
public ConsumerFactory<String, String> consumerFactory() {
return new DefaultKafkaConsumerFactory<>(kafkaConsumerProperties.getKafkaConsumerConfig());
}
...
@Configuration
@ConfigurationProperties
public class KafkaConsumerProperties {
protected Map<String, Object> kafkaConsumerConfig = new HashMap<>();
@ConfigurationProperties("kafkaConsumerConfig")
public Map<String, Object> getKafkaConsumerConfig() {
return (kafkaConsumerConfig);
}
...
To provide the kafkaConsumer config from a properties file, you can use: mapname[key]=value
//application.properties
kafkaConsumerConfig[bootstrap.servers]=localhost:9092, localhost:9093, localhost:9094
kafkaConsumerConfig[group.id]=test-consumer-group-local
kafkaConsumerConfig[value.deserializer]=org.apache.kafka.common.serialization.StringDeserializer
kafkaConsumerConfig[key.deserializer=org.apache.kafka.common.serialization.StringDeserializer
To provide the kafkaConsumer config from a yaml file, you can use "[key]": value In application.yml file:
kafkaConsumerConfig:
"[bootstrap.servers]": localhost:9092, localhost:9093, localhost:9094
"[group.id]": test-consumer-group-local
"[value.deserializer]": org.apache.kafka.common.serialization.StringDeserializer
"[key.deserializer]": org.apache.kafka.common.serialization.StringDeserializer
Upvotes: 7
Reputation: 2031
You can inject values into a Map from the properties file using the @Value
annotation like this.
The property in the properties file.
propertyname={key1:'value1',key2:'value2',....}
In your code.
@Value("#{${propertyname}}") private Map<String,String> propertyname;
Note the hashtag as part of the annotation.
Upvotes: 203
Reputation: 24591
I believe Spring Boot supports loading properties maps out of the box with @ConfigurationProperties annotation.
According that docs you can load properties:
my.servers[0]=dev.bar.com
my.servers[1]=foo.bar.com
into bean like this:
@ConfigurationProperties(prefix="my")
public class Config {
private List<String> servers = new ArrayList<String>();
public List<String> getServers() {
return this.servers;
}
}
I used @ConfigurationProperties feature before, but without loading into map. You need to use @EnableConfigurationProperties annotation to enable this feature.
Cool stuff about this feature is that you can validate your properties.
Upvotes: 25
Reputation: 29316
You can inject .properties
as a map in your class using @Resource
annotation.
If you are working with XML based configuration
, then add below bean in your spring configuration file:
<bean id="myProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:your.properties"/>
</bean>
For, Annotation based:
@Bean(name = "myProperties")
public static PropertiesFactoryBean mapper() {
PropertiesFactoryBean bean = new PropertiesFactoryBean();
bean.setLocation(new ClassPathResource(
"your.properties"));
return bean;
}
Then you can pick them up in your application as a Map:
@Resource(name = "myProperties")
private Map<String, String> myProperties;
Upvotes: 21