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
This commit is contained in:
w4n92h3n 2022-02-06 11:34:33 +08:00 committed by GitHub
parent 7aa6ea2a01
commit 2341677067
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 97 additions and 31 deletions

View File

@ -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;
}

View File

@ -25,5 +25,6 @@ import java.lang.annotation.Target;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Deprecated
public @interface DisableInject {
}
}

View File

@ -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;

View File

@ -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
}
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}