Fix the problem of partial setXXX method injection of spi class (#10354)

* Fix the problem of partial setXXX method injection of spi class

* Update ExtensionLoader.java

* Add check
This commit is contained in:
灼华 2022-07-27 17:20:07 +08:00 committed by GitHub
parent 0e1c120870
commit 659249d07d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 55 additions and 38 deletions

View File

@ -112,6 +112,7 @@ public class Environment extends LifecycleAdapter implements ApplicationExt {
* @deprecated only for ut
*/
@Deprecated
@DisableInject
public void setLocalMigrationRule(String localMigrationRule) {
this.localMigrationRule = localMigrationRule;
}

View File

@ -132,21 +132,25 @@ public class ModuleEnvironment extends Environment implements ModuleExt {
}
@Override
@DisableInject
public void setLocalMigrationRule(String localMigrationRule) {
applicationDelegate.setLocalMigrationRule(localMigrationRule);
}
@Override
@DisableInject
public void setExternalConfigMap(Map<String, String> externalConfiguration) {
applicationDelegate.setExternalConfigMap(externalConfiguration);
}
@Override
@DisableInject
public void setAppExternalConfigMap(Map<String, String> appExternalConfiguration) {
applicationDelegate.setAppExternalConfigMap(appExternalConfiguration);
}
@Override
@DisableInject
public void setAppConfigMap(Map<String, String> appConfiguration) {
applicationDelegate.setAppConfigMap(appConfiguration);
}

View File

@ -41,6 +41,7 @@ import org.apache.dubbo.rpc.model.FrameworkModel;
import org.apache.dubbo.rpc.model.ModuleModel;
import org.apache.dubbo.rpc.model.ScopeModel;
import org.apache.dubbo.rpc.model.ScopeModelAccessor;
import org.apache.dubbo.rpc.model.ScopeModelAware;
import java.io.BufferedReader;
import java.io.IOException;
@ -134,6 +135,8 @@ public class ExtensionLoader<T> {
private static SoftReference<Map<java.net.URL,List<String>>> urlListMapCache = new SoftReference<>(new ConcurrentHashMap<>());
private static List<String> ignoredInjectMethodsDesc = getIgnoredInjectMethodsDesc();
/**
* Record all unacceptable exceptions when using SPI
*/
@ -187,6 +190,13 @@ public class ExtensionLoader<T> {
return asList(strategies);
}
private static List<String> getIgnoredInjectMethodsDesc() {
List<String> ignoreInjectMethodsDesc = new ArrayList<>();
Arrays.stream(ScopeModelAware.class.getMethods()).map(ReflectUtils::getDesc).forEach(ignoreInjectMethodsDesc::add);
Arrays.stream(ExtensionAccessorAware.class.getMethods()).map(ReflectUtils::getDesc).forEach(ignoreInjectMethodsDesc::add);
return ignoreInjectMethodsDesc;
}
ExtensionLoader(Class<?> type, ExtensionDirector extensionDirector, ScopeModel scopeModel) {
this.type = type;
this.extensionDirector = extensionDirector;
@ -835,11 +845,23 @@ public class ExtensionLoader<T> {
continue;
}
/**
* Check {@link DisableInject} to see if we need auto injection for this property
* Check {@link DisableInject} to see if we need auto-injection for this property
*/
if (method.isAnnotationPresent(DisableInject.class)) {
continue;
}
// When spiXXX implements ScopeModelAware, ExtensionAccessorAware,
// the setXXX of ScopeModelAware and ExtensionAccessorAware does not need to be injected
if (method.getDeclaringClass() == ScopeModelAware.class) {
continue;
}
if (instance instanceof ScopeModelAware || instance instanceof ExtensionAccessorAware) {
if (ignoredInjectMethodsDesc.contains(ReflectUtils.getDesc(method))) {
continue;
}
}
Class<?> pt = method.getParameterTypes()[0];
if (ReflectUtils.isPrimitives(pt)) {
continue;

View File

@ -16,6 +16,7 @@
*/
package org.apache.dubbo.config.context;
import org.apache.dubbo.common.context.ModuleExt;
import org.apache.dubbo.common.extension.DisableInject;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
@ -50,10 +51,12 @@ import static org.apache.dubbo.config.AbstractConfig.getTagName;
/**
* Manage configs of module
*/
public class ModuleConfigManager extends AbstractConfigManager {
public class ModuleConfigManager extends AbstractConfigManager implements ModuleExt {
private static final Logger logger = LoggerFactory.getLogger(ModuleConfigManager.class);
public static final String NAME = "moduleConfig";
private Map<String, AbstractInterfaceConfig> serviceConfigCache = new ConcurrentHashMap<>();
private final ConfigManager applicationConfigManager;

View File

@ -203,6 +203,9 @@ public class ApplicationModel extends ScopeModel {
LOGGER.info(getDesc() + " is created");
}
initialize();
Assert.notNull(getApplicationServiceRepository(), "ApplicationServiceRepository can not be null");
Assert.notNull(getApplicationConfigManager(), "ApplicationConfigManager can not be null");
Assert.assertTrue(getApplicationConfigManager().isInitialized(), "ApplicationConfigManager can not be initialized");
}
@Override

View File

@ -123,7 +123,6 @@ public class FrameworkModel extends ScopeModel {
// notify destroy and clean framework resources
// see org.apache.dubbo.config.deploy.FrameworkModelCleaner
notifyDestroy();
checkApplicationDestroy();
if (LOGGER.isInfoEnabled()) {
LOGGER.info(getDesc() + " is destroyed");

View File

@ -61,9 +61,9 @@ public class ModuleModel extends ScopeModel {
}
initialize();
Assert.notNull(serviceRepository, "ModuleServiceRepository can not be null");
Assert.notNull(moduleConfigManager, "ModuleConfigManager can not be null");
Assert.assertTrue(moduleConfigManager.isInitialized(), "ModuleConfigManager can not be initialized");
Assert.notNull(getServiceRepository(), "ModuleServiceRepository can not be null");
Assert.notNull(getConfigManager(), "ModuleConfigManager can not be null");
Assert.assertTrue(getConfigManager().isInitialized(), "ModuleConfigManager can not be initialized");
// notify application check state
ApplicationDeployer applicationDeployer = applicationModel.getDeployer();
@ -76,8 +76,6 @@ public class ModuleModel extends ScopeModel {
protected void initialize() {
super.initialize();
this.serviceRepository = new ModuleServiceRepository(this);
this.moduleConfigManager = new ModuleConfigManager(this);
this.moduleConfigManager.initialize();
initModuleExt();
@ -158,6 +156,10 @@ public class ModuleModel extends ScopeModel {
}
public ModuleConfigManager getConfigManager() {
if (moduleConfigManager == null) {
moduleConfigManager = (ModuleConfigManager) this.getExtensionLoader(ModuleExt.class)
.getExtension(ModuleConfigManager.NAME);
}
return moduleConfigManager;
}

View File

@ -242,7 +242,7 @@ public abstract class ScopeModel implements ExtensionAccessor {
}
/**
* @return the describe string of this scope model
* @return to describe string of this scope model
*/
public String getDesc() {
if (this.desc == null) {

View File

@ -1 +1,2 @@
moduleEnvironment=org.apache.dubbo.common.config.ModuleEnvironment
moduleConfig=org.apache.dubbo.config.context.ModuleConfigManager

View File

@ -37,10 +37,6 @@ public class ExtensionDirectorTest {
String testAppSrvName = "testAppSrv";
String testMdSrvName = "testMdSrv";
String testAppProviderName = "testAppProvider";
String testModuleProviderName = "testModuleProvider";
String testFrameworkProviderName = "testFrameworkProvider";
@Test
public void testInheritanceAndScope() {
@ -144,16 +140,16 @@ public class ExtensionDirectorTest {
@Test
public void testModelDataIsolation() {
//Model Tree
//frameworkModel1
// applicationModel11
// moduleModel111
// moduleModel112
// applicationModel12
// moduleModel121
//frameworkModel2
// applicationModel21
// moduleModel211
//Model Tree
//frameworkModel1
// applicationModel11
// moduleModel111
// moduleModel112
// applicationModel12
// moduleModel121
//frameworkModel2
// applicationModel21
// moduleModel211
FrameworkModel frameworkModel1 = new FrameworkModel();
ApplicationModel applicationModel11 = new ApplicationModel(frameworkModel1);

View File

@ -16,7 +16,6 @@
*/
package org.apache.dubbo.config.spring.extension;
import org.apache.dubbo.common.context.Lifecycle;
import org.apache.dubbo.common.extension.ExtensionAccessor;
import org.apache.dubbo.common.extension.ExtensionInjector;
import org.apache.dubbo.common.extension.SPI;
@ -29,7 +28,7 @@ import java.util.Arrays;
/**
* SpringExtensionInjector
*/
public class SpringExtensionInjector implements ExtensionInjector, Lifecycle {
public class SpringExtensionInjector implements ExtensionInjector {
private ApplicationContext context;
@ -91,18 +90,5 @@ public class SpringExtensionInjector implements ExtensionInjector, Lifecycle {
Arrays.toString(beanNamesForType));
}
return beanFactory.getBean(beanNamesForType[0], type);
}
@Override
public void initialize() throws IllegalStateException {
}
@Override
public void start() throws IllegalStateException {
// no op
}
@Override
public void destroy() {
}
}