使用@Bean 配置时如何到达消息端点
how to reach message endpoints when using @Bean configuration
当我使用 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 定义名称来到达端点?
通过组件使用配置时,文档中提供了信息:
The processing of these annotations creates the same beans (AbstractEndpoints and MessageHandlers (or MessageSources for the inbound channel adapter - see below) as with similar xml components. The bean names are generated with this pattern: [componentName].[methodName].[decapitalizedAnnotationClassShortName] for the AbstractEndpoint and the same name with an additional .handler (.source) suffix for the MessageHandler (MessageSource) bean. The MessageHandlers (MessageSources) are also eligible to be tracked by Section 8.2, “Message History”.
但是这里怎么用呢?
如果我理解正确的话,你想注入其中一些端点到你的服务中。不确定 "why?",但可以这样做(例如 httpHandler
):
@Autowire
@Qualifier("myFlowConfiguration.httpHandler.serviceActivator")
private AbstractEndpoint httpEndpoint;
根据上述规则:
myFlowConfiguration
- class 的 bean 名称,其中包含带有 @ServiceActivator
的方法。在你的情况下,它是你的 @Configuration
httpHandler
方法名带@ServiceActivator
serviceActivator
- @ServiceActivator
.
的小写名称
清楚了吗?
更新
I don't use xml context, only java based configuration, so answer is @Import
很好,谢谢。那是一个答案。任何 @Import
@Configuration
都带有具有完全限定 class 名称的 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
class 的端点引用,我们必须牢记这一点。
当然,为了简化您的生活,您可以在 MyFlowConfiguration
:
中添加一个 name
@Configuration("myFlowConfiguration")
@EnableIntegration
public class MyFlowConfiguration {
当我使用 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 定义名称来到达端点? 通过组件使用配置时,文档中提供了信息:
The processing of these annotations creates the same beans (AbstractEndpoints and MessageHandlers (or MessageSources for the inbound channel adapter - see below) as with similar xml components. The bean names are generated with this pattern: [componentName].[methodName].[decapitalizedAnnotationClassShortName] for the AbstractEndpoint and the same name with an additional .handler (.source) suffix for the MessageHandler (MessageSource) bean. The MessageHandlers (MessageSources) are also eligible to be tracked by Section 8.2, “Message History”.
但是这里怎么用呢?
如果我理解正确的话,你想注入其中一些端点到你的服务中。不确定 "why?",但可以这样做(例如 httpHandler
):
@Autowire
@Qualifier("myFlowConfiguration.httpHandler.serviceActivator")
private AbstractEndpoint httpEndpoint;
根据上述规则:
myFlowConfiguration
- class 的 bean 名称,其中包含带有@ServiceActivator
的方法。在你的情况下,它是你的@Configuration
httpHandler
方法名带@ServiceActivator
serviceActivator
-@ServiceActivator
. 的小写名称
清楚了吗?
更新
I don't use xml context, only java based configuration, so answer is
@Import
很好,谢谢。那是一个答案。任何 @Import
@Configuration
都带有具有完全限定 class 名称的 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
class 的端点引用,我们必须牢记这一点。
当然,为了简化您的生活,您可以在 MyFlowConfiguration
:
name
@Configuration("myFlowConfiguration")
@EnableIntegration
public class MyFlowConfiguration {