Saturday, 26 October 2019

Spring: access all Environment properties as a Map


Using 'org.springframework.core.env.Environment' class, you can get all the environment properties.

Below snippet return all the environment properties as map.
public Map<String, Object> configs() {
    Map<String, Object> properties = new HashMap<>();

    Iterator iterator = ((AbstractEnvironment) env).getPropertySources().iterator();

    while (iterator.hasNext()) {
        PropertySource propertySource = (PropertySource) iterator.next();

        if (propertySource instanceof MapPropertySource) {

            String[] propertyNames = ((MapPropertySource) propertySource).getPropertyNames();

            for (String propName : propertyNames) {
                properties.put(propName, propertySource.getProperty(propName));
            }

        }

    }

    return properties;
}

Find the below working application.

App.java
package com.sample.app;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;

@SpringBootApplication
public class App implements CommandLineRunner {

    @Autowired
    private Environment env;

    public static void main(String args[]) {
        ConfigurableApplicationContext context = SpringApplication.run(App.class, args);

    }

    public Map<String, Object> configs() {
        Map<String, Object> properties = new HashMap<>();

        Iterator iterator = ((AbstractEnvironment) env).getPropertySources().iterator();

        while (iterator.hasNext()) {
            PropertySource propertySource = (PropertySource) iterator.next();

            if (propertySource instanceof MapPropertySource) {

                String[] propertyNames = ((MapPropertySource) propertySource).getPropertyNames();

                for (String propName : propertyNames) {
                    properties.put(propName, propertySource.getProperty(propName));
                }

            }

        }

        return properties;
    }

    @Override
    public void run(String... args) throws Exception {
        Map<String, Object> map = configs();

        System.out.println("****************************************");
        for (String key : map.keySet()) {
            System.out.println(key + " : " + map.get(key));
        }
        System.out.println("****************************************");
    }

}

Create application.properties file under src/main/resources folder.

application.properties
server.port=9999


pom.xml
<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>springbootCore</groupId>
    <artifactId>springbootCore</artifactId>
    <version>1</version>


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

    <dependencies>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

    </dependencies>
</project>



Run App.java, you can see below messages in console.
****************************************
gopherProxySet : false
PATH : /usr/bin:/bin:/usr/sbin:/sbin
awt.toolkit : sun.lwawt.macosx.LWCToolkit
spring.groovy.template.cache : false
file.encoding.pkg : sun.io
java.specification.version : 1.8
sun.cpu.isalist : 
sun.jnu.encoding : UTF-8
spring.thymeleaf.cache : false
java.class.path : /Users/krishna/Documents/EclipseWorkSpaces/Learnings/springbootCore/target/classes:/Users/krishna/.m2/repository/org/springframework/boot/spring-boot-starter/2.1.6.RELEASE/spring-boot-starter-2.1.6.RELEASE.jar:/Users/krishna/.m2/repository/org/springframework/boot/spring-boot/2.1.6.RELEASE/spring-boot-2.1.6.RELEASE.jar:/Users/krishna/.m2/repository/org/springframework/spring-context/5.1.8.RELEASE/spring-context-5.1.8.RELEASE.jar:/Users/krishna/.m2/repository/org/springframework/spring-aop/5.1.8.RELEASE/spring-aop-5.1.8.RELEASE.jar:/Users/krishna/.m2/repository/org/springframework/spring-beans/5.1.8.RELEASE/spring-beans-5.1.8.RELEASE.jar:/Users/krishna/.m2/repository/org/springframework/spring-expression/5.1.8.RELEASE/spring-expression-5.1.8.RELEASE.jar:/Users/krishna/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.1.6.RELEASE/spring-boot-autoconfigure-2.1.6.RELEASE.jar:/Users/krishna/.m2/repository/org/springframework/boot/spring-boot-starter-logging/2.1.6.RELEASE/spring-boot-starter-logging-2.1.6.RELEASE.jar:/Users/krishna/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar:/Users/krishna/.m2/repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar:/Users/krishna/.m2/repository/org/slf4j/slf4j-api/1.7.26/slf4j-api-1.7.26.jar:/Users/krishna/.m2/repository/org/apache/logging/log4j/log4j-to-slf4j/2.11.2/log4j-to-slf4j-2.11.2.jar:/Users/krishna/.m2/repository/org/apache/logging/log4j/log4j-api/2.11.2/log4j-api-2.11.2.jar:/Users/krishna/.m2/repository/org/slf4j/jul-to-slf4j/1.7.26/jul-to-slf4j-1.7.26.jar:/Users/krishna/.m2/repository/javax/annotation/javax.annotation-api/1.3.2/javax.annotation-api-1.3.2.jar:/Users/krishna/.m2/repository/org/springframework/spring-core/5.1.8.RELEASE/spring-core-5.1.8.RELEASE.jar:/Users/krishna/.m2/repository/org/springframework/spring-jcl/5.1.8.RELEASE/spring-jcl-5.1.8.RELEASE.jar:/Users/krishna/.m2/repository/org/yaml/snakeyaml/1.23/snakeyaml-1.23.jar:/Users/krishna/.m2/repository/org/springframework/boot/spring-boot-devtools/2.1.6.RELEASE/spring-boot-devtools-2.1.6.RELEASE.jar
java.vm.vendor : Oracle Corporation
sun.arch.data.model : 64
server.error.include-stacktrace : ALWAYS
java.vendor.url : http://java.oracle.com/
user.timezone : Asia/Kolkata
os.name : Mac OS X
java.vm.specification.version : 1.8
LOGNAME : krishna
user.country : IN
sun.java.launcher : SUN_STANDARD
spring.resources.chain.cache : false
sun.boot.library.path : /Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib
spring.resources.cache.period : 0
sun.java.command : com.sample.app.App
http.nonProxyHosts : local|*.local|169.254/16|*.169.254/16
SHELL : /bin/bash
SSF_LIBRARY_PATH_64 : /Applications/Secure Login Client.app/Contents/MacOS/lib/libsapcrypto.dylib
sun.cpu.endian : little
spring.mvc.log-resolved-exception : true
user.home : /Users/krishna
user.language : en
java.specification.vendor : Oracle Corporation
spring.freemarker.cache : false
java.home : /Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre
SSF_LIBRARY_PATH : /Applications/Secure Login Client.app/Contents/MacOS/lib/libsapcrypto.dylib
TMPDIR : /var/folders/z6/sfz8h4s11bg905bg7yf0g6340000gq/T/
file.separator : /
line.separator : 

spring.h2.console.enabled : true
XPC_FLAGS : 0x0
java.vm.specification.vendor : Oracle Corporation
java.specification.name : Java Platform API Specification
java.awt.graphicsenv : sun.awt.CGraphicsEnvironment
java.awt.headless : true
__CF_USER_TEXT_ENCODING : 0x1F7:0x0:0x0
spring.template.provider.cache : false
Apple_PubSub_Socket_Render : /private/tmp/com.apple.launchd.xrK7AadmQO/Render
spring.reactor.stacktrace-mode.enabled : true
sun.boot.class.path : /Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/sunrsasign.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/classes
server.servlet.jsp.init-parameters.development : true
sun.management.compiler : HotSpot 64-Bit Tiered Compilers
ftp.nonProxyHosts : local|*.local|169.254/16|*.169.254/16
java.runtime.version : 1.8.0_191-b12
user.name : krishna
path.separator : :
APP_ICON_92687 : ../Resources/Eclipse.icns
JAVA_MAIN_CLASS_84687 : com.sample.app.App
os.version : 10.14.6
java.endorsed.dirs : /Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/endorsed
java.runtime.name : Java(TM) SE Runtime Environment
file.encoding : UTF-8
JAVA_STARTED_ON_FIRST_THREAD_92687 : 1
spring.beaninfo.ignore : true
server.port : 9999
java.vm.name : Java HotSpot(TM) 64-Bit Server VM
XPC_SERVICE_NAME : org.eclipse.platform.ide.19784
java.vendor.url.bug : http://bugreport.sun.com/bugreport/
java.io.tmpdir : /var/folders/z6/sfz8h4s11bg905bg7yf0g6340000gq/T/
java.version : 1.8.0_191
user.dir : /Users/krishna/Documents/EclipseWorkSpaces/Learnings/springbootCore
os.arch : x86_64
SQLITE_EXEMPT_PATH_FROM_VNODE_GUARDS : /Users/krishna/Library/WebKit/Databases
PID : 84687
java.vm.specification.name : Java Virtual Machine Specification
java.awt.printerjob : sun.lwawt.macosx.CPrinterJob
USER : krishna
sun.os.patch.level : unknown
spring.mustache.cache : false
SSH_AUTH_SOCK : /private/tmp/com.apple.launchd.mX0NF5ajRW/Listeners
java.library.path : /Users/krishna/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
SNC_LIB_64 : /Applications/Secure Login Client.app/Contents/MacOS/lib/libsapcrypto.dylib
java.vm.info : mixed mode
java.vendor : Oracle Corporation
java.vm.version : 25.191-b12
SNC_LIB : /Applications/Secure Login Client.app/Contents/MacOS/lib/libsapcrypto.dylib
server.servlet.session.persistent : true
java.ext.dirs : /Users/krishna/Library/Java/Extensions:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/ext:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java
sun.io.unicode.encoding : UnicodeBig
java.class.version : 52.0
socksNonProxyHosts : local|*.local|169.254/16|*.169.254/16
HOME : /Users/krishna
****************************************

You can download complete working application from this link.


Previous                                                    Next                                                    Home

No comments:

Post a Comment