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

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

Introduction

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

Prototype

@Override
public void setId(String id) 

Source Link

Document

Set the unique id of this application context.

Usage

From source file:at.ac.univie.isc.asio.spring.SpringContextFactory.java

public AnnotationConfigApplicationContext named(final String label) {
    final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.setParent(root);/* ww w . j  a  v a  2s  .  c  o m*/
    context.setId(root.getId() + ":" + label + ":" + counter.getAndIncrement());
    context.setDisplayName(label);
    return context;
}

From source file:com.fitbur.jestify.junit.spring.IntegrationTestRunner.java

@Override
protected Object createTest() throws Exception {
    try {/*from   www.  jav a  2s .  c om*/
        TestClass testClass = getTestClass();
        Class<?> javaClass = testClass.getJavaClass();
        String name = javaClass.getSimpleName();

        TestDescriptor testDescriptor = testClassContexts.computeIfAbsent(javaClass, p -> {
            Object instance;
            try {
                instance = super.createTest();
                TestContext context = new TestContext(name, javaClass, instance);
                Set<Field> candidatesFields = of(javaClass.getDeclaredFields()).parallel()
                        .filter(f -> f.isAnnotationPresent(Cut.class)).collect(toSet());

                if (candidatesFields.isEmpty()) {
                    checkState(false, "Class under test not defined in %s. "
                            + "Please annotated a single field with @Cut.", name);
                } else if (candidatesFields.size() != 1) {
                    checkState(false, "Found more than one class under test in %s. "
                            + "Please annotated only a single field with @Cut.", name);
                }

                Field cutField = candidatesFields.iterator().next();
                ClassReader testReader = new ClassReader(javaClass.getName());
                ClassReader cutReader = new ClassReader(cutField.getType().getName());

                testReader.accept(new TesAnalyzer(context), EXPAND_FRAMES);
                cutReader.accept(new CutAnalyzer(context), EXPAND_FRAMES);

                return new TestDescriptor(instance, context);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });

        AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext();
        appContext.setAllowBeanDefinitionOverriding(true);
        appContext.setId(name);
        appContext.setDisplayName(name);

        Object instance = testDescriptor.getInstance();
        TestContext context = testDescriptor.getContext();
        IntegrationTestReifier reifier = new IntegrationTestReifier(appContext, instance);
        IntegrationTestCreator integrationTestCreator = new IntegrationTestCreator(context, reifier,
                appContext);
        integrationTestCreator.create();

        applicationContexts.put(javaClass, appContext);
        return instance;
    } catch (IllegalStateException e) {
        notifier.pleaseStop();
        throw e;
    }
}

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

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

    Object testInstance;/* w  ww .  j a 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;
}

From source file:org.zenoss.zep.impl.PluginServiceImpl.java

public void initializePlugins() {
    if (!initializedPlugins.compareAndSet(false, true)) {
        return;/* ww  w . j a  va 2 s.c  o  m*/
    }
    if (this.pluginClassLoader != null) {
        // Create a child ApplicationContext to use to load plug-ins with the plug-in class loader.
        final ClassLoader current = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(this.pluginClassLoader);
        try {
            AnnotationConfigApplicationContext pluginApplicationContext = new AnnotationConfigApplicationContext();
            pluginApplicationContext.setId("Plug-in Application Context");
            pluginApplicationContext.setClassLoader(this.pluginClassLoader);
            pluginApplicationContext.setParent(this.applicationContext);
            pluginApplicationContext.scan("org.zenoss", "com.zenoss", "zenpacks");
            pluginApplicationContext.refresh();
            loadPlugins(pluginApplicationContext);
        } catch (RuntimeException e) {
            logger.warn("Failed to configure plug-in application context", e);
            throw e;
        } finally {
            Thread.currentThread().setContextClassLoader(current);
        }
    } else {
        // Load plug-ins using the primary application context - no plug-ins were found on the classpath.
        loadPlugins(this.applicationContext);
    }
}

From source file:io.gravitee.gateway.handlers.api.ApiContextHandlerFactory.java

AbstractApplicationContext createApplicationContext(Api api) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.setParent(gatewayApplicationContext);
    context.setClassLoader(new ReactorHandlerClassLoader(gatewayApplicationContext.getClassLoader()));
    context.setEnvironment((ConfigurableEnvironment) gatewayApplicationContext.getEnvironment());

    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    configurer.setIgnoreUnresolvablePlaceholders(true);
    configurer.setEnvironment(gatewayApplicationContext.getEnvironment());
    context.addBeanFactoryPostProcessor(configurer);

    context.getBeanFactory().registerSingleton("api", api);
    context.register(ApiHandlerConfiguration.class);
    context.setId("context-api-" + api.getName());
    context.refresh();//  ww  w .  j a v a 2  s .  c  om

    return context;
}