Reply to comment

Executing a method when a Spring application starts

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>

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" />
 

Reply

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.