Permalink
Browse files

INT-2417 Alias Endpoints with Generated Names

Handlers for consumer endpoints get a generated name
derived from the endpoint class name.

It is useful (for example for autowiring in tests)
to give the handlers a well-known name.

If the endpoint has an ID attribute, the handler
now gets a bean name of "<ID>.handler".
  • Loading branch information...
1 parent b262012 commit 59d7432eefdf9ea664ebfabab52b5c39ae43f2a5 @garyrussell garyrussell committed May 17, 2012
@@ -40,6 +40,7 @@
*
* @author Mark Fisher
* @author Oleg Zhurakousky
+ * @author Gary Russell
*/
public abstract class AbstractConsumerEndpointParser extends AbstractBeanDefinitionParser {
@@ -88,7 +89,8 @@ protected final AbstractBeanDefinition parseInternal(Element element, ParserCont
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(ConsumerEndpointFactoryBean.class);
String handlerBeanName = BeanDefinitionReaderUtils.generateBeanName(handlerBeanDefinition, parserContext.getRegistry());
- parserContext.registerBeanComponent(new BeanComponentDefinition(handlerBeanDefinition, handlerBeanName));
+ String[] handlerAlias = IntegrationNamespaceUtils.generateAlias(element);
+ parserContext.registerBeanComponent(new BeanComponentDefinition(handlerBeanDefinition, handlerBeanName, handlerAlias));
builder.addPropertyReference("handler", handlerBeanName);
String inputChannelName = element.getAttribute(inputChannelAttributeName);
@@ -76,7 +76,8 @@ protected String parseAndRegisterConsumer(Element element, ParserContext parserC
definition.getPropertyValues().addPropertyValue(IntegrationNamespaceUtils.ORDER, order);
}
String beanName = BeanDefinitionReaderUtils.generateBeanName(definition, parserContext.getRegistry());
- parserContext.registerBeanComponent(new BeanComponentDefinition(definition, beanName));
+ String[] handlerAlias = IntegrationNamespaceUtils.generateAlias(element);
+ parserContext.registerBeanComponent(new BeanComponentDefinition(definition, beanName, handlerAlias));
return beanName;
}
@@ -13,6 +13,8 @@
package org.springframework.integration.config.xml;
+import static org.springframework.beans.factory.xml.AbstractBeanDefinitionParser.ID_ATTRIBUTE;
+
import java.util.List;
import org.springframework.beans.factory.config.BeanDefinition;
@@ -45,6 +47,8 @@
static final String METHOD_ATTRIBUTE = "method";
static final String ORDER = "order";
static final String EXPRESSION_ATTRIBUTE = "expression";
+ public static final String HANDLER_ALIAS_SUFFIX = ".handler";
+
/**
* Configures the provided bean definition builder with a property value corresponding to the attribute whose name
@@ -254,4 +258,13 @@ public static void configureHeaderMapper(Element element, BeanDefinitionBuilder
rootBuilder.addPropertyValue("headerMapper", headerMapperBuilder.getBeanDefinition());
}
}
+
+ public static String[] generateAlias(Element element) {
+ String[] handlerAlias = null;
+ String id = element.getAttribute(ID_ATTRIBUTE);
+ if (StringUtils.hasText(id)) {
+ handlerAlias = new String[] {id + HANDLER_ALIAS_SUFFIX};
+ }
+ return handlerAlias;
+ }
}
@@ -19,11 +19,9 @@
import static org.junit.Assert.assertEquals;
import org.junit.Test;
-
import org.junit.runner.RunWith;
-
-import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.integration.config.TestConsumer;
import org.springframework.integration.handler.MethodInvokingMessageHandler;
@@ -34,6 +32,7 @@
/**
* @author Mark Fisher
* @author Artem Bilan
+ * @author Gary Russell
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@@ -42,6 +41,15 @@
@Autowired
private ApplicationContext context;
+ @SuppressWarnings("unused") // testing auto wiring only
+ @Autowired
+ @Qualifier("org.springframework.integration.handler.MethodInvokingMessageHandler#0")
+ private MethodInvokingMessageHandler adapterByGeneratedName;
+
+ @SuppressWarnings("unused") // testing auto wiring only
+ @Autowired
+ @Qualifier("adapter.handler")
+ private MethodInvokingMessageHandler adapterByAlias;
@Test
public void checkConfig() {
@@ -7,7 +7,7 @@
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd">
- <service-activator input-channel="literalExpressionInput" expression="'foo'"/>
+ <service-activator id="testAlias" input-channel="literalExpressionInput" expression="'foo'"/>
<service-activator input-channel="beanAsTargetInput" expression="@testBean.caps(payload)"/>
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2010 the original author or authors.
+ * Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,13 +22,16 @@
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.core.MessagingTemplate;
+import org.springframework.integration.handler.ServiceActivatingHandler;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
+ * @author Gary Russell
* @since 2.0
*/
@ContextConfiguration
@@ -53,6 +56,15 @@
@Autowired
private MessageChannel multipleArgsFromPayloadInput;
+ @SuppressWarnings("unused") // testing auto wiring only
+ @Autowired
+ @Qualifier("org.springframework.integration.config.ServiceActivatorFactoryBean#0")
+ private ServiceActivatingHandler testAliasByGeneratedName;
+
+ @SuppressWarnings("unused") // testing auto wiring only
+ @Autowired
+ @Qualifier("testAlias.handler")
+ private ServiceActivatingHandler testAlias;
@Test
public void literalExpression() {
@@ -84,15 +84,15 @@
TcpReceivingChannelAdapter tcpIn;
@Autowired
- @Qualifier(value="org.springframework.integration.ip.udp.UnicastSendingMessageHandler#0")
+ @Qualifier(value="testOutUdp.handler")
UnicastSendingMessageHandler udpOut;
@Autowired
- @Qualifier(value="org.springframework.integration.ip.udp.MulticastSendingMessageHandler#0")
+ @Qualifier(value="testOutUdpiMulticast.handler")
MulticastSendingMessageHandler udpOutMulticast;
@Autowired
- @Qualifier(value="org.springframework.integration.ip.tcp.TcpSendingMessageHandler#0")
+ @Qualifier(value="testOutTcpNio.handler")
TcpSendingMessageHandler tcpOut;
@Autowired
@@ -107,9 +107,14 @@
TcpInboundGateway tcpInboundGateway2;
@Autowired
- @Qualifier(value="org.springframework.integration.ip.tcp.TcpOutboundGateway#0")
+ @Qualifier(value="outGateway.handler")
TcpOutboundGateway tcpOutboundGateway;
+ // verify we can still inject by generated name
+ @Autowired
+ @Qualifier(value="org.springframework.integration.ip.tcp.TcpOutboundGateway#0")
+ TcpOutboundGateway tcpOutboundGatewayByGeneratedName;
+
@Autowired
EventDrivenConsumer outGateway;
@@ -160,11 +165,11 @@
AbstractConnectionFactory cfS3;
@Autowired
- @Qualifier(value="org.springframework.integration.ip.tcp.TcpSendingMessageHandler#1")
+ @Qualifier(value="tcpNewOut1.handler")
TcpSendingMessageHandler tcpNewOut1;
@Autowired
- @Qualifier(value="org.springframework.integration.ip.tcp.TcpSendingMessageHandler#2")
+ @Qualifier(value="tcpNewOut2.handler")
TcpSendingMessageHandler tcpNewOut2;
@Autowired
@@ -192,7 +197,7 @@
TaskScheduler sched;
@Autowired
- @Qualifier(value="org.springframework.integration.ip.tcp.TcpSendingMessageHandler#3")
+ @Qualifier(value="tcpOutClientMode.handler")
TcpSendingMessageHandler tcpOutClientMode;
@Autowired

0 comments on commit 59d7432

Please sign in to comment.