Announcement Announcement Module
Collapse
No announcement yet.
Saving property changes on a Spring bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Saving property changes on a Spring bean

    I have a static file that I want to load properties from on the fly and I would like the bean to retain the changes to the properties after I load them so that I can utilize them again later. An example of data in it might be a list of people you want to email that might change. So, I get the bean from the application context and then I load the properties in a configured bean but the properties don't stay when I fetch the singleton again.

    Does anyone know of a way that I can do this?

    properties file:
    Code:
    email.list=[email protected],[email protected]
    MyPropertyUtil class:
    Code:
    public class MyPropertyUtil {
    	private Properties properties;
    	
    	public MyPropertyUtil() {
    		properties = new Properties();
    	}
    	public boolean loadProperties() {
    		//code to read from a file and iterate over each line to load in properties
    
    			String[] keyAndValue = line.split("=");
    			this.properties.put(keyAndValue[0], keyAndValue[1]);
    	}
    }
    Code that calls MyPropertyUtil class:
    Code:
    BeanFactory beanfactory = new ClassPathXmlApplicationContext("applicationContext.xml");
    MyPropertyUtil propertyUtil = (MyPropertyUtil) beanfactory.getBean("myPropertyUtil");
    
    //Call to update a property on the MyPropertyUtil of type java.util.Properties
    myPropertyUtil.loadProperties();
    bean configuration in applicationContext.xml
    Code:
    <bean name="myPropertyUtil" class="com.util.MyPropertyUtil"></bean>

  • #2
    Found a solution

    Found a way to do it without Spring for those that may be interested. Thanks to the static modifier on the member variable the properties member is retained even though the class is not instantiated, thanks to Java.

    properties file:
    Code:
    email.list=[email protected],[email protected]
    MyPropertyUtil class:
    Code:
    public class MyPropertyUtil {
    	private static Properties properties;
    	
    	public static boolean loadProperties() {
    		//code to read from a file and iterate over each line to load in properties
    
    			String[] keyAndValue = line.split("=");
    			this.properties.put(keyAndValue[0], keyAndValue[1]);
    	}
    	
    	public static boolean getProperty(String propName) {
    		return PropertyUtil.properties.getProperty(propName);
    	}
    }
    Code that calls MyPropertyUtil class:
    Code:
    MyPropertyUtil.loadProperties();

    Comment


    • #3
      Hi vermule,

      Thank you for sharing your ideas!

      I am surprised that your first version didn't work. It's a little boring to me because that violates the singleton pattern's rule.

      The second one should work as you explained but be aware these "static" properties are stored in JVM Perm Space in contrary of those non "static" properties which are stored in JVM Heap Space

      Comment

      Working...
      X