2016-05-25 2 views
0

フレームワーク(ファクトリクラスを作成しない)を使用して条件付きでBeanを外部に挿入するにはどうすればよいですか?Spring IoC:実行時の条件付き注入

以下のシナリオでは、両方のchildBeanはすでにインスタンス化されていますが、条件に基づいて実行時に親beanに注入されます。

<bean id=ChildBean1> 
<bean id=ChildBean2> 
<parentBean name='parentBean' lazy-init="true"> 
    <property name='flag'> 

    <somecondition flag=1/> 
    <property name='child' ref ='childBean1'/> 
    <somecondition flag=2/> 
    <property name='child' ref ='childBean2'/> 
</parentBean> 
+0

を検索して、プロファイルを使用します。

<bean class="com.example.spring.TestBean"> <property name="dependency" value="#{systemProperties['profile'] == 'test' ? dependencyA : dependencyB}" /> </bean> 

また、それは以下のようなJavaコンフィグ何かを使用して可能です。 –

答えて

1

あなたは春の式言語(SPEL)を介してそれを行うことができます。

@Bean 
public HelloBean helloBean() { 
    HelloBean helloBean = new HelloBean(); 
    if (condition) { 
     helloBean.setDependency(dependencyA()); 
    } else { 
     helloBean.setDependency(dependencyB()); 
    } 
    return helloBean; 
} 
+0

こんにちは、ありがとう、しかし、私の要件は、そのトランザクションのフローに基づいてトランザクションごとに動的にインジェクション依存関係です。 –

関連する問題