Skip to content

Commit

Permalink
fix moving BeanLoadCostBeanFactory (#999)
Browse files Browse the repository at this point in the history
  • Loading branch information
alaneuler authored Jul 6, 2022
1 parent 1246404 commit cbb43ed
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.junit.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;

import com.alipay.sofa.boot.constant.SofaBootConstants;
import com.alipay.sofa.runtime.factory.BeanLoadCostBeanFactory;
import com.alipay.sofa.runtime.spring.parser.AsyncInitBeanDefinitionDecorator;

Expand All @@ -37,17 +36,14 @@ public void testIsleModule() {
BeanLoadCostBeanFactory beanFactory = new BeanLoadCostBeanFactory(10, moduleName);
Assert.assertTrue(AsyncInitBeanDefinitionDecorator.isBeanLoadCostBeanFactory(beanFactory
.getClass()));
Assert.assertEquals(moduleName,
AsyncInitBeanDefinitionDecorator.getModuleNameFromBeanFactory(beanFactory));
Assert.assertEquals(moduleName, beanFactory.getId());
}

@Test
public void testNoIsleModule() {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
Assert.assertFalse(AsyncInitBeanDefinitionDecorator.isBeanLoadCostBeanFactory(beanFactory
.getClass()));
Assert.assertEquals(SofaBootConstants.ROOT_APPLICATION_CONTEXT,
AsyncInitBeanDefinitionDecorator.getModuleNameFromBeanFactory(beanFactory));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import com.alipay.sofa.boot.error.ErrorCode;
import com.alipay.sofa.boot.util.BeanDefinitionUtil;
import com.alipay.sofa.runtime.api.annotation.SofaAsyncInit;
import com.alipay.sofa.runtime.factory.BeanLoadCostBeanFactory;
import com.alipay.sofa.runtime.log.SofaLogger;
import com.alipay.sofa.runtime.spring.async.AsyncInitBeanHolder;
import com.alipay.sofa.runtime.spring.parser.AsyncInitBeanDefinitionDecorator;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.beans.factory.BeanFactory;
Expand Down Expand Up @@ -198,8 +198,8 @@ public void setEnvironment(Environment environment) {
private String getModuleName(ApplicationContext applicationContext) {
BeanFactory beanFactory = ((AbstractApplicationContext) applicationContext)
.getBeanFactory();
if (AsyncInitBeanDefinitionDecorator.isBeanLoadCostBeanFactory(beanFactory.getClass())) {
return AsyncInitBeanDefinitionDecorator.getModuleNameFromBeanFactory(beanFactory);
if (beanFactory instanceof BeanLoadCostBeanFactory) {
return ((BeanLoadCostBeanFactory) beanFactory).getId();
}
return SofaBootConstants.ROOT_APPLICATION_CONTEXT;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.lang.reflect.Method;
import java.util.concurrent.CountDownLatch;

import com.alipay.sofa.runtime.factory.BeanLoadCostBeanFactory;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.ProxyFactory;
Expand All @@ -34,7 +35,6 @@
import com.alipay.sofa.runtime.log.SofaLogger;
import com.alipay.sofa.runtime.spring.async.AsyncInitBeanHolder;
import com.alipay.sofa.runtime.spring.async.AsyncTaskExecutor;
import com.alipay.sofa.runtime.spring.parser.AsyncInitBeanDefinitionDecorator;
import org.springframework.core.PriorityOrdered;

/**
Expand Down Expand Up @@ -76,8 +76,8 @@ public Object postProcessAfterInitialization(Object bean, String beanName)
public void afterPropertiesSet() {
ConfigurableBeanFactory beanFactory = ((AbstractApplicationContext) applicationContext)
.getBeanFactory();
if (AsyncInitBeanDefinitionDecorator.isBeanLoadCostBeanFactory(beanFactory.getClass())) {
moduleName = AsyncInitBeanDefinitionDecorator.getModuleNameFromBeanFactory(beanFactory);
if (beanFactory instanceof BeanLoadCostBeanFactory) {
moduleName = ((BeanLoadCostBeanFactory) beanFactory).getId();
} else {
moduleName = SofaBootConstants.ROOT_APPLICATION_CONTEXT;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.sofa.runtime.spring.parser;

import com.alipay.sofa.runtime.factory.BeanLoadCostBeanFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
Expand All @@ -37,10 +38,6 @@
*/
public class AsyncInitBeanDefinitionDecorator implements BeanDefinitionDecorator,
SofaBootTagNameSupport {

private static final String BEAN_LOAD_COST_FACTORY_CLASS = "com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory";
private static final String GET_MODULE_NAME_METHOD = "getModuleName";

@Override
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition,
ParserContext parserContext) {
Expand All @@ -65,38 +62,21 @@ private String getModuleName(ParserContext parserContext) {
BeanDefinitionRegistry registry = parserContext.getRegistry();
if (registry instanceof AbstractApplicationContext) {
BeanFactory beanFactory = ((AbstractApplicationContext) registry).getBeanFactory();
if (isBeanLoadCostBeanFactory(beanFactory.getClass())) {
return getModuleNameFromBeanFactory(beanFactory);
if (beanFactory instanceof BeanLoadCostBeanFactory) {
return ((BeanLoadCostBeanFactory) beanFactory).getId();
}
}

if (isBeanLoadCostBeanFactory(registry.getClass())) {
return getModuleNameFromBeanFactory(registry);
if (registry instanceof BeanLoadCostBeanFactory) {
return ((BeanLoadCostBeanFactory) registry).getId();
}

return SofaBootConstants.ROOT_APPLICATION_CONTEXT;
}

public static boolean isBeanLoadCostBeanFactory(Class factoryClass) {
if (factoryClass == null) {
return false;
}
if (BEAN_LOAD_COST_FACTORY_CLASS.equals(factoryClass.getName())) {
return true;
}

if (!Object.class.equals(factoryClass)) {
return isBeanLoadCostBeanFactory(factoryClass.getSuperclass());
}

return false;
}

public static String getModuleNameFromBeanFactory(Object factory) {
try {
return (String) factory.getClass().getMethod(GET_MODULE_NAME_METHOD).invoke(factory);
} catch (Throwable e) {
return SofaBootConstants.ROOT_APPLICATION_CONTEXT;
}
return BeanLoadCostBeanFactory.class.isAssignableFrom(factoryClass);
}
}

0 comments on commit cbb43ed

Please sign in to comment.