Skip to content

[Spring] Share application context #1848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Dec 21, 2019
11 changes: 3 additions & 8 deletions spring/src/main/java/io/cucumber/spring/FixBootstrapUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,19 @@

package io.cucumber.spring;

import java.util.Set;

import org.springframework.beans.BeanUtils;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.test.context.BootstrapContext;
import org.springframework.test.context.BootstrapWith;
import org.springframework.test.context.CacheAwareContextLoaderDelegate;
import org.springframework.test.context.TestContextBootstrapper;
import org.springframework.test.context.cache.ContextCache;
import org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate;
import org.springframework.test.context.cache.DefaultContextCache;
import org.springframework.test.context.support.DefaultBootstrapContext;
import org.springframework.util.ClassUtils;

import java.util.Set;

/**
* {@code BootstrapUtils} is a collection of utility methods to assist with
* bootstrapping the <em>Spring TestContext Framework</em>.
Expand All @@ -44,9 +42,6 @@
*/
abstract class FixBootstrapUtils {

private static ThreadLocal<ContextCache> contextCache =
ThreadLocal.withInitial(DefaultContextCache::new);

private static final String DEFAULT_TEST_CONTEXT_BOOTSTRAPPER_CLASS_NAME =
"org.springframework.test.context.support.DefaultTestContextBootstrapper";

Expand All @@ -57,7 +52,7 @@ abstract class FixBootstrapUtils {
"org.springframework.test.context.web.WebAppConfiguration";

static BootstrapContext createBootstrapContext(Class<?> testClass) {
CacheAwareContextLoaderDelegate contextLoader = new DefaultCacheAwareContextLoaderDelegate(contextCache.get());
CacheAwareContextLoaderDelegate contextLoader = new DefaultCacheAwareContextLoaderDelegate();
return new DefaultBootstrapContext(testClass, contextLoader);
}

Expand Down
12 changes: 8 additions & 4 deletions spring/src/main/java/io/cucumber/spring/SpringFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,10 @@ public void start() {
notifyContextManagerAboutTestClassStarted();
if (beanFactory == null || isNewContextCreated()) {
beanFactory = testContextManager.getBeanFactory();
for (Class<?> stepClass : stepClasses) {
registerStepClassBeanDefinition(beanFactory, stepClass);
synchronized (beanFactory) {
for (Class<?> stepClass : stepClasses) {
registerStepClassBeanDefinition(beanFactory, stepClass);
}
}
}
GlueCodeContext.getInstance().start();
Expand Down Expand Up @@ -249,8 +251,10 @@ private ConfigurableApplicationContext getContext() {

private void registerGlueCodeScope(ConfigurableApplicationContext context) {
do {
context.getBeanFactory().registerScope(SCOPE_CUCUMBER_GLUE, new GlueCodeScope());
context = (ConfigurableApplicationContext) context.getParent();
synchronized (context) {
context.getBeanFactory().registerScope(SCOPE_CUCUMBER_GLUE, new GlueCodeScope());
context = (ConfigurableApplicationContext) context.getParent();
}
} while (context != null);
}
}
Expand Down