簡體   English   中英

如何讓 spring 向靜態字段注入值

[英]How to make spring inject value into a static field

我知道這可能看起來像以前問過的問題,但我在這里面臨不同的問題。

我有一個實用程序類,它只有靜態方法。 我沒有,我也不會從中舉個例子。

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

    }

    public static void methodB(){

    }
}

現在我需要 Spring 用數據庫屬性 Properties 填充 dataBaseAttr。Spring 配置是:

<?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 沒有任何改變我仍然不能使用變量,因為類不會被實例化並且變量總是等於空。

你有兩種可能性:

  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類上添加一個新的 setter:

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>

我有一個類似的要求:我需要將一個 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類的靜態字段中呢?

我創建了一個@Configuration ,它在Spring ApplicationContext 構建時被獲取。 這個@Configuration被注入了我需要作為靜態字段注入到其他類中的所有bean。 然后使用@PostConstruct注釋,我抓住了一個鈎子來執行所有靜態字段注入邏輯。

@Configuration
public class StaticFieldInjectionConfiguration {

    @Inject
    private PersonRepository personRepository;

    @PostConstruct
    private void init() {
        Person.setPersonRepository(personRepository);
    }
}

由於這些答案很舊,我找到了這個替代方案。 它非常干凈,僅適用於 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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM