How to Fill Hashmap from Java Property File With Spring @Value

How to fill HashMap from java property file with Spring @Value

I make one solution inspired by the previous post.

Register property file in the Spring configuration:

<util:properties id="myProp" location="classpath:my.properties"/>

And I create component:

@Component("PropertyMapper")
public class PropertyMapper {

@Autowired
ApplicationContext applicationContext;

public HashMap<String, Object> startWith(String qualifier, String startWith) {
return startWith(qualifier, startWith, false);
}

public HashMap<String, Object> startWith(String qualifier, String startWith, boolean removeStartWith) {
HashMap<String, Object> result = new HashMap<String, Object>();

Object obj = applicationContext.getBean(qualifier);
if (obj instanceof Properties) {
Properties mobileProperties = (Properties)obj;

if (mobileProperties != null) {
for (Entry<Object, Object> e : mobileProperties.entrySet()) {
Object oKey = e.getKey();
if (oKey instanceof String) {
String key = (String)oKey;
if (((String) oKey).startsWith(startWith)) {
if (removeStartWith)
key = key.substring(startWith.length());
result.put(key, e.getValue());
}
}
}
}
}

return result;
}
}

And when I want to map all properties that begin with specifix value to HashMap, with @Value annotation:

@Service
public class MyServiceImpl implements MyService {

@Value("#{PropertyMapper.startWith('myProp', 'service.expiration.', true)}")
private HashMap<String, Object> portalExpirations;

Reading a Map from properties file and load with spring annotation @Value

application.properties:

property.map={first:value, second:value}

then in Java code you can:

@Value("#{${property.map}}")
Map<String, String> map;

How to get spring boot property file value as Map<String,String>

I made a mistake by adding the @ConfigurationProperties(prefix="specialist"). No need of prefix="specialist". Because the map property value i have added as specialist, so there is no need of specialist in the ConfigurationProperties prefix. After the change it was worked good.

@Configuration
@ConfigurationProperties
@PropertySource("classpath:specialist.properties")
public class SpecialistProperties {


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

public Map<String, String> getProp() {
return prop;
}

public void setProp(Map<String, String> prop) {
this.prop = prop;
}
}

Spring Boot - inject map from properties file

NullPointerException is probably from empty ApplicationProperties.

All custom properties should be annotated @ConfigurationProperties(prefix="custom").
After that, on your main class (class with main method) you must add @EnableConfigurationProperties(CustomProperties.class).
For autocomplete you can use:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

If you use @ConfigurationProperties without prefix you use only field name. Field name in you properites. In your case path-mapper, next you specific key and value. Example:

path-mapper.key=value

Remeber after changes in your own properites you need to reload application. Example:

https://github.com/kchrusciel/SpringPropertiesExample

How to fill map, configured with spring, from properties file

You're in luck, because the Properties class implements Map!
Just define the properties bean like so:

<util:properties id="myProps" location="classpath:myProps.properties" />

(Don't forget to import the Spring util namespace)

how to load map from properties file with spring

There is no default converter for that. So you have to write your own converter and register it to the conversationService.

Here an annotation-based example:

@Bean(name="conversionService") 
public static ConversionService getConversionService(){
ConversionServiceFactoryBean conversionServiceFactoryBean = new ConversionServiceFactoryBean();
Set<Converter<?,?>> converters = new HashSet<Converter<?,?>>();
converters.add(new StringToHashMapConverter());
conversionServiceFactoryBean.setConverters(converters);
conversionServiceFactoryBean.afterPropertiesSet();
return conversionServiceFactoryBean.getObject();
}

The Converter:

import java.util.HashMap;

import org.springframework.core.convert.converter.Converter;

public class StringToHashMapConverter implements Converter<String,HashMap> {

@Override
public HashMap convert(String paramS) {
//do the coversion
}

}


Related Topics



Leave a reply



Submit