在Spring中从属性文件传递bean ID

在Spring中从属性文件传递bean ID,spring,spring-integration,Spring,Spring Integration,我有一个springboot应用程序,它使用spring集成在多个组件之间路由tcp消息,从功能角度来看,它工作正常。我一直在尝试清理代码,并用属性文件中声明的常量替换多个硬编码值(通道名称): 实际示例包括多个通道和一个路由器: <int:channel id="input"> <int:channel id="outputA"/> <int:channel id="outputB"/> <int:channel id="outputC"/>

我有一个springboot应用程序,它使用spring集成在多个组件之间路由tcp消息,从功能角度来看,它工作正常。我一直在尝试清理代码,并用属性文件中声明的常量替换多个硬编码值(通道名称):

实际示例包括多个通道和一个路由器:

<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>
我不想让通道ID硬编码在两个地方,既在XML文件中,也在Router类(或任何其他可以将它们作为常量保存的类)中

因此我尝试了以下方法:

1) 将ID存储在属性YML文件中:

router:
    channel:
        outputA: outputA
        outputB: outputB
        outputC: outputC
<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>
2) 更新XML文件:

router:
    channel:
        outputA: outputA
        outputB: outputB
        outputC: outputC
<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>
问题是在Spring创建XML文件中声明的bean时,它没有解析属性值,因此它创建了一个ID为${router.channel.outputA}而不是实际值outputA的bean

这不是不加载属性文件的问题,因为如果我尝试将属性传递给其他字段(不是bean ID),它工作正常,并且正确地注入了值,例如:

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

这就是Spring的工作方式。
id
只能静态声明。这就是“@Bean”的Java配置基于方法名的方式,我们不能在外部指定方法名。只是因为它已经编译过了

您应该重新考虑您的设计,以保持当前的功能