Search in sources :

Example 1 with SofaModuleApplicationContext

use of com.alipay.sofa.isle.spring.context.SofaModuleApplicationContext 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 2 with SofaModuleApplicationContext

use of com.alipay.sofa.isle.spring.context.SofaModuleApplicationContext 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)

Aggregations

SofaModuleProperties (com.alipay.sofa.isle.spring.config.SofaModuleProperties)2 SofaModuleApplicationContext (com.alipay.sofa.isle.spring.context.SofaModuleApplicationContext)2 BeanLoadCostBeanFactory (com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory)2 PropertyEditorRegistrar (org.springframework.beans.PropertyEditorRegistrar)2 PropertyEditorRegistry (org.springframework.beans.PropertyEditorRegistry)2 QualifierAnnotationAutowireCandidateResolver (org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver)2 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)2 ClassArrayEditor (org.springframework.beans.propertyeditors.ClassArrayEditor)2 ClassEditor (org.springframework.beans.propertyeditors.ClassEditor)2 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)2