如何从spring bean注入静态变量?

时间:2014-08-11 13:40:26

标签: java spring static-members

如何从spring bean注入静态变量?

public class MyClass{
    static String str;
    // how to set value to this variable through Spring
}

<b>Spring bean</b>
<bean id='myclass' class = 'com.so.MyClass'>
    <property name="str" value="xmlpath" />
</bean>.

2 个答案:

答案 0 :(得分:0)

您可以为静态变量使用非静态设置器

public void setStr(String str) {
    your.package.MyClass.str = str;
}

并在您的背景下:

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="yourMethod" value="your.packaged.MyClass.setStr"/>
    <property name="arguments">
        <list>
            <ref bean="str"/>
        </list>
   </property>
</bean>

答案 1 :(得分:0)

                                            

在属性标签中,属性名称将是“staticMethod”,根据我