How to read property value from application properties in Java?
This sneppet will guide you on how to read property value from application properties in Java. In Java, you can get a property value from the application.properties in many ways. Let’s learn with examples in the below sections. The application.properties file is a configuration file used by the application to store key-value pairs of configuration data. This file likely contains configuration settings such as database connections, server ports, logging settings, and other application-specific settings.
For example,
APPLICATION_LOG_TO_STANDARD_OUTPUT=true HTTP_HOST=localhost HTTP_PORT=8080 HTTP_THREADS=10 LOG_LEVEL=INFO
1. Passing the property as command-line argument
While running Java application you can use -D option and pass the property as a command-line argument as shown below
java -D APPLICATION_LOG_TO_STANDARD_OUTPUT=true -jar myapp.jar
Then, in your Java class, you can access the property value using System.getProperty() method.
2. Using Properties Class
You can load the properties file into a Properties object using the load() method as shown below.
import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; public class MyClass { public static void main(String[] args) throws IOException { Properties props = new Properties(); props.load(new FileInputStream("application.properties")); String logToStandardOutputProperty = props.getProperty("APPLICATION_LOG_TO_STANDARD_OUTPUT"); System.out.println(logToStandardOutputProperty); } }
The above Java code reads a property value from a file named application.properties and prints its value to the console.
In order to get property using System.getProperty() method, you need to use the following code sneppet.
for (String key : props.stringPropertyNames()) { System.setProperty(key, props.getProperty(key)); }
This above Java code iterates over the property names in the props Properties object and sets each property as a system property using the System.setProperty() method. In other words, it copies all properties from the props object to the system properties. So that you can use System.getProperty() method to read property value from application properties file.
3. Using System.setProperty() method
You can set the property programmatically using the System.setProperty() method.
For example,
public class MyClass { public static void main(String[] args) { System.setProperty("APPLICATION_LOG_TO_STANDARD_OUTPUT", "true"); String logToStandardOutputProperty = System.getProperty("APPLICATION_LOG_TO_STANDARD_OUTPUT"); System.out.println(logToStandardOutputProperty); } }
The above Java code sets a system property named APPLICATION_LOG_TO_STANDARD_OUTPUT to true and then prints its value to the console.
4. Using Spring Framework
If you are Spring framework in your project, then you can use @Value annotation to inject the property in to your Java class.
For example,
import org.springframework.beans.factory.annotation.Value; public class MyClass { @Value("${APPLICATION_LOG_TO_STANDARD_OUTPUT}") private String logToStandardOutputProperty; public void myMethod() { System.out.println(logToStandardOutputProperty); } }
In this case, you need to make sure that the application.properties file is loaded into the Spring application context.
- Sort an array of objects by a string property value ?
- Python Programs to Print Patterns – Pyramid, Triangle using Star
- Java Palindrome Program- Check String or Number is Palindrome
- Program to Check Given Number is Odd or Even
- Convert single digit number to double digits string
- Find the greatest of three numbers
- Most Common Element in an Unsorted Array
- Get Common Elements between Two Unsorted Arrays
- Implement Stack using Linked List
- Convert negative to positive number in Python
- TypeError: a bytes-like object is required, not ‘str’ – Python3
- Extract numbers from a string in python
- Java String substring() example program
- Find which users belongs to a specific group in linux
- print multiplication table for any number
- Check if Python Object is a Number ?
- In-place merge sort instead of normal merge sort ?
- Ways to print arrays in Java
- NullPointerException ? What causes them and how to avoid them? Java
- Why is processing a sorted array faster than processing an unsorted array?
References