2012-07-04 14 views
55

これは以前に尋ねられた質問のように見えるかもしれませんが、ここでは別の問題に直面しています。静的フィールドにスプリング注入値を設定する方法

私は静的メソッドしか持たないユーティリティクラスを持っています。私はしないし、私はそれからインスタンスを取ることはありません。私はすでに他のBeanにそれを行ったが、ここで、このクラス(Utilsの)問題ではない

<?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> 

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

    } 

    public static void methodB(){ 

    } 
} 

今、私はデータベースとdataBaseAttrを埋めるために春を必要とするがProperties.Springの設定がある属性私はそれをBeanに変更しても、変数は使用できません。クラスはインスタンス化されず、変数は常にnullに等しいからです。

答えて

93

あなたは二つの可能性があります。

  1. 静的プロパティ/フィールドの非静的セッターを。
  2. org.springframework.beans.factory.config.MethodInvokingFactoryBeanを使用して静的セッターを呼び出します。

最初のオプションでは、通常のセッターを持つBeanがありますが、静的プロパティ/フィールドを設定するインスタンスプロパティを設定します。

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

いますが、このセッター(そのより回避策など)を公開するBeanのインスタンスを持っている必要があり、これを行うためです。次のようにそれが行われることになる第2のケースで

<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> 
+0

ありがとうございました。本当にありがとうございました。 –

+0

あなたは大歓迎です! –

+0

私はそれをよく理解していなかったので、私は最初の解決策を試しませんでした。私は2番目のソリューションを試して、それは素晴らしい仕事をした。 –

20

私は、同様の要件を持っていた:私は(「アイデンティティで何か」のような「実体」、例えばJPAエンティティ)私のPersonエンティティクラスにSpring管理リポジトリBeanを注入するために必要な。 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 
    } 

    ... 
} 

は、どのように私はPersonクラスの静的フィールドにそのPersonRepositoryシングルトンを注入したのですか?

私は@Configurationを作成しました。これは、Spring ApplicationContext構築時間で取得されました。この@Configurationは、静的フィールドとして他のクラスに注入する必要のあるすべてのBeanを注入します。その後、@PostConstructアノテーションを使用して、すべての静的フィールド注入ロジックを行うフックを捕まえます。

@Configuration 
public class StaticFieldInjectionConfiguration { 

    @Inject 
    private PersonRepository personRepository; 

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

これは静的メソッドからどのようにアクセスできますか?私はこれがまったく可能だとは思わない!それはPerson.personRepository = personRepositoryではありませんか? –

+3

@ JonasGeiregatメソッドが静的であり、静的変数にアクセスしている可能性があります。 –

+3

静的コンテキストで 'this'を介して静的変数を参照する方法を知りたいですか? – Kripz

関連する問題