You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by il...@apache.org on 2018/11/01 14:52:22 UTC

[incubator-dubbo] branch master updated: [Dubbo-2678][For Master] Add ability to turn off SPI auto injection, special support for Object type. (#2682)

This is an automated email from the ASF dual-hosted git repository.

iluo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new 3cf801b  [Dubbo-2678][For Master] Add ability to turn off SPI auto injection, special support for Object type.  (#2682)
3cf801b is described below

commit 3cf801bb9d45524ae1e4692e04aeda2bcb5050dd
Author: ken.lj <ke...@gmail.com>
AuthorDate: Thu Nov 1 22:52:15 2018 +0800

    [Dubbo-2678][For Master] Add ability to turn off SPI auto injection, special support for Object type.  (#2682)
    
    * Add ability to turn off SPI auto injection, special support for generic Object type injection.
    
    * Change Inject to AutoInject since it's main purpose is to turn off auto-injection.
    
    * disable is redundant in DisableInject annotation
---
 .../dubbo/common/extension/DisableInject.java      | 29 +++++++++++
 .../dubbo/common/extension/ExtensionLoader.java    |  6 +++
 .../common/extension/ExtensionLoaderTest.java      | 13 ++++-
 .../common/extension/injection/InjectExt.java      | 27 ++++++++++
 .../extension/injection/impl/InjectExtImpl.java    | 60 ++++++++++++++++++++++
 ...ache.dubbo.common.extension.injection.InjectExt |  1 +
 .../spring/extension/SpringExtensionFactory.java   | 13 +++--
 7 files changed, 144 insertions(+), 5 deletions(-)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/DisableInject.java b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/DisableInject.java
new file mode 100644
index 0000000..577a28d
--- /dev/null
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/DisableInject.java
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.common.extension;
+
+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;
+
+@Documented
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.TYPE, ElementType.METHOD})
+public @interface DisableInject {
+}
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionLoader.java b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionLoader.java
index 1d071a1..31f72e6 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionLoader.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionLoader.java
@@ -532,6 +532,12 @@ public class ExtensionLoader<T> {
                     if (method.getName().startsWith("set")
                             && method.getParameterTypes().length == 1
                             && Modifier.isPublic(method.getModifiers())) {
+                        /**
+                         * Check {@link DisableInject} to see if we need auto injection for this property
+                         */
+                        if (method.getAnnotation(DisableInject.class) != null) {
+                            continue;
+                        }
                         Class<?> pt = method.getParameterTypes()[0];
                         try {
                             String property = method.getName().length() > 3 ? method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4) : "";
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoaderTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoaderTest.java
index b141707..185296f 100644
--- a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoaderTest.java
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoaderTest.java
@@ -47,7 +47,8 @@ import org.apache.dubbo.common.extension.ext8_add.impl.AddExt3_ManualAdaptive;
 import org.apache.dubbo.common.extension.ext8_add.impl.AddExt4_ManualAdaptive;
 import org.apache.dubbo.common.extension.ext9_empty.Ext9Empty;
 import org.apache.dubbo.common.extension.ext9_empty.impl.Ext9EmptyImpl;
-
+import org.apache.dubbo.common.extension.injection.InjectExt;
+import org.apache.dubbo.common.extension.injection.impl.InjectExtImpl;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -425,4 +426,14 @@ public class ExtensionLoaderTest {
         Assert.assertTrue(list.get(1).getClass() == OrderActivateExtImpl1.class);
     }
 
+    @Test
+    public void testInjectExtension() {
+        // test default
+        InjectExt injectExt = ExtensionLoader.getExtensionLoader(InjectExt.class).getExtension("injection");
+        InjectExtImpl injectExtImpl = (InjectExtImpl) injectExt;
+        Assert.assertNotNull(injectExtImpl.getSimpleExt());
+        Assert.assertNull(injectExtImpl.getSimpleExt1());
+        Assert.assertNull(injectExtImpl.getGenericType());
+    }
+
 }
\ No newline at end of file
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/injection/InjectExt.java b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/injection/InjectExt.java
new file mode 100644
index 0000000..f2ffe41
--- /dev/null
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/injection/InjectExt.java
@@ -0,0 +1,27 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.common.extension.injection;
+
+import org.apache.dubbo.common.extension.SPI;
+
+/**
+ *
+ */
+@SPI("injection")
+public interface InjectExt {
+    String echo(String msg);
+}
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/injection/impl/InjectExtImpl.java b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/injection/impl/InjectExtImpl.java
new file mode 100644
index 0000000..ccff954
--- /dev/null
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/injection/impl/InjectExtImpl.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.common.extension.injection.impl;
+
+import org.apache.dubbo.common.extension.DisableInject;
+import org.apache.dubbo.common.extension.ext1.SimpleExt;
+import org.apache.dubbo.common.extension.injection.InjectExt;
+
+public class InjectExtImpl implements InjectExt {
+
+    private SimpleExt simpleExt;
+
+    private SimpleExt simpleExt1;
+
+    private Object genericType;
+
+    public void setSimpleExt(SimpleExt simpleExt) {
+        this.simpleExt = simpleExt;
+    }
+
+    @DisableInject
+    public void setSimpleExt1(SimpleExt simpleExt1) {
+        this.simpleExt1 = simpleExt1;
+    }
+
+    public void setGenericType(Object genericType) {
+        this.genericType = genericType;
+    }
+
+    @Override
+    public String echo(String msg) {
+        return null;
+    }
+
+    public SimpleExt getSimpleExt() {
+        return simpleExt;
+    }
+
+    public SimpleExt getSimpleExt1() {
+        return simpleExt1;
+    }
+
+    public Object getGenericType() {
+        return genericType;
+    }
+}
diff --git a/dubbo-common/src/test/resources/META-INF/dubbo/internal/org.apache.dubbo.common.extension.injection.InjectExt b/dubbo-common/src/test/resources/META-INF/dubbo/internal/org.apache.dubbo.common.extension.injection.InjectExt
new file mode 100644
index 0000000..b3d82a0
--- /dev/null
+++ b/dubbo-common/src/test/resources/META-INF/dubbo/internal/org.apache.dubbo.common.extension.injection.InjectExt
@@ -0,0 +1 @@
+injection=org.apache.dubbo.common.extension.injection.impl.InjectExtImpl
\ No newline at end of file
diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/extension/SpringExtensionFactory.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/extension/SpringExtensionFactory.java
index 56862f3..ea63a35 100644
--- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/extension/SpringExtensionFactory.java
+++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/extension/SpringExtensionFactory.java
@@ -16,7 +16,6 @@
  */
 package org.apache.dubbo.config.spring.extension;
 
-import java.util.Set;
 import org.apache.dubbo.common.extension.ExtensionFactory;
 import org.apache.dubbo.common.extension.SPI;
 import org.apache.dubbo.common.logger.Logger;
@@ -26,6 +25,8 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException;
 import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
 import org.springframework.context.ApplicationContext;
 
+import java.util.Set;
+
 /**
  * SpringExtensionFactory
  */
@@ -69,13 +70,17 @@ public class SpringExtensionFactory implements ExtensionFactory {
             }
         }
 
-        logger.warn("No spring extension(bean) named:" + name + ", try to find an extension(bean) of type " + type.getName());
+        logger.warn("No spring extension (bean) named:" + name + ", try to find an extension (bean) of type " + type.getName());
+
+        if (Object.class == type) {
+            return null;
+        }
 
         for (ApplicationContext context : contexts) {
             try {
                 return context.getBean(type);
             } catch (NoUniqueBeanDefinitionException multiBeanExe) {
-                throw multiBeanExe;
+                logger.warn("Find more than 1 spring extensions (beans) of type " + type.getName() + ", will stop auto injection. Please make sure you have specified the concrete parameter type and there's only one extension of that type.");
             } catch (NoSuchBeanDefinitionException noBeanExe) {
                 if (logger.isDebugEnabled()) {
                     logger.debug("Error when get spring extension(bean) for type:" + type.getName(), noBeanExe);
@@ -83,7 +88,7 @@ public class SpringExtensionFactory implements ExtensionFactory {
             }
         }
 
-        logger.warn("No spring extension(bean) named:" + name + ", type:" + type.getName() + " found, stop get bean.");
+        logger.warn("No spring extension (bean) named:" + name + ", type:" + type.getName() + " found, stop get bean.");
 
         return null;
     }