Monday 30 July 2018

Spring boot: Application property files

In this post, you are going to learn
a.   What is 'application.properties' file
b.   Example application using 'application.properties' file
c.   Where can I place 'application.properties' file?
d.   How to change the configuration file name?
e.   How to refer the explicit location of the config file name?

What is 'application.properties' file
You can configure the spring application by placing the properties in 'application.properties' file.

Example application using 'application.properties' file
Create ‘application.properties’ file and place it in application class path. For example, I added this file in src/main/resources folder.

application.properties
vendorName=selflearningjava
version=1.5

By using @Value annotation, you can inject these properties.

ConfigBean.java
package com.sample.myApp.model;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class ConfigBean {

 @Value("${vendorName}")
 private String vendor;

 @Value("${version}")
 private String version;

 public String getVendor() {
  return vendor;
 }

 public void setVendor(String vendor) {
  this.vendor = vendor;
 }

 public String getVersion() {
  return version;
 }

 public void setVersion(String version) {
  this.version = version;
 }

}

Application.java

package com.sample.myApp;

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

import com.sample.myApp.model.ConfigBean;

@SpringBootApplication
public class Application {

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

  ConfigBean configBean = applicationContext.getBean(ConfigBean.class);

  String vendor = configBean.getVendor();
  String version = configBean.getVersion();

  System.out.println("**********************************************");
  System.out.printf("vendor : %s\n", vendor);
  System.out.printf("version : %s\n", version);
  System.out.println("**********************************************");
  
  applicationContext.close();

 }

}


When you ran ‘Application.java’, you can able to see below messages in the console.

**********************************************
vendor : selflearningjava
version : 14.3
**********************************************


Project structure looks like below.


Where can I place 'application.properties' file?
SpringApplication loads properties from application.properties files in the following locations and adds them to the Spring Environment:

a.   A /config subdirectory of the current directory
b.   The current directory
c.   A classpath /config package
d.   The classpath root

The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).

How to change the configuration file name?
If you do not like the configuration file name 'application.properties', then you can change the configuration file name by specifying 'spring.config.name' environment property.

For example, below command checks for the filename 'configuration.properties' instead of 'applicaiton.properties'.
java -jar myproject.jar --spring.config.name=configuration

How to pass the config file name in eclipse?
Right click on ‘Application.java’ -> Run As -> Run Configurations



Add below statement in ‘Arguments’ tab.

--spring.config.name=configuration


How to refer the explicit location of the config file name?
You can also refer to an explicit location by using the spring.config.location environment property.

Example
java -jar myproject.jar --spring.config.location=classpath:/default.properties,

You can specify multiple locations by separating them by comma (,).

Example
java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties




Previous                                                 Next                                                 Home

No comments:

Post a Comment