Example usage for org.springframework.context.event EventListenerFactory createApplicationListener

List of usage examples for org.springframework.context.event EventListenerFactory createApplicationListener

Introduction

In this page you can find the example usage for org.springframework.context.event EventListenerFactory createApplicationListener.

Prototype

ApplicationListener<?> createApplicationListener(String beanName, Class<?> type, Method method);

Source Link

Document

Create an ApplicationListener for the specified method.

Usage

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 {/*from w  ww  .j a va2 s . 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);
            }
        }
    }
}