Spring Boot - Reading a Map from properties file - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Saturday, September 29, 2018

Spring Boot - Reading a Map from properties file

You generally encounter to read map from application.properties file in spring boot. You can load with spring annotation @Value.

You may also interested in how to read custom object as value in map from yml file.

P.S Tested with spring boot 2.0.5.RELEASE

Properties file

walking.techie.map={'01':1001,'02':1002,'03':1003,'04':1004,'05':1005}

Project dependencies

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.walking.techie</groupId>
    <artifactId>read-map-from-properties-file</artifactId>
    <version>0.0.1</version>
    <packaging>jar</packaging>

    <name>read-map-from-properties-file</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

ReadPropertiesFile

This is a component, such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.

package com.walking.techie;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class ReadPropertiesFile {
    @Value("#{${walking.techie.map}}")
    private Map<String, Integer> map;

    public Map<String, Integer> getKeyValue() {
        return map;
    }
}

Run Application

package com.walking.techie;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import java.util.Map;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ApplicationContext applicationContext = SpringApplication.run(Application.class, args);
        ReadPropertiesFile propertiesFileBean = applicationContext.getBean(ReadPropertiesFile.class);
        Map<String, Integer> keyValuePair = propertiesFileBean.getKeyValue();
        for (String key : keyValuePair.keySet()) {
            System.out.println("Key:: " + key + " value:: " + keyValuePair.get(key));
        }
    }
}

Output

When you run this standalone application you will see something like below:

2018-09-29 17:10:15.408  INFO 10324 --- [           main] com.walking.techie.Application           : Started Application in 1.672 seconds (JVM running for 2.983)
Key:: 01 value:: 1001
Key:: 02 value:: 1002
Key:: 03 value:: 1003
Key:: 04 value:: 1004
Key:: 05 value:: 1005

find the above working code from git.

No comments :

Post a Comment