spring framework tutorial

Assign value to static variables from application.properties in Spring Boot ?

This tutorial guides you on how to assign value to static variables and local variables from application.properties file in Spring Boot application. You will also learn how to use @ConfigurationProperties approach to define properties.

Spring Boot : Assign value to static variables from application.properties

If you have lot of static variables and other local variables for which you would like to initialize values using application.properties file then you are at the right place.

For example, if you have a spring boot application and wanted to assign value to static variables and other local variables from application.properties, then you have to follow the below example.

SpringDemoApplication

import java.net.URI;

import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringDemoApplication {
private static final int BUFFER_SIZE = Integer.parseInt(PropertyIdentifier.getProperty("buffer.size"));
private static Socket socketio;

public static void main(String[] args) {

    SpringApplication.run(SpringDemoApplication.class, args);
    socketIOClient();

}


private static void connectSocketIOClient() {

    String host = PropertyIdentifier.getProperty("server.host");
    String port = PropertyIdentifier.getProperty("server.port");
    String proxyServer = PropertyIdentifier.getProperty("proxy.server");
    String proxyPort = PropertyIdentifier.getProperty("proxy.port");
    ----
    ----
    socketio = IO.socket(URI.create("ws://"+host+":"+port));
    socketio = socketio.connect();
    ---
    ---
}

---
}

Then you need to write the following property identifier utility class used by your demo application.

PropertyIdentifier.java

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;

public class PropertyIdentifier {

private static Properties properties;
    static {
       properties = new Properties();
       URL url = new PropertyIdentifier().getClass().getClassLoader().getResource("application.properties");
        try{
           properties.load(new FileInputStream(url.getPath()));
        } catch (FileNotFoundException e) {
               e.printStackTrace();
        } catch (IOException e) {
              e.printStackTrace();
        } catch (Exception e) {
              e.printStackTrace();
        }
     }

     public static String getProperty(String key){
        return properties.getProperty(key);
     }
}

Finally, you need to create application.properties file under src/main/resources directory with the following configurations.

application.properties

server.port=4040
server.host=10.187.241.83
proxy.server=23.45.29.251
procy.port=8086
buffer.size=10000

Using @ConfigurationProperties

This is another approach and probably the better way to load multiple properties in a single class. This way you can isolate property configurations in to separate POJO’s.

You need to use @Configuration. Therefore, Spring creates Spring bean in the application context. The @ConfigurationProperties requires to use prefix for all values or properties that you wanted to use in the class.

For example,

@Configuration
@ConfigurationProperties(prefix = "server")
public class ConfigProperties {
    
    private String host;
    private int port;

    // standard getters and setters
}

You can define the standard Java bean setters and getters for each properties that you are configuring.

Note, if you don’t use @Configuration in the POJO, then you need to use @EnableConfigurationProperties(ConfigProperties.class) in the main Spring application class to bind the properties in to the POJO.

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

That’s it. Spring will now automatically bind properties defined in the properties file which has the prefix “server” and the same name as on the fields that are defined in the ConfigurationProperties class.

For example, the simple properties file using @ConfigurationProperties should look like below.

#Example properties file for @ConfigurationProperties
server.host=4040
server.port=10.187.241.83

In this article, you had learnt how to assign value to static variables and local variables from application.properties file in Spring Boot application. And also learnt how to use @ConfigurationProperties annotation to externalize the configuration and access values from properties file.

Hope it is helpful 🙂

You’ll also like:

References:

guest
0 Comments
Inline Feedbacks
View all comments