Results 1 to 3 of 3

Thread: Saving property changes on a Spring bean

  1. #1
    Join Date
    Feb 2009
    Location
    NE
    Posts
    16

    Question 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=test@test.com,emailme@test.com
    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. #2
    Join Date
    Feb 2009
    Location
    NE
    Posts
    16

    Lightbulb 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=test@test.com,emailme@test.com
    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();

  3. #3
    Join Date
    Jun 2012
    Location
    Paris
    Posts
    9

    Default

    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •