Generally when we try to inject any value for a variable using spring we inject the non static variables.
But it is quite often that we need to initialize a static variable in a java pojo class.
The normal way of injecting the values in pojo class does not work for static variables.
The MethodInvokingFactoryBean class of spring needs to be used for this purpose.
Here is a example.
We have an employee class where company is static variable.
We will inject value for company using MethodInvokingFactoryBean.
Employee.java
public class Employee{
private static String company;
private String emp_id;
public static void setCompany(String comp){
Employee.company=comp;
}
//other getter and setters
}
spring.xml
<bean id="abc" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod">
<value>Employee.setCompany</value>
</property>
<property name="arguments">
<list>
My New Company
</list>
</property>
</bean>
Note: My New Company is the value which we want to set for static variable company. We are setting this value using setter method of employee.