6

java spring を使用して実行時に Bean のプロパティを動的に変更するにはどうすればよいですか? プロパティ「class」として「class1」または「class2」を使用する必要があるBean mainViewがあります。この決定は、「withSmartcard」プロパティが「Y」または「N」であるプロパティ ファイルに基づいて行う必要があります。

アプリケーションコンテキスト:

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

プロパティファイル:

withSmartcard=Y

4

5 に答える 5

10

PropertyPlaceHolder を使用して、プロパティ ファイルを管理します。

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

次のように ref 属性を変更します。

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

プロパティ ファイル some.where.MyApp.properties に、値として class1 または class2 (選択したもの) を持つwithSmartCardClassImplementationという名前のキーを追加します。

withSmartCardClassImplementation=class1
于 2009-04-28T13:27:55.780 に答える
4

PropertyPlaceholderConfigurerが必要です。マニュアルのそのセクションは、私がその場でできるよりもよくそれを示しています。

あなたの例では、プロパティの値をclass1or class2(Spring コンテキストでの目的の 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 はチューリング完全であることを意図していません。

mainView はアプリケーション コンテキストに直接アクセスできます。

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

よりクリーンなソリューションは、ユーザー構成の処理を独自のクラスにカプセル化して、上記を実行して ApplicationContextAware にする必要があるクラスの数を減らし、必要に応じて他のクラスに挿入することです。

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

余談: mainView と class1 / class2 の間の循環依存関係を考えると、構成例が合法かどうかはわかりません。

于 2009-04-28T13:55:34.060 に答える
1

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

于 2009-04-28T13:23:45.790 に答える
1

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

于 2009-04-28T13:29:43.420 に答える
1

上記の@Olivierに同意します。

それを行うには多くの方法があります。

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

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

于 2009-04-28T14:00:28.400 に答える