Search in sources :

Example 6 with BeanLoadCostBeanFactory

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);
}
Also used : PropertyEditorRegistry(org.springframework.beans.PropertyEditorRegistry) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) PropertyEditorRegistrar(org.springframework.beans.PropertyEditorRegistrar) ClassArrayEditor(org.springframework.beans.propertyeditors.ClassArrayEditor) ClassEditor(org.springframework.beans.propertyeditors.ClassEditor) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) BeanLoadCostBeanFactory(com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory) SofaModuleApplicationContext(com.alipay.sofa.isle.spring.context.SofaModuleApplicationContext) QualifierAnnotationAutowireCandidateResolver(org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver) SofaModuleProperties(com.alipay.sofa.isle.spring.config.SofaModuleProperties)

Example 7 with BeanLoadCostBeanFactory

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);
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) DeploymentDescriptor(com.alipay.sofa.isle.deployment.DeploymentDescriptor) BeanLoadCostBeanFactory(com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) BeanLoadCostBeanFactory(com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory)

Example 8 with BeanLoadCostBeanFactory

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));
}
Also used : BeanLoadCostBeanFactory(com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory) Test(org.junit.Test)

Example 9 with BeanLoadCostBeanFactory

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);
}
Also used : PropertyEditorRegistry(org.springframework.beans.PropertyEditorRegistry) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) PropertyEditorRegistrar(org.springframework.beans.PropertyEditorRegistrar) ClassArrayEditor(org.springframework.beans.propertyeditors.ClassArrayEditor) ClassEditor(org.springframework.beans.propertyeditors.ClassEditor) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) BeanLoadCostBeanFactory(com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory) SofaModuleApplicationContext(com.alipay.sofa.isle.spring.context.SofaModuleApplicationContext) QualifierAnnotationAutowireCandidateResolver(org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver) SofaModuleProperties(com.alipay.sofa.isle.spring.config.SofaModuleProperties)

Example 10 with BeanLoadCostBeanFactory

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);
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ModuleStat(com.alipay.sofa.boot.startup.ModuleStat) BeanLoadCostBeanFactory(com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Aggregations

BeanLoadCostBeanFactory (com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory)10 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)6 DeploymentDescriptor (com.alipay.sofa.isle.deployment.DeploymentDescriptor)4 SofaModuleProperties (com.alipay.sofa.isle.spring.config.SofaModuleProperties)4 Test (org.junit.Test)4 BeanFactory (org.springframework.beans.factory.BeanFactory)4 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)4 BeanStat (com.alipay.sofa.boot.startup.BeanStat)2 ModuleStat (com.alipay.sofa.boot.startup.ModuleStat)2 ApplicationRuntimeModel (com.alipay.sofa.isle.ApplicationRuntimeModel)2 DeploymentDescriptorConfiguration (com.alipay.sofa.isle.deployment.DeploymentDescriptorConfiguration)2 DefaultModuleDeploymentValidator (com.alipay.sofa.isle.deployment.impl.DefaultModuleDeploymentValidator)2 FileDeploymentDescriptor (com.alipay.sofa.isle.deployment.impl.FileDeploymentDescriptor)2 SofaModuleApplicationContext (com.alipay.sofa.isle.spring.context.SofaModuleApplicationContext)2 File (java.io.File)2 Properties (java.util.Properties)2 PropertyEditorRegistrar (org.springframework.beans.PropertyEditorRegistrar)2 PropertyEditorRegistry (org.springframework.beans.PropertyEditorRegistry)2 QualifierAnnotationAutowireCandidateResolver (org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver)2 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)2