From 23416770677bbc1fac4203b954eedf82c2401381 Mon Sep 17 00:00:00 2001 From: w4n92h3n Date: Sun, 6 Feb 2022 11:34:33 +0800 Subject: [PATCH] Bugfix - ExtensionLoader can not inject by type properly. (#9187) * bugfix ExtensionLoader can not inject by type properly. refactor DisableInject Annotation to the generic Inject Annotation. * update Inject Annotation disabled to enable. * update Inject Annotation disabled to enable. * update Inject Annotation disabled to enable. * update Inject Annotation disabled to enable. * bugfix ExtensionLoader can not inject by type properly. refactor DisableInject Annotation to the generic Inject Annotation. * Compatible with @Inject & @DisableInject, and tag @DisableInject with @Deprecated * Compatible with @Inject & @DisableInject, and tag @DisableInject with @Deprecated --- .../dubbo/common/config/Environment.java | 10 ++-- .../dubbo/common/extension/DisableInject.java | 3 +- .../common/extension/ExtensionLoader.java | 46 ++++++++++++++----- .../apache/dubbo/common/extension/Inject.java | 41 +++++++++++++++++ .../dubbo/config/context/ConfigManager.java | 12 ++--- .../injection/impl/InjectExtImpl.java | 4 +- .../ZookeeperDynamicConfigurationFactory.java | 4 +- .../ZookeeperMetadataReportFactory.java | 4 +- .../zookeeper/ZookeeperRegistryFactory.java | 4 +- 9 files changed, 97 insertions(+), 31 deletions(-) create mode 100644 dubbo-common/src/main/java/org/apache/dubbo/common/extension/Inject.java diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/config/Environment.java b/dubbo-common/src/main/java/org/apache/dubbo/common/config/Environment.java index 18c6eef715a..f7734f4b5a3 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/config/Environment.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/config/Environment.java @@ -19,7 +19,7 @@ import org.apache.dubbo.common.config.configcenter.DynamicConfiguration; import org.apache.dubbo.common.context.FrameworkExt; import org.apache.dubbo.common.context.LifecycleAdapter; -import org.apache.dubbo.common.extension.DisableInject; +import org.apache.dubbo.common.extension.Inject; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.config.AbstractConfig; @@ -77,14 +77,14 @@ public void initialize() throws IllegalStateException { this.appExternalConfiguration.setProperties(appExternalConfigurationMap); } - @DisableInject + @Inject(enable = false) public void setExternalConfigMap(Map externalConfiguration) { if (externalConfiguration != null) { this.externalConfigurationMap = externalConfiguration; } } - @DisableInject + @Inject(enable = false) public void setAppExternalConfigMap(Map appExternalConfiguration) { if (appExternalConfiguration != null) { this.appExternalConfigurationMap = appExternalConfiguration; @@ -179,7 +179,7 @@ public boolean isConfigCenterFirst() { return configCenterFirst; } - @DisableInject + @Inject(enable = false) public void setConfigCenterFirst(boolean configCenterFirst) { this.configCenterFirst = configCenterFirst; } @@ -188,7 +188,7 @@ public Optional getDynamicConfiguration() { return Optional.ofNullable(dynamicConfiguration); } - @DisableInject + @Inject(enable = false) public void setDynamicConfiguration(DynamicConfiguration dynamicConfiguration) { this.dynamicConfiguration = dynamicConfiguration; } 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 index 577a28df6b4..6c59f3c7c75 100644 --- 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 @@ -25,5 +25,6 @@ @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) +@Deprecated public @interface DisableInject { -} +} \ No newline at end of file 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 359f2e502bb..1b9d93f67f1 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 @@ -452,6 +452,7 @@ public T getExtension(String name, boolean wrap) { /** * get the original type. + * * @param name * @return */ @@ -707,28 +708,39 @@ private T injectExtension(T instance) { if (!isSetter(method)) { continue; } - /** - * Check {@link DisableInject} to see if we need auto injection for this property + + /* + * Check {@link DisableInject} to see if we need autowire injection for this property */ if (method.getAnnotation(DisableInject.class) != null) { continue; } + Class pt = method.getParameterTypes()[0]; if (ReflectUtils.isPrimitives(pt)) { continue; } - try { - String property = getSetterProperty(method); - Object object = objectFactory.getExtension(pt, property); - if (object != null) { - method.invoke(instance, object); + /* + * Check {@link Inject} to see if we need auto-injection for this property + * {@link Inject#enable} == false will skip inject property phase + * {@link Inject#InjectType#ByName} default inject by name + */ + String property = getSetterProperty(method); + Inject inject = method.getAnnotation(Inject.class); + if (inject == null) { + injectValue(instance, method, pt, property); + } else { + if (!inject.enable()) { + continue; } - } catch (Exception e) { - logger.error("Failed to inject via method " + method.getName() - + " of interface " + type.getName() + ": " + e.getMessage(), e); - } + if (inject.type() == Inject.InjectType.ByType) { + injectValue(instance, method, pt, null); + } else { + injectValue(instance, method, pt, property); + } + } } } catch (Exception e) { logger.error(e.getMessage(), e); @@ -736,6 +748,18 @@ private T injectExtension(T instance) { return instance; } + private void injectValue(T instance, Method method, Class pt, String property) { + try { + Object object = objectFactory.getExtension(pt, property); + if (object != null) { + method.invoke(instance, object); + } + } catch (Exception e) { + logger.error("Failed to inject via method " + method.getName() + + " of interface " + type.getName() + ": " + e.getMessage(), e); + } + } + private void initExtension(T instance) { if (instance instanceof Lifecycle) { Lifecycle lifecycle = (Lifecycle) instance; diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/Inject.java b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/Inject.java new file mode 100644 index 00000000000..6ec89d87267 --- /dev/null +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/Inject.java @@ -0,0 +1,41 @@ +/* + * 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; + +import static org.apache.dubbo.common.extension.Inject.InjectType.ByName; + +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.METHOD}) +public @interface Inject { + // whether enable injection or not + boolean enable() default true; + + // inject type default by name injection + InjectType type() default ByName; + + enum InjectType{ + ByName, + ByType + } +} diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/context/ConfigManager.java b/dubbo-common/src/main/java/org/apache/dubbo/config/context/ConfigManager.java index bfddf97bb4c..1e298baa0bf 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/config/context/ConfigManager.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/config/context/ConfigManager.java @@ -18,7 +18,7 @@ import org.apache.dubbo.common.context.FrameworkExt; import org.apache.dubbo.common.context.LifecycleAdapter; -import org.apache.dubbo.common.extension.DisableInject; +import org.apache.dubbo.common.extension.Inject; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.CollectionUtils; @@ -77,7 +77,7 @@ public ConfigManager() { } // ApplicationConfig correlative methods - @DisableInject + @Inject(enable = false) public void setApplication(ApplicationConfig application) { addConfig(application, true); } @@ -92,7 +92,7 @@ public ApplicationConfig getApplicationOrElseThrow() { // MonitorConfig correlative methods - @DisableInject + @Inject(enable = false) public void setMonitor(MonitorConfig monitor) { addConfig(monitor, true); } @@ -102,7 +102,7 @@ public Optional getMonitor() { } // ModuleConfig correlative methods - @DisableInject + @Inject(enable = false) public void setModule(ModuleConfig module) { addConfig(module, true); } @@ -111,7 +111,7 @@ public Optional getModule() { return ofNullable(getConfig(getTagName(ModuleConfig.class))); } - @DisableInject + @Inject(enable = false) public void setMetrics(MetricsConfig metrics) { addConfig(metrics, true); } @@ -120,7 +120,7 @@ public Optional getMetrics() { return ofNullable(getConfig(getTagName(MetricsConfig.class))); } - @DisableInject + @Inject(enable = false) public void setSsl(SslConfig sslConfig) { addConfig(sslConfig, true); } 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 index ccff9547f53..4c8fcf0fef6 100644 --- 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 @@ -16,7 +16,7 @@ */ package org.apache.dubbo.common.extension.injection.impl; -import org.apache.dubbo.common.extension.DisableInject; +import org.apache.dubbo.common.extension.Inject; import org.apache.dubbo.common.extension.ext1.SimpleExt; import org.apache.dubbo.common.extension.injection.InjectExt; @@ -32,7 +32,7 @@ public void setSimpleExt(SimpleExt simpleExt) { this.simpleExt = simpleExt; } - @DisableInject + @Inject(enable = false) public void setSimpleExt1(SimpleExt simpleExt1) { this.simpleExt1 = simpleExt1; } diff --git a/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDynamicConfigurationFactory.java b/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDynamicConfigurationFactory.java index 6dcedd98ebf..d3567a0cb51 100644 --- a/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDynamicConfigurationFactory.java +++ b/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDynamicConfigurationFactory.java @@ -19,7 +19,7 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.config.configcenter.AbstractDynamicConfigurationFactory; import org.apache.dubbo.common.config.configcenter.DynamicConfiguration; -import org.apache.dubbo.common.extension.DisableInject; +import org.apache.dubbo.common.extension.Inject; import org.apache.dubbo.remoting.zookeeper.ZookeeperTransporter; /** @@ -33,7 +33,7 @@ public ZookeeperDynamicConfigurationFactory() { this.zookeeperTransporter = ZookeeperTransporter.getExtension(); } - @DisableInject + @Inject(enable = false) public void setZookeeperTransporter(ZookeeperTransporter zookeeperTransporter) { this.zookeeperTransporter = zookeeperTransporter; } diff --git a/dubbo-metadata/dubbo-metadata-report-zookeeper/src/main/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReportFactory.java b/dubbo-metadata/dubbo-metadata-report-zookeeper/src/main/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReportFactory.java index ee3e2d97a56..c564bfdb1d0 100644 --- a/dubbo-metadata/dubbo-metadata-report-zookeeper/src/main/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReportFactory.java +++ b/dubbo-metadata/dubbo-metadata-report-zookeeper/src/main/java/org/apache/dubbo/metadata/store/zookeeper/ZookeeperMetadataReportFactory.java @@ -17,7 +17,7 @@ package org.apache.dubbo.metadata.store.zookeeper; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.extension.DisableInject; +import org.apache.dubbo.common.extension.Inject; import org.apache.dubbo.metadata.report.MetadataReport; import org.apache.dubbo.metadata.report.support.AbstractMetadataReportFactory; import org.apache.dubbo.remoting.zookeeper.ZookeeperTransporter; @@ -33,7 +33,7 @@ public ZookeeperMetadataReportFactory() { this.zookeeperTransporter = ZookeeperTransporter.getExtension(); } - @DisableInject + @Inject(enable = false) public void setZookeeperTransporter(ZookeeperTransporter zookeeperTransporter) { this.zookeeperTransporter = zookeeperTransporter; } diff --git a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistryFactory.java b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistryFactory.java index 0308d73ab8c..72301ed9c6c 100644 --- a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistryFactory.java +++ b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistryFactory.java @@ -17,7 +17,7 @@ package org.apache.dubbo.registry.zookeeper; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.extension.DisableInject; +import org.apache.dubbo.common.extension.Inject; import org.apache.dubbo.registry.Registry; import org.apache.dubbo.registry.support.AbstractRegistryFactory; import org.apache.dubbo.remoting.zookeeper.ZookeeperTransporter; @@ -38,7 +38,7 @@ public ZookeeperRegistryFactory() { * * @param zookeeperTransporter */ - @DisableInject + @Inject(enable = false) public void setZookeeperTransporter(ZookeeperTransporter zookeeperTransporter) { this.zookeeperTransporter = zookeeperTransporter; }