7

I would like to maintain a list of application properties like service endpoints, application variables, etc. in a Spring application. These properties should be able to updated dynamically (possibly through an web page by system administrator).

Does spring has an inbuilt feature to accomplish this requirement?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Vel
  • 767
  • 5
  • 10
  • 24
  • 4
    See here: http://stackoverflow.com/questions/26150527/how-can-i-reload-properties-file-in-spring-4-using-annotations In addition to the solutions posted one comment references the following built-in functionality: http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#dynamic-language-refreshable-beans – Alan Hay Dec 17 '16 at 10:53
  • You might want to have a look at spring-boot admin. http://codecentric.github.io/spring-boot-admin/1.5.3/ – Ketan Patil Oct 05 '17 at 07:24

4 Answers4

3

I am not sure spring has an implementation for updating the properties file dynamically.

You can do something like reading the properties file using FileInputStream into a Properties object. Then you will be able to update the properties. Later you can write back the properties to the same file using the FileOutputStream.

// reading the existing properties
FileInputStream in = new FileInputStream("propertiesFile");
Properties props = new Properties();
props.load(in);
in.close();
// writing back the properties after updation
FileOutputStream out = new FileOutputStream("propertiesFile");
props.setProperty("property", "value");
props.store(out, null);
out.close();
Johny
  • 2,128
  • 3
  • 20
  • 33
  • we will be able to update or maybe add properties as well with this.. but we will need to restart the server so that the updates/additions take effect. isn't it? – user1305398 May 20 '22 at 09:57
0

I am not sure, but check if you can make use @ConfigurationProperties of Spring boot framework.

@ConfigurationProperties(locations = "classpath:application.properties", ignoreUnknownFields = false, prefix = "spring.datasource")
  1. You can keep this application.properties file in you classpath
  2. Change the properties in this file without redeploying the application

Java Experts - I am just trying to explore my view. Corrections are always welcome.

Edit - I read a good examples on @PropertySource here

0190198
  • 1,667
  • 16
  • 23
0

Externalizing properties, take a look here

Spring loads these properties which can be configured at runtime and accessed in your application in different ways.

viruskimera
  • 193
  • 16
0

Add your own implementation of a PropertySource to your Environment.

Warning: Properties used by @ConfigurationProperties and @Value annotations are only read once on application startup, so changing the actual property values at runtime will have no effect (until restarted).

Andreas
  • 154,647
  • 11
  • 152
  • 247