Close

Spring Boot - Binding Command line Json Properties using @ConfigurationProperties

[Last Updated: Oct 18, 2018]

This example shows how to bind Command line JSON properties using @ConfigurationProperties.

Example

Using @ConfigurationProperties

package com.logicbig.example;

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

@Component
@ConfigurationProperties("app")
public class AppSettings{
  private String title;
  private boolean active;
    .............
}
package com.logicbig.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;

@Component
public class ClientBean {
  @Autowired
  private AppSettings appSettings;

  @PostConstruct
  private void postConstruct() {
      System.out.println(appSettings);
  }
}
package com.logicbig.example;

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

@SpringBootApplication
public class ExampleMain {

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

Supplying JSON via System Properties

Using spring-boot maven plugin:

D:\boot-json-env-with-configuration-props>mvn -q -Dspring.application.json="{\"app\":{\"title\":\"test\",\"active\":true}}"  spring-boot:run
AppSettings{title='test', active=true}

Packaging:

D:\example-projects\spring-boot\boot-json-env-with-configuration-props>mvn -q package spring-boot:repackage

Executing jar

D:\boot-json-env-with-configuration-props>java -Dspring.application.json="{\"app\":{\"title\":\"test\",\"active\":true}}"  -jar target/boot-json-env-with-configuration-props-1.0-SNAPSHOT.jar
AppSettings{title='test', active=true}

Supplying JSON via application argument

D:\boot-json-env-with-configuration-props>java -jar target/boot-json-env-with-configuration-props-1.0-SNAPSHOT.jar --spring.application.json="{\"app\":{\"title\":\"test\",\"active\":true}}"
AppSettings{title='test', active=true}

Supplying JSON via Environment Variable

Setting environment variable from command line in windows:

D:\example-projects\spring-boot\boot-json-env-with-configuration-props>set SPRING_APPLICATION_JSON={"app":{"title":"test","active":true}}
D:\boot-json-env-with-configuration-props>mvn spring-boot:run
AppSettings{title='test', active=true}
D:\boot-json-env-with-configuration-props>java -jar target/boot-json-env-with-configuration-props-1.0-SNAPSHOT.jar
AppSettings{title='test', active=true}

On Linux we can set environment variable from command line as:

$ export SPRING_APPLICATION_JSON="{\"app\":{\"title\":\"test\",\"active\":true}}"

Example Project

Dependencies and Technologies Used:

  • Spring Boot 2.0.5.RELEASE
    Corresponding Spring Version 5.0.9.RELEASE
  • spring-boot-starter : Core starter, including auto-configuration support, logging and YAML.
  • JDK 1.8
  • Maven 3.5.4

Command line Json Properties + @ConfigurationProperties Select All Download
  • boot-json-env-with-configuration-props
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • AppSettings.java
          • resources

    See Also