19

Is it possible to invoke static method in Spring configuration file?

public MyClass {

   public static void staticMethod() {
       //do something
   }

}
<bean id="myBean" class="MyClass">
   <!-- invoke here -->
</bean>
Luciano van der Veekens
  • 6,307
  • 4
  • 26
  • 30
LucaA
  • 693
  • 3
  • 7
  • 18

4 Answers4

23
  1. When the static method creates an instance of MyClass you an do it like this

config

<bean id="myBean" class="MyClass" factory-method="staticMethod">
   <!-- invoke here -->
</bean>

code

public static MyClass staticMethod() {
       //create and Configure a new Instance
}
  1. If you want the method only to be called on bean instantiation spring can't do it this way.

config

<bean id="myBean" class="MyClass" init-method="init">
   <!-- invoke here -->
</bean>

code

public static void staticMethod() {
       //create and Configure a new Instance
}

public void init() {
     staticMethod();
}
Xstian
  • 8,184
  • 10
  • 42
  • 72
Hank Lapidez
  • 1,857
  • 18
  • 23
  • 2
    Don't know why this answer was down-voted... it answers the question more accurately than the up-voted answer IMO. – John Rix Mar 30 '15 at 13:11
  • I need to return a value from a static method and use the value as property value , can any one help please – Sayak Choudhury Oct 23 '18 at 12:12
  • 1
    What if the static method requires arguments? how do you pass them? – Ghilteras Jan 29 '19 at 21:37
  • Method 1 won't work with params this way. With method 2 the params could be instance properties of MyClass and used int init() as args to staticMethod. more examples here https://www.javatpoint.com/dependency-injection-with-factory-method . May type 3 from the examples could help. – Hank Lapidez Feb 05 '19 at 10:09
17

try this

<bean id="b1" class="org.springframework.beans.factory.config.MethodInvokingBean">
    <property name="staticMethod" value="MyClass.staticMethod" />
</bean>

see http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/config/MethodInvokingBean.html

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
4

Try something like this:

<!-- call static method -->
<bean id="test" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass" value="MyClass" />
    <property name="targetMethod" value="staticMethod" />
    <property name="arguments">
        <list>
            <value>anArgument</value>
        </list>
    </property>
</bean>

Remove arguments as you might not need them.

Taken from https://gist.github.com/bulain/1139874

I was needing to call a static method. The above code worked fine.

This might be useful as well: How to make spring inject value into a static field.

Community
  • 1
  • 1
jbarrameda
  • 1,927
  • 2
  • 16
  • 19
0

If you are using annotations for spring configuration you can add the following method into your @Configuration class:

@Bean
public MethodInvokingFactoryBean methodInvokingFactoryBean() {
    MethodInvokingFactoryBean methodInvokingFactoryBean = new MethodInvokingFactoryBean();
    methodInvokingFactoryBean.setStaticMethod("MyClass.staticMethod");

    return methodInvokingFactoryBean;
}
Cloud
  • 983
  • 11
  • 11