Executing a method when a Spring application starts
Submitted by rowell on Mon, 01/21/2008 - 16:46
This configuration will call the method specified in the init attribute when this bean is created, but it will only work if the method does not require any parameter...
<bean class="com.samples.Echo" init="echo" />
If you need to pass parameters, use the following sample configuration...
<bean id="sampleBean" class="com.samples.Echo"/>
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="sampleBean" />
<property name="targetMethod" value="echo" />
<property name="arguments">
<list>
<value>Hello World!</value>
</list>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="sampleBean" />
<property name="targetMethod" value="echo" />
<property name="arguments">
<list>
<value>Hello World!</value>
</list>
</property>
</bean>
When dealing with a static method, simply change the targetObject to targetClass and targetMethod to staticMethod...
<property name="targetClass" value="com.samples.Echo" />
<property name="staticMethod" value="echoStatic" />
<property name="staticMethod" value="echoStatic" />