0

I am working with Spring and have information in my application.properties that I want to update from an HTML page

Myapplication.properties

...
spring.mail.host=smtp.gmail.com
spring.mail.port=587
...

Let say we need to change the port.

Is it possible to do something like that and what is the result if a user is logged in and we made a change?

I also read this post Update property in spring environment in java code is it the right solution.

I guess if I say that we need to rebuild the appplication.properties after changing some information.

Yagami Light
  • 1,756
  • 4
  • 19
  • 39

2 Answers2

1

Is it possible to do something like that and what is the result if a user is logged in and we made a change?

if i understood it right, you want to change mail port in runtime? if so :

of course this is possible, but changing the value in property file alone wouldn't result in a actual change in your system, you should know that it is your responsibility to manage the reconstruction of a new mail sender instance in which you should also consider issues like multi-threading , race-condition , etc

I propose you to use application.properties in system startup to initialize your instance, and in case of change use something like this: taking advatage of the Changing mail configuration in runtime and singleton pattern you should probabaly reach your aim :

@Component
public class MailSender{

  @Value("${spring.mail.host}")
  public static String host;
  @Value("${spring.mail.port}")
  private static Integer port

  private static JavaMailSender instance;

  public static synchronized JavaMailSender getInstance(Integer port) {
     if (instance == null || port!= null) {
      MailSender.port = port!=null ? port: MailSender.port;
      JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
      mailSender.setHost(MailSender.host);
      mailSender.setPort(MailSender.port);
      return instance;
  }

}

The above code is an alteration of singleton pattern in which we check whether the mail instance is null or port has new value recreate the instance otherwise if instance has already a value use that, in this way you can change port run time. please notice that by the code above I am trying to give you some insight into the problem and you may change it based on your design.

I hope I got your purpose correctly.

  • did you look to the post that i add in my question if i understand even if i use `System.setProperty("key","value")` it will not change the key. – Yagami Light Sep 01 '18 at 12:56
  • yes, but application.properties would not get changed on runtime, if you need a variable to change runtime you can use the system property like in the link you have provided. take a look also at https://stackoverflow.com/questions/21204334/system-setproperty-and-system-getproperty – MohammadReza Alagheband Sep 01 '18 at 13:06
  • You are totally true it doesn't change the mail port i will test your solution and give u a feedback any idea how to implement your solution (more details). – Yagami Light Sep 01 '18 at 13:33
  • sure, i'd be glad to help – MohammadReza Alagheband Sep 01 '18 at 14:04
  • to make your solution more valuable please add this post link the combinaison of the informations that i get from your post and the informations that i get from the link [Changing mail configuration in runtime](https://stackoverflow.com/questions/16599078/changing-mail-configuration-in-runtime) helped me to solve my problem – Yagami Light Sep 01 '18 at 14:06
  • also add all the valuable link that you provide in your comments – Yagami Light Sep 01 '18 at 14:08
  • sure, it's been added – MohammadReza Alagheband Sep 01 '18 at 14:15
0

First of all, after changing configuration you have to reload beans dependent on changed variables.

I would recommend you to have a look at Spring Cloud Config project.

It has the following features:

  • Stores configs (and changes) in Git
  • Can change configuration properties at runtime, and force subscribed applications to reload their context (or even dependent beans only) automatically

Despite it is not direct answer to your question (it doesn't have an UI for configuration), but it is a good reason to search UI for Spring Cloud Config instead.

Max Farsikov
  • 2,451
  • 19
  • 25