1

I'm writing a WebApp back-end with spring in java. in the code there are a lot of magic numbers. is there a way to put this in a config in such a way that any changes in this config will be put in effect without restarting the entire app?

VVN
  • 1,607
  • 2
  • 16
  • 25
ziraak
  • 131
  • 3
  • 14
  • check this question http://stackoverflow.com/questions/26150527/how-can-i-reload-properties-file-in-spring-4-using-annotations – user1516873 Feb 26 '16 at 10:45

3 Answers3

1

When java process start it loads spring context, once spring context is loaded it only reads properties file once so if you are changing any property you have to restart your app thats good to have way.

OR you can replace java.util.Properties with a PropertiesConfiguration from the Apache Commons Configuration project. It supports automatic reloading, either by detecting when the file changes, or by triggering through JMX.

one more alternate way is to keep all your prop variables in database and refresh your reference cache periodically that way you don't have to restart your app and can change properties on-fly from database.

Amey Jadiye
  • 3,066
  • 3
  • 25
  • 38
0

You can call the configuration file through the below steps:

  1. Use the @Configuration annotation for the class which calls the config file.
  2. Another annotation to be declared above the class for defining the path to the config file @PropertySource({"URL/PATH_OF_THE_CONFIG_FILE"})
  3. @Value("${PROPERTY_KEY}") annotation above a variable where the value corresponding to the property_key needs to be assigned.
  4. The the following bean in the same configuration invoking class.

    @Bean 
    public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
            return new PropertySourcesPlaceholderConfigurer();
        }
    
  5. Make sure the @ComponentScan covers the folder where the config file is placed

Srikanta
  • 1,145
  • 2
  • 12
  • 22
0

Here is the way you can configure it

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemalocation="http://www.springframework.org/schema/beans  
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
   <!--To load properties file -->  
   <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    
     <property name="location" value="classpath:META-INF/*-config.properties"> 
   </property></bean>    
  <bean id="createCustomer" class="com.example.Customer">  
    <property name="propertyToInject" value="${example.propertyNameUnderPropertyFile}">  
</beans>  

You can also refer it in java file straight away

public class Customer {
 @Value("${example.propertyNameUnderPropertyFile}")
 private String attr;

 }
M Sach
  • 33,416
  • 76
  • 221
  • 314