diff --git a/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/annotation/Reference.java b/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/annotation/Reference.java index 3220d9d23ad..b3a93d7b72d 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/annotation/Reference.java +++ b/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/annotation/Reference.java @@ -16,16 +16,12 @@ */ package com.alibaba.dubbo.config.annotation; -import com.alibaba.dubbo.common.Constants; - import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import static com.alibaba.dubbo.common.Constants.DEFAULT_PROTOCOL; - /** * Reference * @@ -119,10 +115,9 @@ /** * The communication protocol of Dubbo Service * - * @return the default value is "dubbo" - * @see Constants#DEFAULT_PROTOCOL + * @return the default value is "" * @since 2.6.6 */ - String protocol() default DEFAULT_PROTOCOL; + String protocol() default ""; } diff --git a/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ReferenceBeanBuilder.java b/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ReferenceBeanBuilder.java index 9b92d214169..27de58bf71e 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ReferenceBeanBuilder.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ReferenceBeanBuilder.java @@ -31,6 +31,7 @@ import java.beans.PropertyEditorSupport; import java.util.Map; +import static com.alibaba.dubbo.common.Constants.DEFAULT_PROTOCOL; import static com.alibaba.dubbo.config.spring.util.BeanFactoryUtils.getOptionalBean; import static com.alibaba.dubbo.config.spring.util.ObjectUtils.of; import static org.springframework.util.StringUtils.commaDelimitedListToStringArray; @@ -122,6 +123,22 @@ public void setAsText(String text) throws java.lang.IllegalArgumentException { // Bind annotation attributes dataBinder.bind(new AnnotationPropertyValuesAdapter(reference, applicationContext.getEnvironment(), IGNORE_FIELD_NAMES)); + setProtocolIfAbsent(reference, referenceBean); + } + + /** + * If ReferenceBean.protocol is empty and @Reference.protocol() is the default value, + * ReferenceBean.protocol is about to set the default protocol forcibly. + * + * @param reference {@link Reference} annotation + * @param referenceBean {@link ReferenceBean} Object + * @since 2.6.6 + */ + private void setProtocolIfAbsent(Reference reference, ReferenceBean referenceBean) { + if (!StringUtils.hasText(referenceBean.getProtocol()) + && DEFAULT_PROTOCOL.equals(reference.protocol())) { + referenceBean.setProtocol(DEFAULT_PROTOCOL); + } } diff --git a/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/api/HelloService.java b/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/api/HelloService.java index 1feb2bd5223..367ad9e37c8 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/api/HelloService.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/api/HelloService.java @@ -17,6 +17,13 @@ package com.alibaba.dubbo.config.spring.api; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.QueryParam; + +@Path("/hello-service") public interface HelloService { - String sayHello(String name); + + @GET + String sayHello(@QueryParam("name") String name); } diff --git a/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/multiple/consumer/MultipleProtocolsServiceConsumer.java b/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/multiple/consumer/MultipleProtocolsServiceConsumer.java index 2eeb073b49c..5166db8f6f1 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/multiple/consumer/MultipleProtocolsServiceConsumer.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/multiple/consumer/MultipleProtocolsServiceConsumer.java @@ -41,11 +41,26 @@ static class ConsumerConfiguration { @Reference(version = "${hello.service.version}", protocol = "dubbo") private HelloService dubboHelloService; -// @Reference(version = "${hello.service.version}", protocol = "rest") -// private HelloService restHelloService; + @Reference(version = "${hello.service.version}", protocol = "rest") + private HelloService restHelloService; + +// @Bean +// public ReferenceBean restReferenceBean(@Value("${hello.service.version}") String version) { +// ReferenceBean referenceBean = new ReferenceBean(); +// referenceBean.setVersion(version); +// referenceBean.setProtocol("rest"); +// referenceBean.setInterface(HelloService.class); +// return referenceBean; +// } } +// @ImportResource("classpath:/META-INF/spring/dubbo-rest-consumer.xml") +// @Configuration +// static class ConsumerXMLConfiguration { +// } + + public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); @@ -54,7 +69,7 @@ public static void main(String[] args) { ConsumerConfiguration configuration = context.getBean(ConsumerConfiguration.class); System.out.println(configuration.dubboHelloService.sayHello("mercyblitz")); -// System.out.println(configuration.restHelloService.sayHello("mercyblitz")); + System.out.println(configuration.restHelloService.sayHello("mercyblitz")); context.close(); } diff --git a/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/multiple/provider/DefaultHelloService.java b/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/multiple/provider/DefaultHelloService.java index 60c1dea0201..43a7ced1cfe 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/multiple/provider/DefaultHelloService.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/multiple/provider/DefaultHelloService.java @@ -20,17 +20,11 @@ import com.alibaba.dubbo.config.spring.api.HelloService; import com.alibaba.dubbo.rpc.RpcContext; -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.QueryParam; - -@Service(version = "${hello.service.version}", protocol = {"dubbo"}) -@Path("/hello-service") +@Service(version = "${hello.service.version}", protocol = {"dubbo", "rest"}) public class DefaultHelloService implements HelloService { @Override - @GET - public String sayHello(@QueryParam("name") String name) { + public String sayHello(String name) { return String.format("[%s] Hello , %s", RpcContext.getContext().getUrl(), name); } } diff --git a/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/multiple/provider/MultipleProtocolsServiceProvider.java b/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/multiple/provider/MultipleProtocolsServiceProvider.java index 8e3981a499f..e55d70acea2 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/multiple/provider/MultipleProtocolsServiceProvider.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/multiple/provider/MultipleProtocolsServiceProvider.java @@ -16,7 +16,6 @@ */ package com.alibaba.dubbo.config.spring.beans.factory.annotation.multiple.provider; -import com.alibaba.dubbo.config.spring.beans.factory.annotation.multiple.EmbeddedZooKeeper; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -35,8 +34,8 @@ public class MultipleProtocolsServiceProvider { public static void main(String[] args) throws IOException { - EmbeddedZooKeeper embeddedZooKeeper = new EmbeddedZooKeeper(2181, false); - embeddedZooKeeper.start(); +// EmbeddedZooKeeper embeddedZooKeeper = new EmbeddedZooKeeper(2181, false); +// embeddedZooKeeper.start(); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(MultipleProtocolsServiceProvider.class); @@ -46,6 +45,6 @@ public static void main(String[] args) throws IOException { System.in.read(); context.close(); - embeddedZooKeeper.stop(); +// embeddedZooKeeper.stop(); } } diff --git a/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/spring/dubbo-rest-consumer.xml b/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/spring/dubbo-rest-consumer.xml new file mode 100644 index 00000000000..c7db5ff57e6 --- /dev/null +++ b/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/spring/dubbo-rest-consumer.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + \ No newline at end of file