69

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

我有一个只有静态方法的实用程序类。我没有,我也不会从中举一个例子。

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

    }

    public static void methodB(){

    }
}

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

<?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,我仍然无法使用该变量,因为该类不会被实例化并且总是变量等于空。

4

3 回答 3

111

你有两种可能:

  1. 静态属性/字段的非静态设置器;
  2. 用于org.springframework.beans.factory.config.MethodInvokingFactoryBean调用静态设置器。

在第一个选项中,您有一个带有常规 setter 的 bean,而是设置了一个实例属性,您设置了静态属性/字段。

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

但为了做到这一点,您需要一个 bean 的实例来公开这个 setter(它更像是一种解决方法)。

在第二种情况下,它将按如下方式完成:

<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该类上添加一个新的设置器:

public static setDataBaseAttr(Properties p)

在您的上下文中,您将使用上面举例说明的方法对其进行配置,或多或少类似于:

<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>
于 2012-07-04T07:36:43.957 回答
22

我有一个类似的要求:我需要将 Spring 管理的存储库 bean 注入我的Person实体类(“实体”,如“具有身份的东西”,例如 JPA 实体)。一个Person实例有朋友,为了让这个Person实例返回它的朋友,它应该委托给它的存储库并在那里查询朋友。

@Entity
public class Person {
    private static PersonRepository personRepository;

    @Id
    @GeneratedValue
    private long id;

    public static void setPersonRepository(PersonRepository personRepository){
        this.personRepository = personRepository;
    }

    public Set<Person> getFriends(){
        return personRepository.getFriends(id);
    }

    ...
}

.

@Repository
public class PersonRepository {

    public Person get Person(long id) {
        // do database-related stuff
    }

    public Set<Person> getFriends(long id) {
        // do database-related stuff
    }

    ...
}

那么我是如何将该PersonRepository单例注入到类的静态字段中的Person呢?

我创建了一个,它在Spring ApplicationContext 构建时@Configuration被拾取。这会注入所有我需要作为静态字段注入其他类的bean。然后通过注释,我捕获了一个钩子来执行所有静态字段注入逻辑。@Configuration@PostConstruct

@Configuration
public class StaticFieldInjectionConfiguration {

    @Inject
    private PersonRepository personRepository;

    @PostConstruct
    private void init() {
        Person.setPersonRepository(personRepository);
    }
}
于 2013-02-05T13:24:12.870 回答
2

由于这些答案很旧,我找到了这个替代方案。它非常干净,只使用 java 注释:

要修复它,请创建一个“非静态设置器”来为静态变量分配注入的值。例如 :

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class GlobalValue {

public static String DATABASE;

@Value("${mongodb.db}")
public void setDatabase(String db) {
    DATABASE = db;
}
}

https://www.mkyong.com/spring/spring-inject-a-value-into-static-variables/

于 2019-09-26T05:17:43.253 回答