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
How to Determine If a List of String Contains Null or Empty Elements
How to Make the Icon Background of an Android App Transparent
Java.Lang.Illegalargumentexception: Not a Managed Type: Class
On Selenium Webdriver How to Get Text from Span Tag
How to Query Multiple Hash Keys in Dynamodb
How to Open a .Dat File in Java Program
Java - Check If Jtextfield Is Empty or Not
How to Do Sorting in Array List Without Using Collections in Java
Eclipse: the Resource Is Not on the Build Path of a Java Project
Pass Multiple Parameters to Rest API - Spring
Get Unique Values from Arraylist in Java
How to Test Code Dependent on Environment Variables Using Junit
Spring Data JPA and Parameters That Can Be Null
Javax.Validation - Allow Null But Validate If the Value Is Not
Selecting from Div Class Dropdown - Selenium