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

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

Introduction

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

Prototype

@Override
public void setParent(@Nullable ApplicationContext parent) 

Source Link

Document

Set the parent of this application context, also setting the parent of the internal BeanFactory accordingly.

Usage

From source file:com.kurento.kmf.spring.KurentoApplicationContextUtils.java

public static AnnotationConfigApplicationContext createKurentoHandlerServletApplicationContext(
        Class<?> servletClass, String servletName, ServletContext sc, String handlerClassName) {
    Assert.notNull(sc, "Cannot create Kurento ServletApplicationContext from null ServletContext");
    Assert.notNull(servletClass, "Cannot create KurentoServletApplicationContext from a null Servlet class");
    Assert.notNull(servletClass, "Cannot create KurentoServletApplicationContext from a null Hanlder class");

    if (childContexts == null) {
        childContexts = new ConcurrentHashMap<String, AnnotationConfigApplicationContext>();
    }/*from  ww w. jav  a 2s . c  om*/

    AnnotationConfigApplicationContext childContext = childContexts
            .get(servletClass.getName() + ":" + servletName);
    Assert.isNull(childContext, "Pre-existing context found associated to servlet class "
            + servletClass.getName() + " and servlet name " + servletName);

    childContext = new AnnotationConfigApplicationContext();
    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClassName(handlerClassName);
    childContext.registerBeanDefinition(handlerClassName, beanDefinition);
    if (!kurentoApplicationContextExists()) {
        createKurentoApplicationContext(sc);
    }
    childContext.setParent(getKurentoApplicationContext());
    childContext.refresh();
    childContexts.put(servletClass.getName(), childContext);

    return childContext;
}

From source file:org.springsource.investigation.ReproTests.java

@SuppressWarnings("unchecked")
@Test//  w ww.  j  a v  a 2  s .c o  m
public void repro() {
    ConfigurableApplicationContext parent = new GenericApplicationContext();
    parent.refresh();

    AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
    child.setParent(parent);
    child.refresh();

    ConfigurableEnvironment env = child.getBean(ConfigurableEnvironment.class);
    assertThat("UNKNOWN ENV", env,
            anyOf(sameInstance(parent.getEnvironment()), sameInstance(child.getEnvironment())));
    assertThat("EXPECTED CHILD CTX ENV", env, sameInstance(child.getEnvironment()));
}

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);
    context.setId(root.getId() + ":" + label + ":" + counter.getAndIncrement());
    context.setDisplayName(label);/* ww w. j a  v  a 2  s .c om*/
    return context;
}

From source file:org.exoplatform.container.spring.AnnotationConfigApplicationContextProvider.java

/**
 * {@inheritDoc}// w w w . j av  a 2s . c o  m
 */
public ApplicationContext getApplicationContext(ApplicationContext parent) {
    try {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.setParent(parent);
        for (String value : params.getValues()) {
            Class<?> clazz = ClassLoading.forName(value, AnnotationConfigApplicationContextProvider.class);
            ctx.register(clazz);
        }
        ctx.refresh();
        return ctx;
    } catch (Exception e) {
        throw new RuntimeException("Could not create the ApplicationContext", e);
    }
}

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();/*from   ww  w.j a v  a 2 s  . c  om*/

    return context;
}

From source file:shiver.me.timbers.spring.security.JwtSpringSecurityAdaptor.java

private void autowireThis(HttpSecurity http) {
    final ApplicationContext parent = http.getSharedObject(ApplicationContext.class);
    checkForJwtAnnotation(parent);//w  w  w .j a va2  s  . c o  m

    final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.setParent(parent);
    context.register(PropertySourcesPlaceholderConfigurer.class);
    context.register(getClass());
    context.refresh();
    context.getAutowireCapableBeanFactory().autowireBean(this);
}

From source file:org.twinkql.template.AbstractTwinkqlTemplateFactory.java

/**
 * Gets the twinkql template.//from ww  w.j  a v  a2  s. c om
 *
 * @return the twinkql template
 * @throws Exception the exception
 */
public TwinkqlTemplate getTwinkqlTemplate() {

    DefaultListableBeanFactory parentBeanFactory = new DefaultListableBeanFactory();

    parentBeanFactory.registerSingleton("twinkqlContext", this.getTwinkqlContext());

    GenericApplicationContext parentContext = new GenericApplicationContext(parentBeanFactory);

    parentContext.refresh();

    AnnotationConfigApplicationContext annotationConfigApplicationContext = this
            .decorateContext(new AnnotationConfigApplicationContext());
    annotationConfigApplicationContext.setParent(parentContext);
    annotationConfigApplicationContext.scan(PACKAGE_SCAN);
    annotationConfigApplicationContext.refresh();

    TwinkqlTemplate template = annotationConfigApplicationContext.getBean(TwinkqlTemplate.class);

    return template;
}

From source file:org.jacpfx.vertx.spring.SpringVerticleFactory.java

private Verticle createSpringVerticle(final Class<?> currentVerticleClass, ClassLoader classLoader) {
    final SpringVerticle annotation = currentVerticleClass.getAnnotation(SpringVerticle.class);
    final Class<?> springConfigClass = annotation.springConfig();

    // Create the parent context  
    final GenericApplicationContext genericApplicationContext = new GenericApplicationContext();
    genericApplicationContext.setClassLoader(classLoader);
    if (parentContext != null) {
        genericApplicationContext.setParent(parentContext);
    }//from w  w  w .j  ava2s .  com
    genericApplicationContext.refresh();
    genericApplicationContext.start();

    // 1. Create a new context for each verticle and use the specified spring configuration class if possible
    AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
    annotationConfigApplicationContext.setParent(genericApplicationContext);
    annotationConfigApplicationContext.register(SpringContextConfiguration.class, springConfigClass);

    // 2. Register a bean definition for this verticle
    annotationConfigApplicationContext.registerBeanDefinition(currentVerticleClass.getSimpleName(),
            new VerticleBeanDefinition(currentVerticleClass));

    // 3. Add a bean factory post processor to avoid configuration issues
    annotationConfigApplicationContext
            .addBeanFactoryPostProcessor(new SpringSingleVerticleConfiguration(currentVerticleClass));
    annotationConfigApplicationContext.refresh();
    annotationConfigApplicationContext.start();
    annotationConfigApplicationContext.registerShutdownHook();

    // 5. Return the verticle by fetching the bean from the context
    return (Verticle) annotationConfigApplicationContext.getBeanFactory()
            .getBean(currentVerticleClass.getSimpleName());
}

From source file:io.milton.servlet.SpringMiltonFilter.java

@SuppressWarnings("resource")
protected void initSpringApplicationContext(FilterConfig fc) {

    final WebApplicationContext rootContext = WebApplicationContextUtils
            .getWebApplicationContext(fc.getServletContext());

    StaticApplicationContext parent;//from w  w  w  .j  a  v  a  2  s . co m
    if (rootContext != null) {
        log.info("Found a root spring context, and using it");
        parent = new StaticApplicationContext(rootContext);
    } else {
        log.info("No root spring context");
        parent = new StaticApplicationContext();
    }

    final FilterConfigWrapper configWrapper = new FilterConfigWrapper(fc);
    parent.getBeanFactory().registerSingleton("config", configWrapper);
    parent.getBeanFactory().registerSingleton("servletContext", fc.getServletContext());
    File webRoot = new File(fc.getServletContext().getRealPath("/"));
    parent.getBeanFactory().registerSingleton("webRoot", webRoot);
    log.info("Registered root webapp path in: webroot=" + webRoot.getAbsolutePath());
    parent.refresh();

    final String configClass = fc.getInitParameter("contextConfigClass");
    final String sFiles = fc.getInitParameter("contextConfigLocation");

    ConfigurableApplicationContext ctx = null;
    if (StringUtils.isNotBlank(configClass)) {
        try {
            Class<?> clazz = Class.forName(configClass);
            final AnnotationConfigApplicationContext annotationCtx = new AnnotationConfigApplicationContext();
            annotationCtx.setParent(parent);
            annotationCtx.register(clazz);
            annotationCtx.refresh();
            ctx = annotationCtx;
        } catch (ClassNotFoundException e) {
            ctx = null;
            log.error("Unable to create a child context for Milton", e);
        }
    } else {
        String[] contextFiles;
        if (sFiles != null && sFiles.trim().length() > 0) {
            contextFiles = sFiles.split(" ");
        } else {
            contextFiles = new String[] { "applicationContext.xml" };
        }

        try {
            ctx = new ClassPathXmlApplicationContext(contextFiles, parent);
        } catch (BeansException e) {
            log.error("Unable to create a child context for Milton", e);
        }
    }

    if (ctx == null) {
        log.warn("No child context available, only using parent context");
        context = parent;
    } else {
        context = ctx;
    }

}

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

public void initializePlugins() {
    if (!initializedPlugins.compareAndSet(false, true)) {
        return;//from w  w w.  j  a  va2 s . c om
    }
    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);
    }
}