Monday, 10 August 2020

Spring Boot @ConfigurationProperties and Properties File

 In this tutorial, you will learn to use @ConfigurationProperties to map properties files to POJO classes in Spring Boot application. Let’s get started to build a simple example with Java and Maven

What you’ll need

    Your favorite IDE

    JDK 8+ or OpenJDK 8+

    Maven 3+

    Stack

    Java

    Spring Boot


Init project structure and dependencies

Project structure

├── src

│   └── main

│       ├── java

│       │   └── com

│       │       └── answerindepth

│       │           └── util

│       │               ├── Application.java

│       │               ├── NestedProperties.java

│       │               └── SimpleProperties.java

│       └── resources

│           └── application.properties

└── pom.xml



pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         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.answerindepth.springboot</groupId>
    <artifactId>springboot-nested-properties-file</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
    </parent>

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

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

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

Define Properties file and handler
Properties file
application.properties

simple.a=Simple property
nested.a.b=Nested property

Mapping Properties file into POJO classes

SimpleProperties.java

package com.answerindepth.util;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "simple")
public class SimpleProperties {
    private String a;

    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }
}
NestedProperties.java

package com.answerindepth.util;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "nested")
public class NestedProperties {
    public static class A {
        private String b;

        public String getB() {
            return b;
        }

        public void setB(String b) {
            this.b = b;
        }
    }

    private A a;

    public A getA() {
        return a;
    }

    public void setA(A a) {
        this.a = a;
    }
}

Config and Run

Application Bootstrap

Application.java

package com.answerindepth.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@Slf4j

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public CommandLineRunner runner(SimpleProperties simpleProperties, NestedProperties nestedProperties) {
        return r -> {
            log.info(simpleProperties.getA());
            log.info(nestedProperties.getA().getB());
        };
    }
}
Run with Maven
Type the below command at the project root directory

mvn clean spring-boot:run

No comments:

Post a Comment

Spring Boot @ConfigurationProperties and Properties File

 In this tutorial, you will learn to use @ConfigurationProperties to map properties files to POJO classes in Spring Boot application. Let’s ...