Properties Files In Spring Boot

What are Property Files in Spring Boot ?

In general, in java applications we will use the properties files for the following requirements.

  1. To manage the frequently modified data in the java applications.
  2. To manage the labels of the GUI components in the GUI applications.
  3. To manage the messages w.r.t a particular local in the Internationalization. 
  4. To manage the Exception descriptions in the exception handling.
  5. To manage all the data validation messages in the Data Validations.
  6. To provide all the database configurations in the Database related applications.
  7. To manage all the server configurations in the Server side applications.

——
——

In Spring Boot applications, the main purpose of the properties files is

  1. To provide the application’s logical name.  
  2. To provide server configurations like port numbers,…
  3. To provide the database configurations like datasource name, driver class name, driver url, database user name, database password,…
  4. To provide security configurations like access privileges, user roles,…
  5. To provide administrative configurations like health checks, metrics,..
  6. To provide profile configurations, dev configurations, testing configurations, production configurations,…

——–
——–

In general, in the property files, we will provide the data in the form of key-value pairs.

In Spring Boot applications, we will provide the properties files under the “resources” folder.

In Spring Boot applications, the default name of the properties file is “application.properties”.

In Spring Boot, by default Tomcat server will come with the port number 8080, here to change the server port number we have to use the following property in the application.properties file.

server.port=1234

In Spring Boot applications, if we want to provide a separate logical name to the application then we have to use the following property in the properties file.

spring.application.name = testapp

In Spring Boot applications, if we want to block the Banner on the console then we have to use the following property in the properties file.

spring.main.banner-mode=off

In the Spring Boot applications, it is possible to provide our own banner in place of the Spring , for this we have to use the following steps.

  1. Create Spring Boot project
  2. Create banner.txt file under resources
  3. Generate a custom banner through any online tool.

https://devops.datenkollektiv.de/banner.txt/index.html

  1. Copy the Custom Banner from the above URL and past into the banner.txt file.
  2. Provide the required properties in the properties files.

Example – banner.txt

banner.txt
,------. ,--. ,--.,------. ,----. ,---.
| .-. \ | | | || .--. '' .-./ / O \
| | \ 😐 | | || '--'.'| | .---.| .-. |
| '--' /' '-' '| |\ \ ' '--' || | | |
`-------' `-----' `--' '--' `------' `--' `--'

${application.title} ${application.version}
Powered by Spring Boot ${spring-boot.version}

application.properties

spring.application.name=app07
application.title=Spring Boot Custom Banner Application
application.version=1.0
spring-boot.version=1.3

App07Application.java

package com.durgasoft.app07;

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

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

In the above application, Spring Boot is able to get the custom banner from the banner.txt file and display the custom banner in the console, here banner.txt file is the default file to get the banner, but it is possible to change the name of the banner.txt file, but we have to provide the name and location of the banner file in the application.properties file with the following property.

spring.banner.location=fileName.txt

Define our own properties

In Spring Boot applications, it is possible to define our own properties in the properties file and we are able to access those properties from the properties file.

To get the data from the properties file we have to take a bean class , where we have to define the properties w.r.t the properties names of the properties file and use the following annotation.

@Value(“${propertyName}”)

Example :

application.properties file

spring.application.name=app08

employee.eno=111
employee.ename=Durga
employee.esal=5000
employee.eaddr=Hyd

Employee.java – Bean class

package com.durgasoft.app08.beans;

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

@Component
public class Employee {

@Value("${employee.eno}")
private int eno;
@Value("${employee.ename}")
private String ename;
@Value("${employee.esal}")
private float esal;
@Value("${employee.eaddr}")
private String eaddr;

public int getEno() {
return eno;
}

public void setEno(int eno) {
this.eno = eno;
}

public String getEname() {
return ename;
}

public void setEname(String ename) {
this.ename = ename;
}

public float getEsal() {
return esal;
}

public void setEsal(float esal) {
this.esal = esal;
}

public String getEaddr() {
return eaddr;
}

public void setEaddr(String eaddr) {
this.eaddr = eaddr;
}
}

EmployeeRunner.java

package com.durgasoft.app08.runner;

import com.durgasoft.app08.beans.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class EmployeeRunner implements CommandLineRunner {

@Autowired
private Employee employee;

@Override
public void run(String... args) throws Exception {
System.out.println("Employee Details");
System.out.println("----------------------");
System.out.println("Employee Number : "+employee.getEno());
System.out.println("Employee Name : "+employee.getEname());
System.out.println("Employee Salary : "+employee.getEsal());
System.out.println("Employee Address : "+employee.getEaddr());
}
}

App08Application.java

package com.durgasoft.app08;

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

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

NOTE — In the Spring boot applications, if the properties file provided properties name and the bean component fields names are the same, then it is possible to copy the data from the properties file to the respective bean component directly without using @Value annotation, but we have to use the following annotation at bean class. 

@ConfigurationProperties(prefix=”---”)

Employee.java — bean class

package com.durgasoft.app08.beans;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "employee")
@Component
public class Employee {
//@Value("${employee.eno}")
private int eno;
//@Value("${employee.ename}")
private String ename;
//@Value("${employee.esal}")
private float esal;
//@Value("${employee.eaddr}")
private String eaddr;

public int getEno() {
return eno;
}

public void setEno(int eno) {
this.eno = eno;
}

public String getEname() {
return ename;
}

public void setEname(String ename) {
this.ename = ename;
}

public float getEsal() {
return esal;
}

public void setEsal(float esal) {
this.esal = esal;
}

public String getEaddr() {
return eaddr;
}

public void setEaddr(String eaddr) {
this.eaddr = eaddr;
}
}

In Spring boot applications, if we provide data in the properties file then that data will be stored in the “Environment” object internally , here it is possible to get the data from the Environment object directly without taking any bean class.

To get the value of a property from the properties file we have to use the following method from Environment.

application.properties

spring.application.name=app09

student.sid=S-111
student.sname=Durga
student.saddr=Hyd
student.semail=durga@durgasoft.com
student.smobile=9988776655

AppRunner.java

package com.durgasoft.app09.runner;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class AppRunner implements CommandLineRunner {

@Autowired
private Environment environment;
@Override
public void run(String... args) throws Exception {
System.out.println("Student Details");
System.out.println("----------------------");
System.out.println("Student Id : "+environment.getProperty("student.sid"));
System.out.println("Student Name : "+environment.getProperty("student.sname"));
System.out.println("Student Address : "+environment.getProperty("student.saddr"));
System.out.println("Student Email : "+environment.getProperty("student.semail"));
System.out.println("Student Mobile : "+environment.getProperty("student.smobile"));
}
}

App09Application.java

package com.durgasoft.app09;

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

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

Q) Is it possible to provide more than one properties file in a single Spring boot application?

Yes, it is possible to provide more than one properties file in a single spring boot application , but we must use different names for the properties files and we must configure all these properties files either with @PropertyResource or with @PropertyResources annotations.

Note: @PropertySources is able to include an array of @PropertySource.

employee.properties
employee.eid=E-111
employee.ename=AAA
employee.esal=50000
employee.eaddr=Hyd

student.properties
student.sid=S-111
student.sname=BBB
student.saddr=Chennai
student.squal=BTech

customer.properties
customer.cid=C-111
customer.cname=CCC
customer.caddr=Pune
customer.cmobile=9988776655

AppRunner.java

package com.durgasoft.app10.runner;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@PropertySource("classpath:employee.properties")
@PropertySource("classpath:student.properties")
@PropertySource("classpath:customer.properties")

@Component
public class AppRunner implements CommandLineRunner {
@Autowired
private Environment environment;
@Override
public void run(String... args) throws Exception {
System.out.println("Student Details");
System.out.println("----------------------");
System.out.println("Student Id : " + environment.getProperty("student.sid"));
System.out.println("Student Name : " + environment.getProperty("student.sname"));
System.out.println("Student Address : " + environment.getProperty("student.saddr"));
System.out.println("Student Qual. : " + environment.getProperty("student.squal"));
System.out.println();

System.out.println("Customer Details");
System.out.println("----------------------");
System.out.println("Customer Id : " + environment.getProperty("customer.cid"));
System.out.println("Customer Name : " + environment.getProperty("customer.cname"));
System.out.println("Customer Address : " + environment.getProperty("customer.caddr"));
System.out.println("Customer Mobile : " + environment.getProperty("customer.cmobile"));
System.out.println();

System.out.println("Employee Details");
System.out.println("----------------------");
System.out.println("Employee Id : " + environment.getProperty("employee.eid"));
System.out.println("Employee Name : " + environment.getProperty("employee.ename"));
System.out.println("Employee Salary : " + environment.getProperty("employee.esal"));
System.out.println("Employee Address : " + environment.getProperty("employee.eaddr"));
}
}

Alternatively :

@PropertySources(
{
@PropertySource("classpath:employee.properties"),
@PropertySource("classpath:student.properties"),
@PropertySource("classpath:customer.properties")
}
)

Properties files locations

In Spring Boot Applications, we are able to provide the properties files in the following locations.

  1. Under resources folder
  2. Under resources / config folder
  3. Under project folder
  4. Under Project / config

In Spring Boot Project, resources and its internal folders are in classpath directly, but the project and its internal folders except resources are not in classpath.

If we want to get the properties files which are existed in the resources and its internal folders then we have to use @PropertySource(“classpath: filename.properties”)

@PropertySource(“classpath: abc.properties”)
@PropertySource(“classpath: ./config/xyz.properties”)

If we want to get the properties files data which are not in the classpath and which are available in a particular file system then we have to use @PropertySource(“file:fileName.properties”) 

@PropertySource(“file: abc.properties”)
@PropertySource(“file: ./config/xyz.properties”)

Example

resources/a.properties
message1=This message is from resources/a.properties.

resources/config/b.properties
message2=This message is from resources/config/b.properties

app11/c.properties
message3=This message is from app11/c.properties

app11/config/d.properties
message4=This message is from app11/config/d.properties

AppRunner.java

package com.durgasoft.app11.runner;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@PropertySources(
{
@PropertySource("classpath:a.properties"),
@PropertySource("classpath:./config/b.properties"),
@PropertySource("file:c.properties"),
@PropertySource("file:./config/d.properties")
}
)
@Component
public class AppRunner implements CommandLineRunner {
@Autowired
private Environment env;
@Override
public void run(String... args) throws Exception {
System.out.println("Message1 : "+env.getProperty("message1"));
System.out.println("Message2 : "+env.getProperty("message2"));
System.out.println("Message3 : "+env.getProperty("message3"));
System.out.println("Message4 : "+env.getProperty("message4"));
}
}

App11Application.java

package com.durgasoft.app11;

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

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

Output :

Message1 : This message is from resources/a.properties.
Message2 : This message is from resources/config/b.properties
Message3 : This message is from app11/c.properties
Message4 : This message is from app11/config/d.properties

In the Environment object, all messages are retrieved from the properties files in an order in which we provided @PropertySource annotations inside the @PropertySources annotation.