Bean属性' channelIdentifierMap'不可写或具有无效的setter方法

时间:2014-12-24 09:07:42

标签: java spring spring-integration

我正在使用Spring集成4.0,我尝试创建一个有两个消息通道的payloadTypeRouter对象 - 一个用于String有效负载,另一个用于Integer有效负载。我试图通过以下java代码来实现:

package MessageExamples;
import org.springframework.messaging.Message;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.router.PayloadTypeRouter;
public class Test {
public static void main(String[] args) {
QueueChannel q_channel1=new QueueChannel();
QueueChannel q_channel2=new QueueChannel();
ApplicationContext ctx= new ClassPathXmlApplicationContext("SpringIntegration.xml");
PayloadTypeRouter r= (PayloadTypeRouter) ctx.getBean("payloadTypeRouter");
}}

使用以下配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd">
    <bean id="payloadTypeRouter"
    class="org.springframework.integration.router.PayloadTypeRouter">
    <property name="channelIdentifierMap">
    <map>
    <entry key="java.lang.String" value-ref="stringChannel"/>
    <entry key="java.lang.Integer" value-ref="integerChannel"/>
    </map>
    </property>
    </bean>
    <int:channel id="stringChannel"/>
    <int:channel id="integerChannel"/>
    </beans>

当我尝试运行它时,我收到以下错误消息:

  

线程中的异常&#34; main&#34; org.springframework.beans.factory.BeanCreationException:创建名称为&#39; payloadTypeRouter&#39;的bean时出错。在类路径资源[SpringIntegration.xml]中定义:设置属性值时出错;嵌套异常是org.springframework.beans.NotWritablePropertyException:无效的属性&#39; channelIdentifierMap&#39; bean类[org.springframework.integration.router.PayloadTypeRouter]:Bean属性&#39; channelIdentifierMap&#39;不可写或具有无效的setter方法。

我注意到AbstractMessageRouter在Spring Integration 4.0版中没有包含map setter,尽管它在以前的版本中有一个。我该如何配置这种路由器?

2 个答案:

答案 0 :(得分:1)

我认为您不需要最后的频道定义。根据文档,您只需要:

<bean id="payloadTypeRouter"
      class="org.springframework.integration.router.PayloadTypeRouter">
    <property name="channelIdentifierMap">
        <map>
            <entry key="java.lang.String" value-ref="stringChannel"/>
            <entry key="java.lang.Integer" value-ref="integerChannel"/>
        </map>
    </property>
</bean>

或者,等同的定义:

<int:payload-type-router input-channel="routingChannel">
    <int:mapping type="java.lang.String" channel="stringChannel" />
    <int:mapping type="java.lang.Integer" channel="integerChannel" />
</int:payload-type-router>

答案 1 :(得分:0)

该属性已重命名为channelMapping several years ago(在2.1中);我打开了JIRA Issue来修复文档。

感谢您指出这一点。