Example usage for org.springframework.context ConfigurableApplicationContext getParent

List of usage examples for org.springframework.context ConfigurableApplicationContext getParent

Introduction

In this page you can find the example usage for org.springframework.context ConfigurableApplicationContext getParent.

Prototype

@Nullable
ApplicationContext getParent();

Source Link

Document

Return the parent context, or null if there is no parent and this is the root of the context hierarchy.

Usage

From source file:com.example.config.BeanCountingApplicationListener.java

@SuppressWarnings("resource")
@Override//from  w  ww  .  ja v a  2s  .c  om
public void onApplicationEvent(ApplicationReadyEvent event) {
    if (!event.getApplicationContext().equals(this.context)) {
        return;
    }
    int count = 0;
    ConfigurableApplicationContext context = event.getApplicationContext();
    String id = context.getId();
    List<String> names = new ArrayList<>();
    while (context != null) {
        count += context.getBeanDefinitionCount();
        names.addAll(Arrays.asList(context.getBeanDefinitionNames()));
        context = (ConfigurableApplicationContext) context.getParent();
    }
    logger.info("Bean count: " + id + "=" + count);
    logger.debug("Bean names: " + id + "=" + names);
    try {
        logger.info("Class count: " + id + "="
                + ManagementFactory.getClassLoadingMXBean().getTotalLoadedClassCount());
    } catch (Exception e) {
    }
}

From source file:org.dspace.app.rest.utils.DSpaceKernelInitializer.java

@Override
public void initialize(final ConfigurableApplicationContext applicationContext) {
    // Check if the kernel is already started
    this.dspaceKernel = DSpaceKernelManager.getDefaultKernel();
    if (this.dspaceKernel == null) {
        DSpaceKernelImpl kernelImpl = null;
        try {/* w w w  .  j av a2s . c  om*/
            // Load the kernel with default settings
            kernelImpl = DSpaceKernelInit.getKernel(null);
            if (!kernelImpl.isRunning()) {
                // Determine configured DSpace home & init the Kernel
                kernelImpl.start(getDSpaceHome(applicationContext.getEnvironment()));
            }
            this.dspaceKernel = kernelImpl;

        } catch (Exception e) {
            // failed to start so destroy it and log and throw an exception
            try {
                if (kernelImpl != null) {
                    kernelImpl.destroy();
                }
                this.dspaceKernel = null;
            } catch (Exception e1) {
                // nothing
            }
            String message = "Failure during ServletContext initialisation: " + e.getMessage();
            log.error(message, e);
            throw new RuntimeException(message, e);
        }
    }

    if (applicationContext.getParent() == null) {
        // Set the DSpace Kernel Application context as a parent of the Spring Boot context so that
        // we can auto-wire all DSpace Kernel services
        applicationContext.setParent(dspaceKernel.getServiceManager().getApplicationContext());

        //Add a listener for Spring Boot application shutdown so that we can nicely cleanup the DSpace kernel.
        applicationContext.addApplicationListener(new DSpaceKernelDestroyer(dspaceKernel));
    }
}

From source file:org.springframework.boot.SpringApplication.java

private ConfigurableApplicationContext doRun(SpringApplicationRunListeners listeners, String... args) {
    ConfigurableApplicationContext context;
    // Create and configure the environment
    ConfigurableEnvironment environment = getOrCreateEnvironment();
    configureEnvironment(environment, args);
    listeners.environmentPrepared(environment);
    if (isWebEnvironment(environment) && !this.webEnvironment) {
        environment = convertToStandardEnvironment(environment);
    }// www  .  jav a2  s .  c  o m

    if (this.bannerMode != Banner.Mode.OFF) {
        printBanner(environment);
    }

    // Create, load, refresh and run the ApplicationContext
    context = createApplicationContext();
    context.setEnvironment(environment);
    postProcessApplicationContext(context);
    applyInitializers(context);
    listeners.contextPrepared(context);
    if (this.logStartupInfo) {
        logStartupInfo(context.getParent() == null);
        logStartupProfileInfo(context);
    }

    // Add boot specific singleton beans
    ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
    context.getBeanFactory().registerSingleton("springApplicationArguments", applicationArguments);

    // Load the sources
    Set<Object> sources = getSources();
    Assert.notEmpty(sources, "Sources must not be empty");
    load(context, sources.toArray(new Object[sources.size()]));
    listeners.contextLoaded(context);

    // Refresh the context
    refresh(context);
    if (this.registerShutdownHook) {
        try {
            context.registerShutdownHook();
        } catch (AccessControlException ex) {
            // Not allowed in some environments.
        }
    }
    afterRefresh(context, applicationArguments);
    listeners.finished(context, null);
    return context;
}

From source file:org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer.java

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    ConfigurableEnvironment environment = applicationContext.getEnvironment();
    MapPropertySource decrypted = new MapPropertySource(DECRYPTED_PROPERTY_SOURCE_NAME,
            decrypt(environment.getPropertySources()));
    if (!decrypted.getSource().isEmpty()) {
        // We have some decrypted properties
        insert(environment.getPropertySources(), decrypted);
        ApplicationContext parent = applicationContext.getParent();
        if (parent != null && (parent.getEnvironment() instanceof ConfigurableEnvironment)) {
            ConfigurableEnvironment mutable = (ConfigurableEnvironment) parent.getEnvironment();
            // The parent is actually the bootstrap context, and it is fully
            // initialized, so we can fire an EnvironmentChangeEvent there to rebind
            // @ConfigurationProperties, in case they were encrypted.
            insert(mutable.getPropertySources(), decrypted);
            parent.publishEvent(new EnvironmentChangeEvent(decrypted.getSource().keySet()));
        }/* w  w w .ja  v a 2s .com*/
    }
}

From source file:org.springframework.samples.petclinic.system.BeanCountingApplicationListener.java

@SuppressWarnings("resource")
@Override/*from  w w  w.j  av  a  2  s  . co  m*/
public void onApplicationEvent(ContextRefreshedEvent event) {
    if (!event.getApplicationContext().equals(this.context)) {
        return;
    }
    int count = 0;
    ConfigurableApplicationContext context = (ConfigurableApplicationContext) event.getApplicationContext();
    String id = context.getId();
    List<String> names = new ArrayList<>();
    while (context != null) {
        count += context.getBeanDefinitionCount();
        names.addAll(Arrays.asList(context.getBeanDefinitionNames()));
        context = (ConfigurableApplicationContext) context.getParent();
    }
    logger.info("Bean count: " + id + "=" + count);
    logger.debug("Bean names: " + id + "=" + names);
    try {
        logger.info("Class count: " + id + "="
                + ManagementFactory.getClassLoadingMXBean().getTotalLoadedClassCount());
    } catch (Exception e) {
    }
}