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);
}
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()));
}
}
}
}
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);
}
}
}
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;
}
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;
}
Aggregations