2009-04-28 17 views
6

Javaスプリングを使用して実行時にBeanのプロパティを動的に変更する方法を教えてください。 私はbean "mainView"を持っています。これは、 "class1"または "class2"のいずれかのプロパティ "class"として使用する必要があります。 この決定は、プロパティファイルのベースで行う必要があります。プロパティ "withSmartcard"は "Y"または "N"です。スプリングスプリングを動的に変更する

のApplicationContext:

<bean id="mainView" 
    class="mainView"> 
    <property name="angebotsClient" ref="angebotsClient" /> 
    <property name="class" ref="class1" /> 
</bean> 



<bean id="class1" 
    class="class1"> 
    <constructor-arg ref="mainView" /> 
</bean> 

<bean id="class2" 
    class="class2"> 
    <constructor-arg ref="mainView" /> 
</bean> 

PropertyFile:

withSmartcard = Y

答えて

1

使用クラスファクトリ。 あなたのプロパティに基づいてインスタンスを返すことができます。

10

<bean id="myPropertyPlaceHolder" 
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <description>The service properties file</description> 
    <property name="location" value="classpath:/some.where.MyApp.properties" /> 
    </bean> 

...あなたのプロパティファイルを管理するためにPropertyPlaceHolderを使用し、次のようにあなたの参照の属性を変更します。あなたの特性で

<bean id="mainView" 
    class="mainView"> 
    <property name="angebotsClient" ref="angebotsClient" /> 
    <property name="class" ref="${withSmartCardClassImplementation}" /> 
</bean> 

はsome.where.MyApp.propertiesファイルの追加、キーwithSmartCardClassImplementationとなります。これは、値としてclass1またはclass2(選択したもの)を持ちます。

withSmartCardClassImplementation=class1 
+0

{$ classIdToBeUsed}または$ {classIdToBeUsed}としますか? –

+0

$ {classIdToBeUsed} :) Typo、ありがとう!明らかに、私はKSSのためにwithSmartCardClassImplementation – Olivier

1

私はあなたがBeanFactoryPostProcessorを実装するクラスを書くことができると信じています。このクラスのBeanが(他のBeanとともに)XML構成ファイルに存在する場合、Springは自動的にそのpostProcessBeanFactory(ConfigurableListableBeanFactory)メソッドを呼び出します。このメソッドに渡されたConfigurableListableBeanFactoryオブジェクトは、Springが作業を開始する前にBean定義を変更するために使用できます。

4

PropertyPlaceholderConfigurerが必要です。マニュアルのその部分は、私がその場でできるよりも優れていることを示しています。

この例では、プロパティの値をclass1またはclass2(春のコンテキストでは目的のBeanの名前)に変更する必要があります。

代わりに、ご使用の構成は次のようになります。

<bean id="mainView" 
    class="mainView"> 
    <property name="angebotsClient" ref="angebotsClient" /> 
    <property name="class"> 
     <bean class="${classToUse}"> 
      <constructor-arg ref="mainView"/> 
     </bean> 
    </property> 
</bean> 

含む構成ファイルで: classToUse = fully.qualified.name.of.some.Class

をBeanやクラス名を使用しないだろうユーザーが編集可能な構成ファイルで使用可能であり、実際には構成パラメーター値として「Y」と「N」を使用する必要があります。その場合、Javaでこれを行うだけで済みますが、Springはチューリング完了のためのものではありません。

if (this.withSmartCards) { 
    this.class_ = context.getBean("class1"); 
} else { 
    this.class_ = context.getBean("class2"); 
} 

クリーナーソリューションは、独自のクラスでユーザ設定の処理をカプセル化することになるApplicationContextAwareする必要がクラスの数を減らすために上記の操作を行います。直接アプリケーションコンテキストにアクセスすることができ

MAINVIEW必要に応じて他のクラスに注入します。

BeanFactoryPostProcessorを使用すると、クラスプロパティの定義をプログラムで登録できます。FactoryBeanを使用すると、動的にBeanを作成できます。どちらもSpringのやや進んだ使い方です。

脇に:mainViewとclass1/class2の間に循環的な依存関係がある場合、あなたの設定例が合法かどうかはわかりません。

1

上記@Olivierに同意します。

これにはさまざまな方法があります。

  1. 使用PropertyPlaceHolderConfigurer ..
  2. 使用BeanPostProcessor ..
  3. 使用春AOP、アドバイスを作成し、プロパティを操作。

上記の項目1をおすすめします。

+0

はい、classIdToBeUsedを変更します: – Olivier

+0

うん、単純で簡単に管理.. – Satya

関連する問題