Add support for functional registration of application listener
This commit adds a functional style registration of an application listener for a particular event. Rather than introducing another method at the ConfigurableApplicationContext interface level, this commit provides a factory method in GenericApplicationListener. Closes gh-21411
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
* Copyright 2002-2024 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.
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.context.event;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.core.ResolvableType;
|
||||
@@ -53,4 +55,16 @@ public interface GenericApplicationListener extends SmartApplicationListener {
|
||||
*/
|
||||
boolean supportsEventType(ResolvableType eventType);
|
||||
|
||||
/**
|
||||
* Create a new {@code ApplicationListener} for the given event type.
|
||||
* @param eventType the event to listen to
|
||||
* @param consumer the consumer to invoke when a matching event is fired
|
||||
* @param <E> the specific {@code ApplicationEvent} subclass to listen to
|
||||
* @return a corresponding {@code ApplicationListener} instance
|
||||
* @since 6.1.3
|
||||
*/
|
||||
static <E extends ApplicationEvent> GenericApplicationListener forEventType(Class<E> eventType, Consumer<E> consumer) {
|
||||
return new GenericApplicationListenerDelegate<>(eventType, consumer);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.event;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.core.ResolvableType;
|
||||
|
||||
/**
|
||||
* A {@link GenericApplicationListener} implementation that supports a single
|
||||
* event type.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 6.1.3
|
||||
* @param <E> the specific {@code ApplicationEvent} subclass to listen to
|
||||
*/
|
||||
class GenericApplicationListenerDelegate<E extends ApplicationEvent> implements GenericApplicationListener {
|
||||
|
||||
private final Class<E> supportedEventType;
|
||||
|
||||
private final Consumer<E> consumer;
|
||||
|
||||
GenericApplicationListenerDelegate(Class<E> supportedEventType, Consumer<E> consumer) {
|
||||
this.supportedEventType = supportedEventType;
|
||||
this.consumer = consumer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationEvent event) {
|
||||
this.consumer.accept(this.supportedEventType.cast(event));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsEventType(ResolvableType eventType) {
|
||||
return this.supportedEventType.isAssignableFrom(eventType.toClass());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user