Behavior of static member when using factory-method
I have a factory that has a non static getObject that returns a static member of the factory.
That static member updates its data when a file is updated on disk. I know this is working as I can see the value change when the file is reloaded via a low priority thread.
What I expected to see is that the reference would change but is instead behaving like the static member is never updated within the bean context.
I realize that the FooConfigurationFactory is a singleton but I would expect that any call to that singleton's factory-method which returns a static FooConfiguration would see the FooConfiguration update between calls to getBean("fooConfiguration"), if the static FooConfiguration changed but I am not seeing that. What is the reason for that and how would I gain the behavior that I am expecting?
Example from a single JUnit test with no request scoping, etc:
Code:
FooConfiguration config = (FooConfiguration)context.getBean("fooConfiguration");
assertTrue(config.isEnabled("someProp"));
System.out.println("Hey go modify your file!");
Thread.sleep(30000);
// I modify the file and see the file reload and the static reference update by getting the value of someProp and seeing it is now changed
assertFalse(config.isEnabled("someProp"));
So the assertFalse fails because someProp is still evaluating to true even though I can see that the static ref within the factory has changed for that property after the load. I expected the assertFalse to pass but the unit test still thinks the value is true.
Code:
public class FooConfigurationFactory {
private static FooConfiguration fooConfiguration;
private URL fileLocation;
private long pollingDelay;
private FileWatcherThread thread;
public FooConfigurationFactory() {
}
public void init() {
if (FooConfigurationFactory.fooConfiguration == null) {
loadProperties();
try {
thread = new FileWatcherThread(getFileLocation(), getPollingDelay());
} catch (Exception e) {
e.printStackTrace();
}
}
}
public FooConfiguration getObject() {
return FooConfigurationFactory.fooConfiguration;
}
public void loadProperties() {
FooConfiguration tmpFooConfiguration = new FooConfiguration(getFileLocation());
FooConfigurationFactory.fooConfiguration = tmpFooConfiguration;
System.out.println("\n\n>>>>>>>>>>>>>>>>> loadProperties called. someProp = " + fooConfiguration.isEnabled("someProp") + "\n\n");
}
......
Code:
<bean id="fooConfigurationFactory"
class="FooConfigurationFactory"
init-method="init">
<property name="fileLocation" ref="fooUrl"/>
<property name="pollingDelay" value="1000"/>
</bean>
<bean id="fooConfiguration"
factory-bean="FooConfigurationFactory"
factory-method="getObject"/>