From f8d04276dfadd3460b77fe6cba13e93b84bdd900 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Wed, 24 Oct 2018 12:20:18 +0800 Subject: [PATCH 1/3] Add ability to turn off SPI auto injection, special support for generic Object type injection. --- .../common/extension/ExtensionLoader.java | 7 +++ .../dubbo/common/extension/Inject.java | 30 +++++++++ .../extensionloader/ExtensionLoaderTest.java | 12 ++++ .../extensionloader/injection/InjectExt.java | 27 ++++++++ .../injection/impl/InjectExtImpl.java | 61 +++++++++++++++++++ ...common.extensionloader.injection.InjectExt | 1 + .../extension/SpringExtensionFactory.java | 10 ++- 7 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/Inject.java create mode 100644 dubbo-common/src/test/java/com/alibaba/dubbo/common/extensionloader/injection/InjectExt.java create mode 100644 dubbo-common/src/test/java/com/alibaba/dubbo/common/extensionloader/injection/impl/InjectExtImpl.java create mode 100644 dubbo-common/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.injection.InjectExt diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/ExtensionLoader.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/ExtensionLoader.java index 7509a3d56db..771cc6b4363 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/ExtensionLoader.java +++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/ExtensionLoader.java @@ -515,6 +515,13 @@ private T injectExtension(T instance) { if (method.getName().startsWith("set") && method.getParameterTypes().length == 1 && Modifier.isPublic(method.getModifiers())) { + /** + * Check {@link Inject} to see if we need auto injection for this property + */ + Inject inject = method.getAnnotation(Inject.class); + if (inject != null && !inject.autoinject()) { + 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/main/java/com/alibaba/dubbo/common/extension/Inject.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/Inject.java new file mode 100644 index 00000000000..e23c3ee6f81 --- /dev/null +++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/Inject.java @@ -0,0 +1,30 @@ +/* + * 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 com.alibaba.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 Inject { + boolean autoinject() default true; +} diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/extensionloader/ExtensionLoaderTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/extensionloader/ExtensionLoaderTest.java index 3f7900c38b6..e6ef0281b7f 100644 --- a/dubbo-common/src/test/java/com/alibaba/dubbo/common/extensionloader/ExtensionLoaderTest.java +++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/extensionloader/ExtensionLoaderTest.java @@ -46,6 +46,8 @@ import com.alibaba.dubbo.common.extensionloader.ext8_add.impl.AddExt4_ManualAdaptive; import com.alibaba.dubbo.common.extensionloader.ext9_empty.Ext9Empty; import com.alibaba.dubbo.common.extensionloader.ext9_empty.impl.Ext9EmptyImpl; +import com.alibaba.dubbo.common.extensionloader.injection.InjectExt; +import com.alibaba.dubbo.common.extensionloader.injection.impl.InjectExtImpl; import junit.framework.Assert; import org.junit.Test; @@ -416,4 +418,14 @@ public void testLoadDefaultActivateExtension() throws Exception { 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; + org.junit.Assert.assertNotNull(injectExtImpl.getSimpleExt()); + org.junit.Assert.assertNull(injectExtImpl.getSimpleExt1()); + org.junit.Assert.assertNull(injectExtImpl.getGenericType()); + } + } \ No newline at end of file diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/extensionloader/injection/InjectExt.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/extensionloader/injection/InjectExt.java new file mode 100644 index 00000000000..a9d48dd6e59 --- /dev/null +++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/extensionloader/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 com.alibaba.dubbo.common.extensionloader.injection; + +import com.alibaba.dubbo.common.extension.SPI; + +/** + * + */ +@SPI("injection") +public interface InjectExt { + String echo(String msg); +} diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/extensionloader/injection/impl/InjectExtImpl.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/extensionloader/injection/impl/InjectExtImpl.java new file mode 100644 index 00000000000..ef95088fc56 --- /dev/null +++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/extensionloader/injection/impl/InjectExtImpl.java @@ -0,0 +1,61 @@ +/* + * 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 com.alibaba.dubbo.common.extensionloader.injection.impl; + + +import com.alibaba.dubbo.common.extension.Inject; +import com.alibaba.dubbo.common.extensionloader.ext1.SimpleExt; +import com.alibaba.dubbo.common.extensionloader.injection.InjectExt; + +public class InjectExtImpl implements InjectExt { + + private SimpleExt simpleExt; + + private SimpleExt simpleExt1; + + private Object genericType; + + public void setSimpleExt(SimpleExt simpleExt) { + this.simpleExt = simpleExt; + } + + @Inject(autoinject = false) + 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/com.alibaba.dubbo.common.extensionloader.injection.InjectExt b/dubbo-common/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.injection.InjectExt new file mode 100644 index 00000000000..687d18d5380 --- /dev/null +++ b/dubbo-common/src/test/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.extensionloader.injection.InjectExt @@ -0,0 +1 @@ +injection=com.alibaba.dubbo.common.extensionloader.injection.impl.InjectExtImpl \ No newline at end of file diff --git a/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/extension/SpringExtensionFactory.java b/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/extension/SpringExtensionFactory.java index 389515d1f1d..8673b3925fd 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/extension/SpringExtensionFactory.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/extension/SpringExtensionFactory.java @@ -60,13 +60,17 @@ public T getExtension(Class type, String name) { } } - 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); @@ -74,7 +78,7 @@ public T getExtension(Class type, String name) { } } - 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; } From 68424c00414ffe8286f579d6b9f354acaa8ba730 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Thu, 1 Nov 2018 16:42:06 +0800 Subject: [PATCH 2/3] Add ability to turn off SPI auto injection, special support for generic Object type injection. --- .../common/extension/{Inject.java => DisableInject.java} | 4 ++-- .../com/alibaba/dubbo/common/extension/ExtensionLoader.java | 6 +++--- .../extensionloader/injection/impl/InjectExtImpl.java | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) rename dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/{Inject.java => DisableInject.java} (94%) diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/Inject.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/DisableInject.java similarity index 94% rename from dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/Inject.java rename to dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/DisableInject.java index e23c3ee6f81..f775135542b 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/Inject.java +++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/DisableInject.java @@ -25,6 +25,6 @@ @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) -public @interface Inject { - boolean autoinject() default true; +public @interface DisableInject { + boolean disable() default true; } diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/ExtensionLoader.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/ExtensionLoader.java index 771cc6b4363..23a350263cf 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/ExtensionLoader.java +++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/ExtensionLoader.java @@ -516,10 +516,10 @@ private T injectExtension(T instance) { && method.getParameterTypes().length == 1 && Modifier.isPublic(method.getModifiers())) { /** - * Check {@link Inject} to see if we need auto injection for this property + * Check {@link DisableInject} to see if we need auto injection for this property */ - Inject inject = method.getAnnotation(Inject.class); - if (inject != null && !inject.autoinject()) { + DisableInject inject = method.getAnnotation(DisableInject.class); + if (inject != null && inject.disable()) { continue; } Class pt = method.getParameterTypes()[0]; diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/extensionloader/injection/impl/InjectExtImpl.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/extensionloader/injection/impl/InjectExtImpl.java index ef95088fc56..c2965931345 100644 --- a/dubbo-common/src/test/java/com/alibaba/dubbo/common/extensionloader/injection/impl/InjectExtImpl.java +++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/extensionloader/injection/impl/InjectExtImpl.java @@ -17,7 +17,7 @@ package com.alibaba.dubbo.common.extensionloader.injection.impl; -import com.alibaba.dubbo.common.extension.Inject; +import com.alibaba.dubbo.common.extension.DisableInject; import com.alibaba.dubbo.common.extensionloader.ext1.SimpleExt; import com.alibaba.dubbo.common.extensionloader.injection.InjectExt; @@ -33,7 +33,7 @@ public void setSimpleExt(SimpleExt simpleExt) { this.simpleExt = simpleExt; } - @Inject(autoinject = false) + @DisableInject public void setSimpleExt1(SimpleExt simpleExt1) { this.simpleExt1 = simpleExt1; } From d1a45ade0812f6b4f4e79eec9b565c574d52f133 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Thu, 1 Nov 2018 17:53:05 +0800 Subject: [PATCH 3/3] disable() is redundant in DisableInject annotation. --- .../java/com/alibaba/dubbo/common/extension/DisableInject.java | 1 - .../com/alibaba/dubbo/common/extension/ExtensionLoader.java | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/DisableInject.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/DisableInject.java index f775135542b..3f83834ca7f 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/DisableInject.java +++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/DisableInject.java @@ -26,5 +26,4 @@ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) public @interface DisableInject { - boolean disable() default true; } diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/ExtensionLoader.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/ExtensionLoader.java index 23a350263cf..94c59bb7f05 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/ExtensionLoader.java +++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/ExtensionLoader.java @@ -518,8 +518,7 @@ private T injectExtension(T instance) { /** * Check {@link DisableInject} to see if we need auto injection for this property */ - DisableInject inject = method.getAnnotation(DisableInject.class); - if (inject != null && inject.disable()) { + if (method.getAnnotation(DisableInject.class) != null) { continue; } Class pt = method.getParameterTypes()[0];