Home > front end >  Get all properties as map in properties file Spring Boot
Get all properties as map in properties file Spring Boot

Time:10-06

I have a spring boot project , I want to get those properties as map by prefix , in this exemple the prefix is root :

application.properties :

root.prop = xxxx
root.prop2 = yyyy
root.prop3 = zzzz

I dont want to change my filetype from properties to YAML.

CodePudding user response:

"Easy-peasy":

package com.example.demo;

import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

@SpringBootApplication 
@EnableConfigurationProperties(MyProps.class) // !
public class DemoApplication {

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

    @Bean // a simple test bean
    CommandLineRunner cmd(/*@Autowired*/ MyProps props) {
        return (args) -> {
            System.err.println(props.getRoot());
        };
    }
}

@ConfigurationProperties // ! ..no prefix, because we are "close to"/root! 
class MyProps {

    // this gets our "root." prefix
    private final Map<String, ?> root = new HashMap<>();

    // since initialized, getter sufficient 
    public Map<String, ?> getRoot() {
        return root;
    }

}

With, application.properties:

root.prop = xxxx
root.prop2 = yyyy
root.prop3 = zzzz

And 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</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>

... console prints:

...
2022-10-05 18:12:20.101  INFO 9684 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 0.834 seconds (JVM running for 1.111)
{prop2=yyyy, prop=xxxx, prop3=zzzz}
------------------------------------------------------------------------
...

To reside the properties in a "non-default location" (foo.properties e.g.), just:

//@ ... ion
@EnableConfigurationProperties(MyProps.class)
@PropertySource("classpath:/foo.properties") // ! ...

..but attention: application.properties will have precedence!

Refs:

  • Related