尝试将Spring Integration xml配置转换为Java配置。这是xml配置: https://github.com/spring-projects/spring-integration-samples/blob/master/basic/tcp-client-server/src/main/resources/META-INF/spring/integration/tcpClientServerDemo-context.xml。 这个示例创建了一个客户端套接字和一个服务器套接字,并将消息从客户端发送到服务器...就是这样。
这是我创建的配置和Java类。我对SI的经验不足,无法知道是什么导致了以下异常。
@Configuration
@IntegrationComponentScan
@EnableIntegration
public class TcpConfig {
// CLIENT
@Bean
public TcpNetClientConnectionFactory tcpNetClientConnectionFactory() {
TcpNetClientConnectionFactory tcpNetClientConnectionFactory = new TcpNetClientConnectionFactory("localhost", 8080);
tcpNetClientConnectionFactory.setSingleUse(true);
tcpNetClientConnectionFactory.setSoTimeout(10_000);
return tcpNetClientConnectionFactory;
}
@Bean
public MessageChannel inputChannel() {
return new DirectChannel();
}
@Bean
public TcpOutboundGateway tcpOutboundGateway() {
TcpOutboundGateway tcpOutboundGateway = new TcpOutboundGateway();
tcpOutboundGateway.setConnectionFactory(tcpNetClientConnectionFactory());
tcpOutboundGateway.setRequestTimeout(10_000);
tcpOutboundGateway.setRemoteTimeout(10_000);
tcpOutboundGateway.setReplyChannel(clientBytes2StringChannel());
return tcpOutboundGateway;
}
@Bean
public MessageChannel clientBytes2StringChannel() {
return new DirectChannel();
}
@Bean
@ServiceActivator(inputChannel = "clientBytes2StringChannel", outputChannel="clientOutputChannel")
public ObjectToStringTransformer objectToStringTransformerClient() {
return new ObjectToStringTransformer();
}
@Bean
public MessageChannel clientOutputChannel() {
return new DirectChannel();
}
// SERVER
@Bean
public TcpNetServerConnectionFactory tcpNetServerConnectionFactory() {
return new TcpNetServerConnectionFactory(8080);
}
@Bean
public TcpInboundGateway tcpInboundGateway() {
TcpInboundGateway tcpInboundGateway = new TcpInboundGateway();
tcpInboundGateway.setConnectionFactory(tcpNetServerConnectionFactory());
tcpInboundGateway.setRequestChannel(serverBytes2StringChannel());
tcpInboundGateway.setErrorChannel(errorChannel());
return tcpInboundGateway;
}
@Bean
public MessageChannel serverBytes2StringChannel() {
return new DirectChannel();
}
@Bean
@ServiceActivator(inputChannel = "serverBytes2StringChannel", outputChannel = "toSA")
public ObjectToStringTransformer objectToStringTransformerServer() {
return new ObjectToStringTransformer();
}
@Bean
public MessageChannel toSA() {
return new DirectChannel();
}
@Bean
@ServiceActivator(inputChannel = "toSA")
public EchoService echoService() {
return new EchoService();
}
@Bean
public MessageChannel errorChannel() {
return new DirectChannel();
}
@Bean
@ServiceActivator(inputChannel = "errorChannel")
public Transformer transformer() {
ExpressionParser p = new SpelExpressionParser(new SpelParserConfiguration(true, true));
return new ExpressionEvaluatingTransformer(p.parseExpression("payload.failedMessage.payload + ':' + payload.cause.message"));
}
}
-
@MessagingGateway(name = "gw", defaultRequestChannel = "inputChannel", defaultReplyChannel = "clientOutputChannel")
public interface SimpleGateway {
String send(String text);
}
-
public class EchoService {
public String test(String input) {
if ("FAIL".equals(input)) {
throw new RuntimeException("Failure Demonstration");
}
return "echo:" + input;
}
}
-
// Just running this gives me the exception.
public class TcpApp {
public static void main(String[] args) {
new AnnotationConfigApplicationContext(TcpConfig.class);
}
}
-
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tcpConfig': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Found ambiguous parameter type [class java.lang.Void] for method match: [public void org.springframework.integration.context.IntegrationObjectSupport.setMessageBuilderFactory(org.springframework.integration.support.MessageBuilderFactory), public void org.springframework.integration.context.IntegrationObjectSupport.setBeanFactory(org.springframework.beans.factory.BeanFactory), public void org.springframework.integration.context.IntegrationObjectSupport.setComponentName(java.lang.String), public final void org.springframework.integration.context.IntegrationObjectSupport.setPrimaryExpression(org.springframework.expression.Expression), public void org.springframework.integration.context.IntegrationObjectSupport.setChannelResolver(org.springframework.messaging.core.DestinationResolver), public java.lang.String org.springframework.integration.context.IntegrationObjectSupport.getApplicationContextId(), public void org.springframework.integration.context.IntegrationObjectSupport.setApplicationContext(org.springframework.context.ApplicationContext) throws org.springframework.beans.BeansException]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:562)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
at com.connectplaza.prototype.tcp.TcpApp.main(TcpApp.java:8)
Caused by: java.lang.IllegalArgumentException: Found ambiguous parameter type [class java.lang.Void] for method match: [public void org.springframework.integration.context.IntegrationObjectSupport.setMessageBuilderFactory(org.springframework.integration.support.MessageBuilderFactory), public void org.springframework.integration.context.IntegrationObjectSupport.setBeanFactory(org.springframework.beans.factory.BeanFactory), public void org.springframework.integration.context.IntegrationObjectSupport.setComponentName(java.lang.String), public final void org.springframework.integration.context.IntegrationObjectSupport.setPrimaryExpression(org.springframework.expression.Expression), public void org.springframework.integration.context.IntegrationObjectSupport.setChannelResolver(org.springframework.messaging.core.DestinationResolver), public java.lang.String org.springframework.integration.context.IntegrationObjectSupport.getApplicationContextId(), public void org.springframework.integration.context.IntegrationObjectSupport.setApplicationContext(org.springframework.context.ApplicationContext) throws org.springframework.beans.BeansException]
at org.springframework.util.Assert.isNull(Assert.java:113)
at org.springframework.integration.util.MessagingMethodInvokerHelper.findHandlerMethodsForTarget(MessagingMethodInvokerHelper.java:499)
at org.springframework.integration.util.MessagingMethodInvokerHelper.<init>(MessagingMethodInvokerHelper.java:226)
at org.springframework.integration.util.MessagingMethodInvokerHelper.<init>(MessagingMethodInvokerHelper.java:149)
at org.springframework.integration.util.MessagingMethodInvokerHelper.<init>(MessagingMethodInvokerHelper.java:144)
at org.springframework.integration.handler.MethodInvokingMessageProcessor.<init>(MethodInvokingMessageProcessor.java:60)
at org.springframework.integration.handler.ServiceActivatingHandler.<init>(ServiceActivatingHandler.java:37)
at org.springframework.integration.config.annotation.ServiceActivatorAnnotationPostProcessor.createHandler(ServiceActivatorAnnotationPostProcessor.java:66)
at org.springframework.integration.config.annotation.AbstractMethodAnnotationPostProcessor.postProcess(AbstractMethodAnnotationPostProcessor.java:144)
at org.springframework.integration.config.annotation.MessagingAnnotationPostProcessor.processAnnotationTypeOnMethod(MessagingAnnotationPostProcessor.java:228)
at org.springframework.integration.config.annotation.MessagingAnnotationPostProcessor$1.doWith(MessagingAnnotationPostProcessor.java:200)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:530)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:537)
at org.springframework.integration.config.annotation.MessagingAnnotationPostProcessor.postProcessAfterInitialization(MessagingAnnotationPostProcessor.java:180)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:421)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1635)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
... 10 more
答案 0 :(得分:1)
每当您使用Transformer
进行消息处理时,都需要切换到@Transformer
注释。否则@ServiceActivator
无法确定在运行时调用哪种方法:https://docs.spring.io/spring-integration/reference/html/configuration.html#annotations_on_beans。例如:
@Bean
@Transformer(inputChannel = "clientBytes2StringChannel", outputChannel="clientOutputChannel")
public ObjectToStringTransformer objectToStringTransformerClient() {
return new ObjectToStringTransformer();
}