Example usage for org.springframework.context ConfigurableApplicationContext addApplicationListener

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

Introduction

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

Prototype

void addApplicationListener(ApplicationListener<?> listener);

Source Link

Document

Add a new ApplicationListener that will be notified on context events such as context refresh and context shutdown.

Usage

From source file:org.springframework.boot.context.event.EventPublishingRunListener.java

@Override
public void contextLoaded(ConfigurableApplicationContext context) {
    for (ApplicationListener<?> listener : this.application.getListeners()) {
        if (listener instanceof ApplicationContextAware) {
            ((ApplicationContextAware) listener).setApplicationContext(context);
        }/*www.  j a  va2  s .c  o m*/
        context.addApplicationListener(listener);
    }
    this.initialMulticaster.multicastEvent(new ApplicationPreparedEvent(this.application, this.args, context));
}

From source file:org.springframework.context.event.EventListenerMethodProcessor.java

protected void processBean(final List<EventListenerFactory> factories, final String beanName,
        final Class<?> targetType) {

    if (!this.nonAnnotatedClasses.contains(targetType)) {
        Map<Method, EventListener> annotatedMethods = null;
        try {// ww w. j av  a  2s.c  o  m
            annotatedMethods = MethodIntrospector.selectMethods(targetType,
                    (MethodIntrospector.MetadataLookup<EventListener>) method -> AnnotatedElementUtils
                            .findMergedAnnotation(method, EventListener.class));
        } catch (Throwable ex) {
            // An unresolvable type in a method signature, probably from a lazy bean - let's ignore it.
            if (logger.isDebugEnabled()) {
                logger.debug("Could not resolve methods for bean with name '" + beanName + "'", ex);
            }
        }
        if (CollectionUtils.isEmpty(annotatedMethods)) {
            this.nonAnnotatedClasses.add(targetType);
            if (logger.isTraceEnabled()) {
                logger.trace("No @EventListener annotations found on bean class: " + targetType.getName());
            }
        } else {
            // Non-empty set of methods
            ConfigurableApplicationContext context = getApplicationContext();
            for (Method method : annotatedMethods.keySet()) {
                for (EventListenerFactory factory : factories) {
                    if (factory.supportsMethod(method)) {
                        Method methodToUse = AopUtils.selectInvocableMethod(method, context.getType(beanName));
                        ApplicationListener<?> applicationListener = factory.createApplicationListener(beanName,
                                targetType, methodToUse);
                        if (applicationListener instanceof ApplicationListenerMethodAdapter) {
                            ((ApplicationListenerMethodAdapter) applicationListener).init(context,
                                    this.evaluator);
                        }
                        context.addApplicationListener(applicationListener);
                        break;
                    }
                }
            }
            if (logger.isDebugEnabled()) {
                logger.debug(annotatedMethods.size() + " @EventListener methods processed on bean '" + beanName
                        + "': " + annotatedMethods);
            }
        }
    }
}

From source file:org.springframework.data.gemfire.support.SpringContextBootstrappingInitializer.java

/**
 * Initializes the given ApplicationContext by registering this SpringContextBootstrappingInitializer as an
 * ApplicationListener and registering a runtime shutdown hook.
 *
 * @param applicationContext the ConfigurableApplicationContext to initialize.
 * @return the initialized ApplicationContext.
 * @see org.springframework.context.ConfigurableApplicationContext
 * @see org.springframework.context.ConfigurableApplicationContext#addApplicationListener(org.springframework.context.ApplicationListener)
 * @see org.springframework.context.ConfigurableApplicationContext#registerShutdownHook()
 * @throws java.lang.IllegalArgumentException if the ApplicationContext reference is null!
 *///from w  ww .j av  a 2 s.c  o m
protected ConfigurableApplicationContext initApplicationContext(
        ConfigurableApplicationContext applicationContext) {
    Assert.notNull(applicationContext, "The ConfigurableApplicationContext reference must not be null!");
    applicationContext.addApplicationListener(this);
    applicationContext.registerShutdownHook();
    return setClassLoader(applicationContext);
}