Spring Boot – Managing Properties File or Externalized Configuration

Properties file plays a vital role in configuring application as any changes to application file doesn’t requires recompilation of code. In Spring  Boot, managing properties is simple and we have multiple ways to achieve it.

Spring Boot Properties are considered in the following order

  1. Command line arguments.
  2. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
  3. ServletConfig init parameters.
  4. ServletContext init parameters.
  5. JNDI attributes from java:comp/env.
  6. Java System properties (System.getProperties()).
  7. OS environment variables.
  8. RandomValuePropertySource that has properties only in random.*.
  9. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).
  10. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
  11. Application properties outside of your packaged jar (application.properties and YAML variants).
  12. Application properties packaged inside your jar (application.properties and YAML variants).
  13. @PropertySource annotations on your @Configuration classes.
  14. Default properties (specified by setting SpringApplication.setDefaultProperties).

We will check out the implementation example of few of above mentioned orders.

Let’s create a Spring Boot application with following folder structure and files,

Spring Boot Project

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.1.7.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.talksinfo</groupId>
  <artifactId>SpringProperties</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringProperties</name>
  <description>Demo project for Spring Properties file</description>

  <properties>
    <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>

ApplicationProperties.java

@Configuration
public class ApplicationProperties {

  @Value("${app.name}")
  private String appName;
  
  @Value("${app.url}")
  private String appURL;
  
  @Bean
  public String getAppName() {
    return appName;
  }
  
  @Bean
  public String getAppURL() {
    return appURL;
  }
  
}

SpringPropertiesApplication.java

@SpringBootApplication
public class SpringPropertiesApplication implements CommandLineRunner{

  @Autowired
  ApplicationProperties appProperties;
  
  public static void main(String[] args) {
    SpringApplication.run(SpringPropertiesApplication.class, args);
  }

  @Override
  public void run(String... args) throws Exception {
    System.out.println(appProperties.getAppName());
    System.out.println(appProperties.getAppURL());
  }

}

application.properties

app.name=TALKSINFO
app.url=www.talksinfo.com

Command Line Arguments

As per the above code, when we run application as Spring Boot SpringPropertiesApplication.java gives us output as follows.

TALKSINFO
www.talksinfo.com

Now, we are going to override the app.name and app.urlinapplication.propertiesusing command line arguments. Try executing the application with following as argument,

java -jar SpringPropertiesApplication.jar --app.name=TestingCommandLineArgument --app.url=www.testing.com

Output

TestingCommandLineArgument
www.testing.com

Properties from SPRING APPLICATION JSON

Now, we are going to override the app.name and app.urlinapplication.properties from command line arguments using JSON. Try executing the application with following as argument,

java -jar SpringPropertiesApplication.jar --spring.application.json="{\"app.name\":\"TestingJSON\",\"app.url\":www.talksinfo.com}"

Output

TestingJSON
www.talksinfo.com

Conclusion

Hence we saw about the order of properties being used, overriding properties from command line argument and using JSON.

Managing properties file using other techniques will be carried out in next posts.

Please follow and like us:
error