From c437a0d1c3067e781ae3ae73ba197640402e6555 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 10 Aug 2018 15:42:30 +0200 Subject: [PATCH] Declare default methods for supportsSourceType and getOrder Issue: SPR-17163 --- .../event/GenericApplicationListener.java | 26 +++++++++++++++---- .../event/SmartApplicationListener.java | 24 +++++++++++++---- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/event/GenericApplicationListener.java b/spring-context/src/main/java/org/springframework/context/event/GenericApplicationListener.java index e72075735903..a97c68adf202 100644 --- a/spring-context/src/main/java/org/springframework/context/event/GenericApplicationListener.java +++ b/spring-context/src/main/java/org/springframework/context/event/GenericApplicationListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,24 +24,40 @@ /** * Extended variant of the standard {@link ApplicationListener} interface, - * exposing further metadata such as the supported event type. + * exposing further metadata such as the supported event and source type. * - *

As of Spring Framework 4.2, supersedes {@link SmartApplicationListener} with - * proper handling of generics-based event. + *

As of Spring Framework 4.2, this interface supersedes the Class-based + * {@link SmartApplicationListener} with full handling of generic event types. * * @author Stephane Nicoll * @since 4.2 + * @see SmartApplicationListener + * @see GenericApplicationListenerAdapter */ public interface GenericApplicationListener extends ApplicationListener, Ordered { /** * Determine whether this listener actually supports the given event type. + * @param eventType the event type (never {@code null}) */ boolean supportsEventType(ResolvableType eventType); /** * Determine whether this listener actually supports the given source type. + *

The default implementation always returns {@code true}. + * @param sourceType the source type, or {@code null} if no source */ - boolean supportsSourceType(@Nullable Class sourceType); + default boolean supportsSourceType(@Nullable Class sourceType) { + return true; + } + + /** + * Determine this listener's order in a set of listeners for the same event. + *

The default implementation returns {@link #LOWEST_PRECEDENCE}. + */ + @Override + default int getOrder() { + return LOWEST_PRECEDENCE; + } } diff --git a/spring-context/src/main/java/org/springframework/context/event/SmartApplicationListener.java b/spring-context/src/main/java/org/springframework/context/event/SmartApplicationListener.java index 01bb0cbc9fc3..b66d58878ab2 100644 --- a/spring-context/src/main/java/org/springframework/context/event/SmartApplicationListener.java +++ b/spring-context/src/main/java/org/springframework/context/event/SmartApplicationListener.java @@ -23,26 +23,40 @@ /** * Extended variant of the standard {@link ApplicationListener} interface, - * exposing further metadata such as the supported event type. + * exposing further metadata such as the supported event and source type. * - *

Users are strongly advised to use the {@link GenericApplicationListener} - * interface instead as it provides an improved detection of generics-based - * event types. + *

For full introspection of generic event types, consider implementing + * the {@link GenericApplicationListener} interface instead. * * @author Juergen Hoeller * @since 3.0 * @see GenericApplicationListener + * @see GenericApplicationListenerAdapter */ public interface SmartApplicationListener extends ApplicationListener, Ordered { /** * Determine whether this listener actually supports the given event type. + * @param eventType the event type (never {@code null}) */ boolean supportsEventType(Class eventType); /** * Determine whether this listener actually supports the given source type. + *

The default implementation always returns {@code true}. + * @param sourceType the source type, or {@code null} if no source */ - boolean supportsSourceType(@Nullable Class sourceType); + default boolean supportsSourceType(@Nullable Class sourceType) { + return true; + } + + /** + * Determine this listener's order in a set of listeners for the same event. + *

The default implementation returns {@link #LOWEST_PRECEDENCE}. + */ + @Override + default int getOrder() { + return LOWEST_PRECEDENCE; + } }