如何让弹簧将值注入静态场 [英] How to make spring inject value into a static field
问题描述
我知道这可能看起来像以前问过的问题,但我在这里面临不同的问题.
我有一个只有静态方法的实用程序类.我没有,我也不会拿它举例.
公共类Utils{私有静态属性 dataBaseAttr;公共静态无效方法A(){}公共静态无效方法B(){}}
现在我需要 Spring 用数据库属性 Properties.Spring 配置来填充 dataBaseAttr:
</豆类>
我已经在其他 bean 中完成了,但是这个类 (Utils) 中的问题不是 bean,如果我将它设为 bean,没有任何变化,我仍然无法使用该变量,因为该类不会被实例化并且变量总是等于空.
你有两种可能:
- 静态属性/字段的非静态设置器;
- 使用 <代码>org.springframework.beans.factory.config.MethodInvokingFactoryBean 调用静态设置器.
在第一个选项中,您有一个带有常规 setter 的 bean,但您设置的是静态属性/字段,而不是设置实例属性.
public void setTheProperty(Object value) {foo.bar.Class.STATIC_VALUE = 值;}
但是为了做到这一点,您需要有一个 bean 实例来公开这个 setter(它更像是一种解决方法).
在第二种情况下,将按如下方式进行:
<块引用><property name="staticMethod" value="foo.bar.Class.setTheProperty"/><属性名称=参数"><列表><ref bean="theProperty"/></list></属性></bean>
在你的情况下,你将在 Utils
类上添加一个新的 setter:
public static setDataBaseAttr(Properties p)
并且在您的上下文中,您将使用上面举例说明的方法对其进行配置,或多或少类似于:
<块引用><property name="staticMethod" value="foo.bar.Utils.setDataBaseAttr"/><属性名称=参数"><列表><ref bean="dataBaseAttr"/></list></属性></bean>
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(){
}
}
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>
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.
You have two possibilities:
- non-static setter for static property/field;
- using
org.springframework.beans.factory.config.MethodInvokingFactoryBean
to invoke a static setter.
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;
}
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>
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屋!