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

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

Introduction

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

Prototype

@Override
    public void setClassLoader(@Nullable ClassLoader classLoader) 

Source Link

Usage

From source file:io.gravitee.gateway.policy.impl.processor.spring.SpringPolicyContextProviderFactory.java

public PolicyContextProvider create(PolicyContext policyContext) {
    Import importAnnotation = policyContext.getClass().getAnnotation(Import.class);

    List<Class<?>> importClasses = Arrays.asList(importAnnotation.value());

    LOGGER.info("Initializing a Spring context provider from @Import annotation: {}",
            String.join(",", importClasses.stream().map(Class::getName).collect(Collectors.toSet())));

    AnnotationConfigApplicationContext policyApplicationContext = new AnnotationConfigApplicationContext();
    policyApplicationContext.setClassLoader(policyContext.getClass().getClassLoader());
    importClasses.forEach(policyApplicationContext::register);

    // TODO: set the parent application context ?
    // pluginContext.setEnvironment(applicationContextParent.getEnvironment());
    // pluginContext.setParent(applicationContextParent);

    policyApplicationContext.refresh();/*from   ww  w .  j av a  2  s .c  o m*/
    return new SpringPolicyContextProvider(policyApplicationContext);
}

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  www .  j av  a 2  s.  c  o  m*/

    return context;
}

From source file:org.springframework.data.web.config.SpringDataWebConfigurationIntegrationTests.java

private SpringDataWebConfiguration createConfigWithClassLoader(ClassLoader classLoader) {

    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
            SpringDataWebConfiguration.class);

    context.setClassLoader(classLoader);

    try {/*from w w  w.j  a  v a 2s.c  om*/
        return context.getBean(SpringDataWebConfiguration.class);
    } finally {
        context.close();
    }
}

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

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