Spring integration Spring集成&x27;Dispatcher没有订阅服务器';当在init方法中使用通道时

Spring integration Spring集成&x27;Dispatcher没有订阅服务器';当在init方法中使用通道时,spring-integration,Spring Integration,试图在Springbean的init方法中将消息发布到通道时,获取“Dispatcher has no subscribers”错误。请看下面的例子: applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSch

试图在Springbean的init方法中将消息发布到通道时,获取“Dispatcher has no subscribers”错误。请看下面的例子:

applicationContext.xml

<?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:p="http://www.springframework.org/schema/p"
    xmlns:rmi="http://www.springframework.org/schema/integration/rmi"
    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
        http://www.springframework.org/schema/integration/rmi
        http://www.springframework.org/schema/integration/rmi/spring-integration-rmi.xsd">

    <bean id="currencyService" class="com.demo.CurrencyService" init-method="init"/>

    <int:channel id="currencyChannel" />
    <int:channel id="currencyReplyChannel">
        <int:queue/>
    </int:channel>
    <rmi:outbound-gateway id="currencyServiceGateway"
        request-channel="currencyChannel" remote-channel="currencyServiceChannel"
        reply-channel="currencyReplyChannel" host="localhost" port="2197" />
</beans>

请告诉我第一种方法有什么问题。

在实例化bean之后,但在连接其余上下文之前(在本例中,是在RMI适配器订阅通道之前),会调用init/@PostConstruct方法

您需要等待上下文完全刷新

实现这一点的一种方法是实施

ApplicationListener<ContextRefreshedEvent>

该方法将在上下文完全连接后调用。

即使使用“@PostConstruct”而不是“init method”,也会出现同样的问题。非常感谢您的建议。这种方法奏效了。但我必须编写一个技术服务来实现ApplicationListener并启动所有必需的业务服务。如果我试图在业务服务本身上实现ApplicationListener,我将无法将此业务服务作为单元测试的依赖项注入。似乎这种行为是正确和干净的(使用一个init服务,而不是在所有必需的业务接口上实现ApplicationListener接口),但希望检查这是有意的还是我面临的错误;最好尽可能减少框架依赖性。当然,您可以在单元测试中模拟事件。当然,您也可以使用在第一次使用时而不是在init方法中运行init代码的技术。还有一种技术是实现SmartLifeCycle;将phase设置为Integer.MAX_值,bean的start()方法将是最后被调用的方法之一(如果isAutoStartup()返回true)。非常感谢您的帮助。
public CurrencyListBO getCurrencyList() {
    if(currencyListBO == null) {
        init();
    }
    return currencyListBO;
}
ApplicationListener<ContextRefreshedEvent>
public void onApplicationEvent(ContextRefreshedEvent event)