Java 从 spring 配置调用静态方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27296276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:08:08  来源:igfitidea点击:

Invoke static method from spring config

javaxmlspringstatic

提问by LucaA

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

是否可以在 Spring 配置文件中调用静态方法?

public MyClass {

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

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

回答by Evgeniy Dorofeev

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

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

回答by Hank Lapidez

  1. When the static method creates an instance of MyClass you an do it like this
  1. 当静态方法创建 MyClass 的实例时,您可以这样做

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.
  1. 如果您只想在 bean 实例化时调用该方法,则 spring 不能这样做。

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();
}

回答by jbarrameda

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

摘自https://gist.github.com/bulin/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.

这也可能很有用:How to make spring injection value into a static field

回答by Cloud

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

如果您使用 Spring 配置的注解,您可以将以下方法添加到 @Configuration 类中:

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

    return methodInvokingFactoryBean;
}