Friday, March 20, 2009

Handling Static variables and methods in Spring Framework

In Spring Framework, static variables and methods are handled slightly different manner

When come to static variables, we may face one of the following situations

How to call static methods?
How to call static methods with arguments?
How to set static variables?
How create beans using static factory methods?

Spring provides MethodInvokingFactoryBean to handle these scenarios

How to call static methods?

Use targetClass and targetMethod properties of MethodInvokingFactoryBean to invoke a static method


java.lang.System
getProperties



How to call static methods with arguments?



getProperty


java.version




How to set static variables?

I didn’t find any straight solution, as a workaround we use a setter method.



setMasterData







How create beans using static factory methods?

Spring also provides a simple approach to create beans using a static method calls, might be useful for implementing factory pattern

class="examples.ExampleBean2"
factory-method="createInstance"/>