Example usage for org.springframework.context.annotation AnnotationConfigApplicationContext setAllowCircularReferences

List of usage examples for org.springframework.context.annotation AnnotationConfigApplicationContext setAllowCircularReferences

Introduction

In this page you can find the example usage for org.springframework.context.annotation AnnotationConfigApplicationContext setAllowCircularReferences.

Prototype

public void setAllowCircularReferences(boolean allowCircularReferences) 

Source Link

Document

Set whether to allow circular references between beans - and automatically try to resolve them.

Usage

From source file:com.fitbur.testify.integration.SpringIntegrationTest.java

@Override
protected Statement methodBlock(FrameworkMethod method) {
    TestClass testClass = getTestClass();
    Class<?> javaClass = testClass.getJavaClass();

    Object testInstance;/*from   w ww  .  ja v  a 2 s . co m*/

    try {
        testInstance = createTest();
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }

    TestContext testContext = getTestContext(javaClass);
    testContext.setTestInstance(testInstance);
    String testClassName = testContext.getTestClassName();

    AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext();
    appContext.setId(testClassName);
    appContext.setAllowBeanDefinitionOverriding(true);
    appContext.setAllowCircularReferences(false);

    serviceLocator = new SpringServiceLocator(appContext, serviceAnnotations);

    methodTestNeeds = new TestNeeds(testContext, method.getName(), NeedScope.METHOD);
    methodTestNeeds.init();

    methodTestNeedContainers = new TestNeedContainers(testContext, method.getName(), NeedScope.METHOD);
    methodTestNeedContainers.init();

    TestCaseInstance testCaseInstance = new TestCaseInstance(method.getName(), testInstance);
    serviceLocator.addConstant(testCaseInstance.getTestName(), testCaseInstance);

    SpringServicePostProcessor postProcessor = new SpringServicePostProcessor(serviceLocator, methodTestNeeds,
            methodTestNeedContainers, classTestNeeds, classTestNeedContainers);

    appContext.addBeanFactoryPostProcessor(postProcessor);

    IntegrationTestReifier reifier = new IntegrationTestReifier(testContext, serviceLocator, testInstance);
    IntegrationTestCreator creator = new IntegrationTestCreator(testContext, reifier, serviceLocator);

    if (testContext.getCutDescriptor() != null) {
        creator.cut();
    }

    Set<FieldDescriptor> real = testContext.getFieldDescriptors().values().parallelStream()
            .filter(p -> !p.getInstance().isPresent())
            .filter(p -> p.hasAnnotations(serviceAnnotations.getInjectors())).collect(toSet());

    creator.real(real);

    IntegrationTestVerifier verifier = new IntegrationTestVerifier(testContext, LOGGER);
    verifier.wiring();

    Statement statement = methodInvoker(method, testInstance);
    statement = possiblyExpectingExceptions(method, testInstance, statement);
    statement = withBefores(method, testInstance, statement);
    statement = withAfters(method, testInstance, statement);
    statement = withRules(method, testInstance, statement);

    return statement;
}