1

I have a web form that enables one to update the appliaction.properties, so that user does not have to go to the file and edit it. I wants to load the value from the application.properties file and display in the web form textfield. After the user edit the some changes, user can update the application.properties file.

Question - How can I bind my web form to application.properties file other than using @value in Spring? As the key/value pairs might be updated through web form, I believe @value is not the right choice.

Ken.T
  • 9
  • 5
  • For your application, you can store your updated properties to Map/Dto after loading the properties file and you can read it from there if required. You will need to make sure that you are updating map/dto and properties when you are binding your form with properties file. You will be able to use @Value during application startup only. It will not be useful after context is loading. – asg Sep 25 '17 at 06:50
  • Hi, I am interested in the idea you proposed. Could you by any chance have materials or example that I can relate to? Thanks in advance! – Ken.T Sep 25 '17 at 07:24
  • Please check this.. https://stackoverflow.com/questions/42179664/inject-all-keys-and-values-from-property-file-as-map-in-spring – asg Sep 25 '17 at 07:46
  • Now consider that your map is loaded during startup. Next when you are updating your application.properties file using web form, update this map. And next time when you are reading the properties file read it from this map. – asg Sep 25 '17 at 07:48

2 Answers2

2

The application properties are for the settings the application needs to load when it starts up, and then connects to a database for example, with the username and password specified in that file (although environment variables can also be used.)

You shouldn't really edit this file at run-time, with the exception of using the Refresh Scope annotation. But I don't advise you do that as it's fairly hard in practice to achieve an application which supports changing configuration at run-time.

There is also Spring Cloud Config server for reloading properties at run-time and that provides a REST API although you mainly just use git with that one.

But the key is here that they are all mentioning configuration which is only something developers do. I also don't think you want to use the filesystem for storing your state, much rather have database. There are many reasons to use a file system over a database I won't go into them now.

I will make the assumption you are a beginner looking at your rep. Best advice I can give you is to watch what everyone else is doing and learn from that first. Most of the time people are all doing similar things because it is the right thing to do. If you can't find anybody else changing properties files based on form submissions in their spring app but rather using databases you probably want to go with the database.

Derrops
  • 7,651
  • 5
  • 30
  • 60
  • Hi, Thanks for the reply. But what if I intend to restart the web application after updating the configuration with my web form. Will there be any possible solutions other than using Spring Cloud Config? – Ken.T Sep 25 '17 at 06:32
  • If you are using Spring cloud config, you will not need starting application.. properties will be updated automatically. – asg Sep 25 '17 at 06:47
0

We were in a similar situation last month where client need update in configuration file in run time. We created a central Spring component that loads all properties from file using @Value

@Component
public class ConfigurationManagerComponent{
  @Value("conf.username")
  private String userName;
  @Value("conf.email");
  private String email;
  /*
   * All your attributes
  */
  //getters and setters
  public String getUserName(){
    return this.userName;
  }
  public void setUserName(String userName){
   // first set the value to this class (component)
   this.userName=userName;
   //second save this value in the properties file
   //a specific method you have to implement
    saveproperties("conf.username",userName); 
  }
  /*
   * Do like this for all your getters and setters
  */ 
}

Then, in any time you need to get or save properties, just inject this component and use its getters and setters

@Autowired
ConfigurationManagerComponent configComponent;
String myActualUserName=configComponent.getUserName();

So then you can get all your data in your controller from config component and set them to your spring model to fill form in webpage, and when saving after edit, you can always use setters of your config component(update class and properties file).

This method is not a standard, just a solution we employed to resolve a problem, it's giving good results right now. You can also check Apache commons configuration Here for other solutions.. Hope this will help.

  • Hi, what if there is a need to add new configuration into the application.properties? – Ken.T Oct 02 '17 at 09:01
  • Hi, simply you add this new property in your component and add its getter and setter.. – oussamaLachiheb Oct 02 '17 at 11:44
  • Hi, sorry. I think I should rephrase my question. The question I am asking is what if there is a need to add new configuration to the application.properties through the web form. Eg. Click a "Add" button should create fields for user to input a new configuration. – Ken.T Oct 03 '17 at 02:05
  • Ahh now i understand you, i'm sorry but this solution suppose a static properties file already set – oussamaLachiheb Oct 03 '17 at 07:22