use of com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory in project sofa-boot by alipay.
the class DynamicSpringContextLoader method loadSpringContext.
@Override
public void loadSpringContext(DeploymentDescriptor deployment, ApplicationRuntimeModel application) throws Exception {
SofaModuleProperties sofaModuleProperties = rootApplicationContext.getBean(SofaModuleProperties.class);
BeanLoadCostBeanFactory beanFactory = new BeanLoadCostBeanFactory(sofaModuleProperties.getBeanLoadCost(), deployment.getModuleName());
beanFactory.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
GenericApplicationContext ctx = sofaModuleProperties.isPublishEventToParent() ? new GenericApplicationContext(beanFactory) : new SofaModuleApplicationContext(beanFactory);
ctx.setId(deployment.getModuleName());
String activeProfiles = sofaModuleProperties.getActiveProfiles();
if (StringUtils.hasText(activeProfiles)) {
String[] profiles = activeProfiles.split(SofaBootConstants.PROFILE_SEPARATOR);
ctx.getEnvironment().setActiveProfiles(profiles);
}
setUpParentSpringContext(ctx, deployment, application);
final ClassLoader moduleClassLoader = deployment.getClassLoader();
ctx.setClassLoader(moduleClassLoader);
CachedIntrospectionResults.acceptClassLoader(moduleClassLoader);
// set allowBeanDefinitionOverriding
ctx.setAllowBeanDefinitionOverriding(sofaModuleProperties.isAllowBeanDefinitionOverriding());
ctx.getBeanFactory().setBeanClassLoader(moduleClassLoader);
ctx.getBeanFactory().addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(Class.class, new ClassEditor(moduleClassLoader));
registry.registerCustomEditor(Class[].class, new ClassArrayEditor(moduleClassLoader));
}
});
deployment.setApplicationContext(ctx);
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(ctx);
beanDefinitionReader.setValidating(true);
beanDefinitionReader.setNamespaceAware(true);
beanDefinitionReader.setBeanClassLoader(deployment.getApplicationContext().getClassLoader());
beanDefinitionReader.setResourceLoader(ctx);
loadBeanDefinitions(deployment, beanDefinitionReader);
addPostProcessors(beanFactory);
}
use of com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory in project sofa-boot by alipay.
the class ModuleLogOutputStage method logInfoBeanCost.
private void logInfoBeanCost(StringBuilder stringBuilder, List<DeploymentDescriptor> deploys) {
long totalTime = 0;
long realStart = 0;
long realEnd = 0;
stringBuilder.append("\n").append("Spring bean load time cost list").append("(").append(deploys.size()).append(") >>>>>>>");
StringBuilder sb = new StringBuilder();
int size = deploys.size();
for (int i = 0; i < size; ++i) {
String prefix = (i == size - 1) ? SYMBOLIC2 : SYMBOLIC1;
String indexPrefix = (i == size - 1) ? EMPTY_INDEX_PREFIX : INDENT_PREFIX;
DeploymentDescriptor dd = deploys.get(i);
BeanFactory beanFactory = ((ConfigurableApplicationContext) dd.getApplicationContext()).getBeanFactory();
if (realStart == 0 || dd.getStartTime() < realStart) {
realStart = dd.getStartTime();
}
if (realEnd == 0 || (dd.getStartTime() + dd.getElapsedTime()) > realEnd) {
realEnd = dd.getStartTime() + dd.getElapsedTime();
}
totalTime += dd.getElapsedTime();
if (beanFactory instanceof BeanLoadCostBeanFactory) {
sb.append(prefix).append("[Module] ").append(dd.getName()).append(" [").append(dd.getElapsedTime()).append(" ms]\n");
sb.append(((BeanLoadCostBeanFactory) beanFactory).outputBeanStats(indexPrefix));
}
}
stringBuilder.append(" [totalTime = ").append(totalTime).append(" ms, realTime = ").append(realEnd - realStart).append(" ms]\n").append(sb);
}
use of com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory in project sofa-boot by alipay.
the class AsyncInitBeanDefinitionDecoratorTest method testIsleModule.
@Test
public void testIsleModule() {
String moduleName = "testModule";
BeanLoadCostBeanFactory beanFactory = new BeanLoadCostBeanFactory(10, moduleName);
Assert.assertTrue(AsyncInitBeanDefinitionDecorator.isBeanLoadCostBeanFactory(beanFactory.getClass()));
Assert.assertEquals(moduleName, AsyncInitBeanDefinitionDecorator.getModuleNameFromBeanFactory(beanFactory));
}
use of com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory in project sofa-boot by sofastack.
the class DynamicSpringContextLoader method loadSpringContext.
@Override
public void loadSpringContext(DeploymentDescriptor deployment, ApplicationRuntimeModel application) throws Exception {
SofaModuleProperties sofaModuleProperties = rootApplicationContext.getBean(SofaModuleProperties.class);
BeanLoadCostBeanFactory beanFactory = new BeanLoadCostBeanFactory(sofaModuleProperties.getBeanLoadCost(), deployment.getModuleName());
beanFactory.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
GenericApplicationContext ctx = sofaModuleProperties.isPublishEventToParent() ? new GenericApplicationContext(beanFactory) : new SofaModuleApplicationContext(beanFactory);
ctx.setId(deployment.getModuleName());
String activeProfiles = sofaModuleProperties.getActiveProfiles();
if (StringUtils.hasText(activeProfiles)) {
String[] profiles = activeProfiles.split(SofaBootConstants.PROFILE_SEPARATOR);
ctx.getEnvironment().setActiveProfiles(profiles);
}
setUpParentSpringContext(ctx, deployment, application);
final ClassLoader moduleClassLoader = deployment.getClassLoader();
ctx.setClassLoader(moduleClassLoader);
CachedIntrospectionResults.acceptClassLoader(moduleClassLoader);
// set allowBeanDefinitionOverriding
ctx.setAllowBeanDefinitionOverriding(sofaModuleProperties.isAllowBeanDefinitionOverriding());
ctx.getBeanFactory().setBeanClassLoader(moduleClassLoader);
ctx.getBeanFactory().addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(Class.class, new ClassEditor(moduleClassLoader));
registry.registerCustomEditor(Class[].class, new ClassArrayEditor(moduleClassLoader));
}
});
deployment.setApplicationContext(ctx);
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(ctx);
beanDefinitionReader.setValidating(true);
beanDefinitionReader.setNamespaceAware(true);
beanDefinitionReader.setBeanClassLoader(deployment.getApplicationContext().getClassLoader());
beanDefinitionReader.setResourceLoader(ctx);
loadBeanDefinitions(deployment, beanDefinitionReader);
addPostProcessors(beanFactory);
}
use of com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory in project sofa-boot by sofastack.
the class StartupSpringContextInstallStage method doRefreshSpringContext.
@Override
protected void doRefreshSpringContext(DeploymentDescriptor deployment, ApplicationRuntimeModel application) {
ModuleStat moduleStat = new ModuleStat();
moduleStat.setName(deployment.getModuleName());
moduleStat.setStartTime(System.currentTimeMillis());
super.doRefreshSpringContext(deployment, application);
moduleStat.setEndTime(System.currentTimeMillis());
moduleStat.setCost(moduleStat.getEndTime() - moduleStat.getStartTime());
moduleStat.setThreadName(Thread.currentThread().getName());
ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) deployment.getApplicationContext();
ConfigurableListableBeanFactory beanFactory = ctx.getBeanFactory();
if (beanFactory instanceof BeanLoadCostBeanFactory) {
moduleStat.setChildren(((BeanLoadCostBeanFactory) beanFactory).getBeanStats());
}
contextRefreshStageStat.addChild(moduleStat);
}
Aggregations