You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2022/02/06 03:35:00 UTC
[dubbo] branch master updated: Bugfix - ExtensionLoader can not inject by type properly. (#9187)
This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/master by this push:
new 2341677 Bugfix - ExtensionLoader can not inject by type properly. (#9187)
2341677 is described below
commit 23416770677bbc1fac4203b954eedf82c2401381
Author: w4n92h3n <wa...@msn.com>
AuthorDate: Sun Feb 6 11:34:33 2022 +0800
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
---
.../apache/dubbo/common/config/Environment.java | 10 ++---
.../dubbo/common/extension/DisableInject.java | 3 +-
.../dubbo/common/extension/ExtensionLoader.java | 46 ++++++++++++++++------
.../extension/{DisableInject.java => Inject.java} | 14 ++++++-
.../apache/dubbo/config/context/ConfigManager.java | 12 +++---
.../extension/injection/impl/InjectExtImpl.java | 4 +-
.../ZookeeperDynamicConfigurationFactory.java | 4 +-
.../zookeeper/ZookeeperMetadataReportFactory.java | 4 +-
.../zookeeper/ZookeeperRegistryFactory.java | 4 +-
9 files changed, 69 insertions(+), 32 deletions(-)
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 18c6eef..f7734f4 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 @@ package org.apache.dubbo.common.config;
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 class Environment extends LifecycleAdapter implements FrameworkExt {
this.appExternalConfiguration.setProperties(appExternalConfigurationMap);
}
- @DisableInject
+ @Inject(enable = false)
public void setExternalConfigMap(Map<String, String> externalConfiguration) {
if (externalConfiguration != null) {
this.externalConfigurationMap = externalConfiguration;
}
}
- @DisableInject
+ @Inject(enable = false)
public void setAppExternalConfigMap(Map<String, String> appExternalConfiguration) {
if (appExternalConfiguration != null) {
this.appExternalConfigurationMap = appExternalConfiguration;
@@ -179,7 +179,7 @@ public class Environment extends LifecycleAdapter implements FrameworkExt {
return configCenterFirst;
}
- @DisableInject
+ @Inject(enable = false)
public void setConfigCenterFirst(boolean configCenterFirst) {
this.configCenterFirst = configCenterFirst;
}
@@ -188,7 +188,7 @@ public class Environment extends LifecycleAdapter implements FrameworkExt {
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 577a28d..6c59f3c 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 @@ import java.lang.annotation.Target;
@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 359f2e5..1b9d93f 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 class ExtensionLoader<T> {
/**
* get the original type.
+ *
* @param name
* @return
*/
@@ -707,28 +708,39 @@ public class ExtensionLoader<T> {
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 @@ public class ExtensionLoader<T> {
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/DisableInject.java b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/Inject.java
similarity index 78%
copy from dubbo-common/src/main/java/org/apache/dubbo/common/extension/DisableInject.java
copy to dubbo-common/src/main/java/org/apache/dubbo/common/extension/Inject.java
index 577a28d..6ec89d8 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/Inject.java
@@ -22,8 +22,20 @@ 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 DisableInject {
+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 bfddf97..1e298ba 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 @@ package org.apache.dubbo.config.context;
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 class ConfigManager extends LifecycleAdapter implements FrameworkExt {
}
// ApplicationConfig correlative methods
- @DisableInject
+ @Inject(enable = false)
public void setApplication(ApplicationConfig application) {
addConfig(application, true);
}
@@ -92,7 +92,7 @@ public class ConfigManager extends LifecycleAdapter implements FrameworkExt {
// MonitorConfig correlative methods
- @DisableInject
+ @Inject(enable = false)
public void setMonitor(MonitorConfig monitor) {
addConfig(monitor, true);
}
@@ -102,7 +102,7 @@ public class ConfigManager extends LifecycleAdapter implements FrameworkExt {
}
// ModuleConfig correlative methods
- @DisableInject
+ @Inject(enable = false)
public void setModule(ModuleConfig module) {
addConfig(module, true);
}
@@ -111,7 +111,7 @@ public class ConfigManager extends LifecycleAdapter implements FrameworkExt {
return ofNullable(getConfig(getTagName(ModuleConfig.class)));
}
- @DisableInject
+ @Inject(enable = false)
public void setMetrics(MetricsConfig metrics) {
addConfig(metrics, true);
}
@@ -120,7 +120,7 @@ public class ConfigManager extends LifecycleAdapter implements FrameworkExt {
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 ccff954..4c8fcf0 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 class InjectExtImpl implements InjectExt {
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 6dcedd9..d3567a0 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 @@ package org.apache.dubbo.configcenter.support.zookeeper;
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 class ZookeeperDynamicConfigurationFactory extends AbstractDynamicConfigu
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 ee3e2d9..c564bfd 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 class ZookeeperMetadataReportFactory extends AbstractMetadataReportFactor
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 0308d73..72301ed 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 class ZookeeperRegistryFactory extends AbstractRegistryFactory {
*
* @param zookeeperTransporter
*/
- @DisableInject
+ @Inject(enable = false)
public void setZookeeperTransporter(ZookeeperTransporter zookeeperTransporter) {
this.zookeeperTransporter = zookeeperTransporter;
}