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,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
APPLICATION_LOG_TO_STANDARD_OUTPUT=true
HTTP_HOST=localhost
HTTP_PORT=8080
HTTP_THREADS=10
LOG_LEVEL=INFO
APPLICATION_LOG_TO_STANDARD_OUTPUT=true HTTP_HOST=localhost HTTP_PORT=8080 HTTP_THREADS=10 LOG_LEVEL=INFO
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
java -D APPLICATION_LOG_TO_STANDARD_OUTPUT=true -jar myapp.jar
java -D APPLICATION_LOG_TO_STANDARD_OUTPUT=true -jar myapp.jar
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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);
}
}
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); } }
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
for (String key : props.stringPropertyNames()) {
System.setProperty(key, props.getProperty(key));
}
for (String key : props.stringPropertyNames()) { System.setProperty(key, props.getProperty(key)); }
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,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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);
}
}
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); } }
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,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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);
}
}
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); } }
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.

References

 

Subscribe
Notify of
guest


0 Comments
Inline Feedbacks
View all comments