Changing Spring beans software

I am currently developing a web application. I know some of the basics of Spring, however I have not worked with it so much, so I decided to improve my Spring-skills, and I had a problem that I can not find a good solution for.

I would like to implement a function in my application that will allow the user to configure everything without touching any configuration file (for example, spring beans), basically the person who wants to deploy, run and use the application should only copy the .WAR file to, for example , tomcat, and all configuration must be done in the application (for example, in OpenFire).

So, I assume that the user deploys the application to a specific URL, and everything is done through the user interface, he is asked about some parameters (for example, information about connecting to the database), after all the data is given, everything is stored in the cfg file, the application is restarted and the configuration process is completed.

I wanted to have all the configuration details in spring beans, but I found that I cannot change them programmatically (one solution would be to change the file "from spring" and reload the application context after trusteeship - is this a good approach?). Another option is to store the XML file in WEB-INF, for example, and save the data there when it is collected from the user, but how to load the data from the bean file into the application context?

Any tips appriciated!

+4
source share
2 answers

You can save these values โ€‹โ€‹in the properties file. You can use property file values โ€‹โ€‹in spring bean .
You can also update values โ€‹โ€‹in the properties file with user input received through the interface.

+2
source

You can access the bean through the ApplicationContext class and its subclasses. From here you can call getters and setters for proeprties on all your beans; however, it should be noted that the application context has already been created, like BeanFactory , so some life cycle steps will be skipped.

Setting properties that are not related to the life cycle on your own beans would certainly be possible, but I would not recommend trying to change the beans infrastructure properties (for example, transaction management, package scanning) after the context has been configured and started.

If your target user is a naive user, perhaps you could offer functionality to modify the properties file, and then programmatically reload the web application, and your Spring context will load its properties using PropertyPlaceholderConfigurer or PropertyOverrideConfigurer .

If your target user is an โ€œinternalโ€ IT-oriented user, then you can also use JMX, which allows you to change the properties of each bean in context using jconsole or similar.

0
source

All Articles