如何使弹簧注入值进入静态字段 [英] How to make spring inject value into a static field

查看:118
本文介绍了如何使弹簧注入值进入静态字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能看起来像一个以前提出的问题,但我面临着一个不同的问题。

I know this may looks like a previously asked question but I'm facing a different problem here.

我有一个只有静态方法的实用程序类。我不会,我不会从它的实例。

I have a utility class that has only static methods. I don't and I won't take an instance from it.

public class Utils{
    private static Properties dataBaseAttr;
    public static void methodA(){

    }

    public static void methodB(){

    }
}

现在我需要Spring用dataBaseAttr来填充数据库属性Properties.Spring config是:

Now I need Spring to fill dataBaseAttr with database attributes Properties.Spring config is:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

<util:properties id="dataBaseAttr"
        location="file:#{classPathVariable.path}/dataBaseAttr.properties" />
</beans>

我已经在其他bean中做了,但是这个类(Utils)中的问题不是bean,如果我把它变成一个bean没有什么变化,我仍然不能使用该变量,因为类不会被实例化,变量总是等于null。

I already done it in other beans but the problem here in this class (Utils) isn't a bean, And if I make it a bean nothing changes I still can't use the variable since the class will not be instantiated and variable always equals null.

推荐答案

您有两种可能性:


  1. 静态属性/字段的非静态设置器;

  2. 使用 org.springframework.beans.factory.config.MethodInvokingFactoryBean 以调用静态设置器。

在第一个选项中,你有一个具有常规安装程序的bean,而是设置一个实例属性,你设置静态属性/字段。

In the first option you have a bean with a regular setter but instead setting an instance property you set the static property/field.

public void setTheProperty(Object value) {
    foo.bar.Class.STATIC_VALUE = value;
}

但是为了做到这一点你需要一个bean的实例

but in order to do this you need to have an instance of a bean that will expose this setter (its more like an workaround).

在第二种情况下,将执行以下操作:

In the second case it would be done as follows:


<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="staticMethod" value="foo.bar.Class.setTheProperty"/>
    <property name="arguments">
        <list>
            <ref bean="theProperty"/>
        </list>
   </property>
</bean>


在这种情况下,您将在 Utils class:

On you case you will add a new setter on the Utils class:

public static setDataBaseAttr(Properties p)

在上下文中,您将使用上面列举的方法配置它,或多或少像:

and in your context you will configure it with the approach exemplified above, more or less like:


<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="staticMethod" value="foo.bar.Utils.setDataBaseAttr"/>
    <property name="arguments">
        <list>
            <ref bean="dataBaseAttr"/>
        </list>
   </property>
</bean>


这篇关于如何使弹簧注入值进入静态字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆