【问题标题】:how to reach message endpoints when using @Bean configuration使用@Bean 配置时如何到达消息端点
【发布时间】:2015-04-08 04:38:17
【问题描述】:

当我使用 spring 文档中描述的配置时:

@Configuration
@EnableIntegration
public class MyFlowConfiguration {

    @Bean
    @InboundChannelAdapter(value = "inputChannel", poller = @Poller(fixedDelay = "1000"))
    public MessageSource<String> consoleSource() {
        return CharacterStreamReadingMessageSource.stdin();
    }

    @Bean
    @Transformer(inputChannel = "inputChannel", outputChannel = "httpChannel")
    public ObjectToMapTransformer toMapTransformer() {
        return new ObjectToMapTransformer();
    }

    @Bean
    @ServiceActivator(inputChannel = "httpChannel")
    public MessageHandler httpHandler() {
        HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler("http://foo/service");
        handler.setExpectedResponseType(String.class);
        handler.setOutputChannelName("outputChannel");
        return handler;
    }

    @Bean
    @ServiceActivator(inputChannel = "outputChannel")
    public LoggingHandler loggingHandler() {
        return new LoggingHandler("info");
    }

}

我可以使用什么样的 bean 定义名称来到达端点? 通过组件使用配置时,文档中提供了信息:

这些注解的处理创建了相同的 bean(AbstractEndpoints 和 MessageHandlers(或入站通道适配器的 MessageSources - 见下文)与类似的 xml 组件。bean 名称使用以下模式生成:[componentName].[methodName] .[decapitalizedAnnotationClassShortName] 用于 AbstractEndpoint 和带有附加 .handler (.source) 后缀的 MessageHandler (MessageSource) bean 的名称相同。MessageHandlers (MessageSources) 也符合第 8.2 节“消息历史记录”的跟踪条件。

但是这里怎么用呢?

【问题讨论】:

    标签: java spring spring-integration


    【解决方案1】:

    如果我理解正确,您希望将其中一些端点注入到您的服务中。不确定“为什么?”,但可以这样做(例如,httpHandler):

    @Autowire
    @Qualifier("myFlowConfiguration.httpHandler.serviceActivator")
    private AbstractEndpoint httpEndpoint;
    

    根据上述规则:

    • myFlowConfiguration - 包含 @ServiceActivator 方法的类的 bean 名称。在你的情况下,它是你的@Configuration

    • httpHandler 方法名与@ServiceActivator

    • serviceActivator - @ServiceActivator 的非大写名称。

    清楚吗?

    更新

    我不使用xml上下文,只使用基于java的配置,所以答案是@Import

    好的,谢谢。那是一个答案。任何@Import @Configuration 都存在具有完全限定类名的bean 名称,包括包(来自ConfigurationClassPostProcessor):

    /* using fully qualified class names as default bean names */
     private BeanNameGenerator importBeanNameGenerator = new   AnnotationBeanNameGenerator() {
        @Override
        protected String buildDefaultBeanName(BeanDefinition definition) {
            return definition.getBeanClassName();
        }
    };
    

    因此,如果我们要使用来自 unnamed 类的端点的引用,我们必须牢记这一点。

    当然,为了简化您的生活,您只需将name 添加到您的MyFlowConfiguration

    @Configuration("myFlowConfiguration")
    @EnableIntegration
    public class MyFlowConfiguration {
    

    【讨论】:

    • 我已经按照你写的方式试过了。实际上我需要通过控制总线使用它(例如@'myFlowConfiguration.httpHandler.serviceActivator'.start())。所以首先我尝试只做 context.getBean("myFlowConfiguration.httpHandler.serviceActivator") 并得到 NoSuchBeanDefinition :( 所以它不起作用
    • 好。你介意在调试context.getBeansOfType(EventDrivenConsumer.class) 中检查这个并查看哪个 bean 名称具有该httpHandler() 的端点吗?
    • 非常感谢您的建议,真的很有帮助
    • 然后呢?您的端点的名称是什么?我们的描述可能不正确。所以,它应该被修复。感谢您指出这一点!
    • 它与 package.MyFlowConfiguration.httpHandler.serviceActivator 映射,当使用带有 @MessageEndpoint 注释的组件时,它完全按照文档中的说明进行映射,您在此处回复
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    相关资源
    最近更新 更多