From 4557cc143e62986339ca2888976596ac1c9eaf6b Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Thu, 18 Mar 2010 10:04:44 +0000 Subject: [PATCH] INT-1041 only falling back to default service interface at initialization time rather than in the default constructor so that getObjectType() returns null instead of inconsistent type information --- .../gateway/GatewayProxyFactoryBean.java | 43 +++++++++++++------ .../gateway/GatewayProxyFactoryBeanTests.java | 17 ++++++++ .../integration/gateway/gatewayAutowiring.xml | 21 +++++++++ 3 files changed, 69 insertions(+), 12 deletions(-) create mode 100644 org.springframework.integration/src/test/java/org/springframework/integration/gateway/gatewayAutowiring.xml diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/gateway/GatewayProxyFactoryBean.java b/org.springframework.integration/src/main/java/org/springframework/integration/gateway/GatewayProxyFactoryBean.java index 916d75aab9..bf40ae9725 100644 --- a/org.springframework.integration/src/main/java/org/springframework/integration/gateway/GatewayProxyFactoryBean.java +++ b/org.springframework.integration/src/main/java/org/springframework/integration/gateway/GatewayProxyFactoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 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. @@ -73,14 +73,21 @@ public class GatewayProxyFactoryBean extends AbstractEndpoint implements Factory private Map methodToChannelMap; + /** - * Will initialize this Factory with he default instance of the 'gateway' interface - * {@link GenericSendAndRecieveGateway} which will be used by this proxy if - * 'service-interface' attribute is not set. + * Create a Factory whose service interface type can be configured by setter injection. + * If none is set, it will fall back to the default service interface type, + * {@link GenericSendAndRecieveGateway}, upon initialization. */ - public GatewayProxyFactoryBean(){ - this.serviceInterface = GenericSendAndRecieveGateway.class; + public GatewayProxyFactoryBean() { + // serviceInterface will be determined on demand later } + + public GatewayProxyFactoryBean(Class serviceInterface) { + this.serviceInterface = serviceInterface; + } + + /** * Set the interface class that the generated proxy should implement. * If none is provided explicitly, the default is MessageHandler. @@ -91,6 +98,7 @@ public class GatewayProxyFactoryBean extends AbstractEndpoint implements Factory this.serviceInterface = serviceInterface; } + /** * Set the default request channel. * @@ -148,24 +156,35 @@ public class GatewayProxyFactoryBean extends AbstractEndpoint implements Factory if (this.initialized) { return; } - Assert.notNull(this.serviceInterface, "'serviceInterface' must not be null"); - Method[] methods = this.serviceInterface.getDeclaredMethods(); + Class proxyInterface = this.determineServiceInterface(); + Method[] methods = proxyInterface.getDeclaredMethods(); for (Method method : methods) { MessagingGateway gateway = this.createGatewayForMethod(method); this.gatewayMap.put(method, gateway); } - this.serviceProxy = new ProxyFactory(this.serviceInterface, this).getProxy(this.beanClassLoader); + this.serviceProxy = new ProxyFactory(proxyInterface, this).getProxy(this.beanClassLoader); this.start(); this.initialized = true; } } - public Object getObject() throws Exception { - return this.serviceProxy; + private Class determineServiceInterface() { + if (this.serviceInterface == null) { + this.serviceInterface = GenericSendAndRecieveGateway.class; + } + return this.serviceInterface; } public Class getObjectType() { - return this.serviceInterface; + return (this.serviceInterface != null ? this.serviceInterface : null); + } + + public Object getObject() throws Exception { + if (this.serviceProxy == null) { + this.onInit(); + Assert.notNull(this.serviceProxy, "failed to initialize proxy"); + } + return this.serviceProxy; } public boolean isSingleton() { diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/gateway/GatewayProxyFactoryBeanTests.java b/org.springframework.integration/src/test/java/org/springframework/integration/gateway/GatewayProxyFactoryBeanTests.java index bde4ae60e5..1ac3c634a7 100644 --- a/org.springframework.integration/src/test/java/org/springframework/integration/gateway/GatewayProxyFactoryBeanTests.java +++ b/org.springframework.integration/src/test/java/org/springframework/integration/gateway/GatewayProxyFactoryBeanTests.java @@ -29,6 +29,7 @@ import java.util.concurrent.TimeUnit; import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.channel.DirectChannel; import org.springframework.integration.channel.PollableChannel; @@ -290,6 +291,11 @@ public class GatewayProxyFactoryBeanTests { assertEquals("testBridge", event3.getComponentName()); } + @Test + public void autowiredGateway() { + new ClassPathXmlApplicationContext("gatewayAutowiring.xml", GatewayProxyFactoryBeanTests.class); + } + public static void throwTestException() throws TestException { throw new TestException(); @@ -312,4 +318,15 @@ public class GatewayProxyFactoryBeanTests { static class TestException extends Exception { } + + public static class TestClient { + + private final TestService service; + + @Autowired + public TestClient(TestService service) { + this.service = service; + } + } + } diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/gateway/gatewayAutowiring.xml b/org.springframework.integration/src/test/java/org/springframework/integration/gateway/gatewayAutowiring.xml new file mode 100644 index 0000000000..b67629a3c8 --- /dev/null +++ b/org.springframework.integration/src/test/java/org/springframework/integration/gateway/gatewayAutowiring.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + +