2017-08-07 18 views
0

私はspring統合を使用していくつかのコンポーネント間でtcpメッセージをルーティングするspringbootアプリケーションを持っていますが、機能的なPOVからうまく機能しています。SpringのプロパティファイルからBean IDを渡す

実際の例では、複数のチャネルで構成されており、ルータ:

<int:channel id="input"> 

<int:channel id="outputA"/> 
<int:channel id="outputB"/> 
<int:channel id="outputC"/> 

<int:router method="determineTargetChannel" input-channel="input"> 
    <beans:bean class="MyRouter"/> 
</int:router> 

そして私は、プロパティファイルで宣言された定数で複数のハードコードされた値(チャンネル名)をコードをクリーンアップしようとしているとの交換にこだわっ私もXMLファイル内およびルータクラスの両方で、2つの場所でハードコードチャンネルIDを持っていると思っていなかった

@Component 
public class MyRouter { 

    @Router 
    public String determineTargetChannel(Object payload) { 
     if (condition1) { 
      return "outputA"; 
     } else if (condition2) { 
      return "outputB"; 
     } else return "outputC"; 
    }  
} 

(または定数としてそれらを保持することができ、他のクラス):MyRouterクラス。

router: 
    channel: 
     outputA: outputA 
     outputB: outputB 
     outputC: outputC 

2)を更新したXMLファイル:

<int:channel id="input"> 

<int:channel id="${router.channel.outputA}"/> 
<int:channel id="${router.channel.outputB}"/> 
<int:channel id="${router.channel.outputC}"/> 

<int:router method="determineTargetChannel" input-channel="input"> 
    <beans:bean class="MyRouter"/> 
</int:router> 

3)アップデート

1)プロパティYMLファイル内のIDを格納します。

は、だから私は、次のことを試してみましたMyRouterクラス:

@Component 
public class DARouter { 

    @Value("${router.channel.outputA}") 
    private String outputA; 

    @Value("${router.channel.outputB}") 
    private String outputB; 

    @Value("${router.channel.outputC}") 
    private String outputC; 

    @Router 
    public String determineTargetChannel(Object payload) { 
     if (condition1) { 
      return outputA; 
     } else if (condition2) { 
      return outputB; 
     } else return outputC; 
    } 
} 

問題はで、XMLファイルで宣言されたBeanがSpringで作成された時点でプロパティ値が解決されないため、実際のものではなくID = $ {router.channel.outputA}のBeanが作成される値outputA。

私は他のフィールド(ない豆ID)にプロパティを渡すしようとした場合、それは正常に動作し、値が正しく注入されたので、それは、例のプロパティファイルのロードしないの問題ではありません。

<int:router method="determineTargetChannel" input-channel="${router.channel.outputA}"> 
    <beans:bean class="MyRouter"/> 
</int:router> 

答えて

2

これがSpringの仕組みです。 idは静的にしか宣言できません。それは '@ Bean'のJava設定がメソッド名に基づいているため、外部でメソッド名を指定することはできません。それはすでにコンパイルされているからです。

現在の機能を維持するには、デザインを再検討する必要があります。

関連する問題