Search in sources :

Example 31 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project mapping-benchmark by arnaudroger.

the class RomaMapperFactory method getRowMapperService.

public static RowMapperService getRowMapperService() {
    GenericApplicationContext appContext = new GenericApplicationContext();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(appContext);
    reader.setValidating(false);
    reader.loadBeanDefinitions(new ClassPathResource("roma-context.xml"));
    appContext.refresh();
    return appContext.getBean(RowMapperService.class);
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 32 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project openmrs-core by openmrs.

the class StartModuleExecutionListener method prepareTestInstance.

/**
 * called before @BeforeTransaction methods
 *
 * @see org.springframework.test.context.support.AbstractTestExecutionListener#prepareTestInstance(org.springframework.test.context.TestContext)
 */
@Override
public void prepareTestInstance(TestContext testContext) throws Exception {
    StartModule startModuleAnnotation = testContext.getTestClass().getAnnotation(StartModule.class);
    // if the developer listed some modules with the @StartModule annotation on the class
    if (startModuleAnnotation != null) {
        if (!lastClassRun.equals(testContext.getTestClass().getSimpleName())) {
            // mark this with our class so that the services are only restarted once
            lastClassRun = testContext.getTestClass().getSimpleName();
            if (!Context.isSessionOpen())
                Context.openSession();
            ModuleUtil.shutdown();
            // load the omods that the dev defined for this class
            String modulesToLoad = StringUtils.join(startModuleAnnotation.value(), " ");
            Properties props = BaseContextSensitiveTest.runtimeProperties;
            props.setProperty(ModuleConstants.RUNTIMEPROPERTY_MODULE_LIST_TO_LOAD, modulesToLoad);
            try {
                ModuleUtil.startup(props);
            } catch (Exception e) {
                log.error("Error while starting modules: ", e);
                throw e;
            }
            Assert.assertTrue("Some of the modules did not start successfully for " + testContext.getTestClass().getSimpleName() + ". Only " + ModuleFactory.getStartedModules().size() + " modules started instead of " + startModuleAnnotation.value().length, startModuleAnnotation.value().length <= ModuleFactory.getStartedModules().size());
            /*
				 * Refresh spring so the Services are recreated (aka serializer gets put into the SerializationService)
				 * To do this, wrap the applicationContext from the testContext into a GenericApplicationContext, allowing
				 * loading beans from moduleApplicationContext into it and then calling ctx.refresh()
				 * This approach ensures that the application context remains consistent
				 */
            removeFilteredBeanDefinitions(testContext.getApplicationContext());
            GenericApplicationContext ctx = new GenericApplicationContext(testContext.getApplicationContext());
            XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
            Enumeration<URL> list = OpenmrsClassLoader.getInstance().getResources("moduleApplicationContext.xml");
            while (list.hasMoreElements()) {
                xmlReader.loadBeanDefinitions(new UrlResource(list.nextElement()));
            }
        }
    }
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) UrlResource(org.springframework.core.io.UrlResource) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) Properties(java.util.Properties) URL(java.net.URL)

Example 33 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project openmrs-core by openmrs.

the class StartModuleExecutionListener method removeFilteredBeanDefinitions.

/*
	 * Starting modules may require to remove beans definitions that were initially loaded.
	 */
protected void removeFilteredBeanDefinitions(ApplicationContext context) {
    // first looking at a context loading the bean definitions "now"
    GenericApplicationContext ctx = new GenericApplicationContext();
    (new XmlBeanDefinitionReader(ctx)).loadBeanDefinitions("classpath:applicationContext-service.xml");
    Set<String> filteredBeanNames = new HashSet<>();
    for (String beanName : ctx.getBeanDefinitionNames()) {
        if (beanName.startsWith("openmrsProfile")) {
            filteredBeanNames.add(beanName);
        }
    }
    ctx.close();
    // then looking at the context as it loaded the bean definitions before the module(s) were started
    Set<String> originalBeanNames = new HashSet<>();
    for (String beanName : ((GenericApplicationContext) context).getBeanDefinitionNames()) {
        if (beanName.startsWith("openmrsProfile")) {
            originalBeanNames.add(beanName);
        }
    }
    // removing the bean definitions that have been filtered out by starting the module(s)
    for (String beanName : originalBeanNames) {
        if (!filteredBeanNames.contains(beanName)) {
            filteredDefinitions.put(beanName, ((GenericApplicationContext) context).getBeanDefinition(beanName));
            ((GenericApplicationContext) context).removeBeanDefinition(beanName);
        }
    }
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) HashSet(java.util.HashSet)

Example 34 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project com.revolsys.open by revolsys.

the class SpringUtil method getApplicationContext.

public static GenericApplicationContext getApplicationContext(final ClassLoader classLoader, final Resource... resources) {
    final GenericApplicationContext applicationContext = new GenericApplicationContext();
    applicationContext.setClassLoader(classLoader);
    AnnotationConfigUtils.registerAnnotationConfigProcessors(applicationContext, null);
    final AttributesBeanConfigurer attributesConfig = new AttributesBeanConfigurer(applicationContext);
    applicationContext.addBeanFactoryPostProcessor(attributesConfig);
    final XmlBeanDefinitionReader beanReader = new XmlBeanDefinitionReader(applicationContext);
    beanReader.setBeanClassLoader(classLoader);
    beanReader.loadBeanDefinitions(resources);
    applicationContext.refresh();
    return applicationContext;
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) AttributesBeanConfigurer(com.revolsys.spring.config.AttributesBeanConfigurer) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader)

Example 35 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project com.revolsys.open by revolsys.

the class ModuleImport method getApplicationContext.

protected GenericApplicationContext getApplicationContext(final BeanDefinitionRegistry parentRegistry) {
    if (this.applicationContext == null) {
        this.applicationContext = new GenericApplicationContext();
        if (parentRegistry instanceof ResourceLoader) {
            final ResourceLoader resourceLoader = (ResourceLoader) parentRegistry;
            final ClassLoader classLoader = resourceLoader.getClassLoader();
            this.applicationContext.setClassLoader(classLoader);
        }
        AnnotationConfigUtils.registerAnnotationConfigProcessors(this.applicationContext, null);
        final DefaultListableBeanFactory beanFactory = this.applicationContext.getDefaultListableBeanFactory();
        final BeanFactory parentBeanFactory = (BeanFactory) parentRegistry;
        for (final String beanName : parentRegistry.getBeanDefinitionNames()) {
            final BeanDefinition beanDefinition = parentRegistry.getBeanDefinition(beanName);
            final String beanClassName = beanDefinition.getBeanClassName();
            if (beanClassName != null) {
                if (beanClassName.equals(AttributeMap.class.getName())) {
                    registerTargetBeanDefinition(this.applicationContext, parentBeanFactory, beanName, beanName);
                    this.beanNamesNotToExport.add(beanName);
                } else if (beanClassName.equals(MapFactoryBean.class.getName())) {
                    final PropertyValue targetMapClass = beanDefinition.getPropertyValues().getPropertyValue("targetMapClass");
                    if (targetMapClass != null) {
                        final Object mapClass = targetMapClass.getValue();
                        if (AttributeMap.class.getName().equals(mapClass)) {
                            registerTargetBeanDefinition(this.applicationContext, parentBeanFactory, beanName, beanName);
                            this.beanNamesNotToExport.add(beanName);
                        }
                    }
                }
            }
        }
        beanFactory.addPropertyEditorRegistrar(this.resourceEditorRegistrar);
        final AttributesBeanConfigurer attributesConfig = new AttributesBeanConfigurer(this.applicationContext, this.parameters);
        this.applicationContext.addBeanFactoryPostProcessor(attributesConfig);
        for (final String beanName : this.importBeanNames) {
            registerTargetBeanDefinition(this.applicationContext, parentBeanFactory, beanName, beanName);
            this.beanNamesNotToExport.add(beanName);
        }
        for (final Entry<String, String> entry : this.importBeanAliases.entrySet()) {
            final String beanName = entry.getKey();
            final String aliasName = entry.getValue();
            registerTargetBeanDefinition(this.applicationContext, parentBeanFactory, beanName, aliasName);
            this.beanNamesNotToExport.add(aliasName);
        }
        final XmlBeanDefinitionReader beanReader = new XmlBeanDefinitionReader(this.applicationContext);
        for (final Resource resource : this.resources) {
            beanReader.loadBeanDefinitions(resource);
        }
        this.applicationContext.refresh();
    }
    return this.applicationContext;
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) AttributesBeanConfigurer(com.revolsys.spring.config.AttributesBeanConfigurer) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Resource(com.revolsys.spring.resource.Resource) PropertyValue(org.springframework.beans.PropertyValue) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) AttributeMap(com.revolsys.collection.map.AttributeMap) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory)

Aggregations

GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)378 Test (org.junit.jupiter.api.Test)193 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)106 Test (org.junit.Test)74 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)72 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)50 ClassPathResource (org.springframework.core.io.ClassPathResource)36 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)16 QueueChannel (org.springframework.integration.channel.QueueChannel)16 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)15 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)15 GenericMessage (org.springframework.messaging.support.GenericMessage)14 Properties (java.util.Properties)13 DefaultAdvisorAutoProxyCreator (org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator)13 File (java.io.File)12 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)12 Message (org.springframework.messaging.Message)12 InputStreamResource (org.springframework.core.io.InputStreamResource)11 MBeanExporter (org.springframework.jmx.export.MBeanExporter)11 BeforeEach (org.junit.jupiter.api.BeforeEach)10