From 5c6622fd77d07ac2551a6182349df878f69b17b1 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Mon, 14 Jul 2025 14:09:55 +0200 Subject: [PATCH] Introduce ApplicationContextEvent.getSource() with covariant return type Prior to this commit, ApplicationContextEvent inherited getSource() from java.util.EventObject.getSource() which has an Object return type. This commit introduces a local getSource() implementation in ApplicationContextEvent with an ApplicationContext covariant return type, analogous to TestContextEvent in spring-test. Closes gh-35197 --- .../event/ApplicationContextEvent.java | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/event/ApplicationContextEvent.java b/spring-context/src/main/java/org/springframework/context/event/ApplicationContextEvent.java index 265f91a39d1..e3c296d8eef 100644 --- a/spring-context/src/main/java/org/springframework/context/event/ApplicationContextEvent.java +++ b/spring-context/src/main/java/org/springframework/context/event/ApplicationContextEvent.java @@ -20,9 +20,10 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationEvent; /** - * Base class for events raised for an {@code ApplicationContext}. + * Base class for events raised for an {@link ApplicationContext}. * * @author Juergen Hoeller + * @author Sam Brannen * @since 2.5 */ @SuppressWarnings("serial") @@ -30,7 +31,7 @@ public abstract class ApplicationContextEvent extends ApplicationEvent { /** * Create a new {@code ApplicationContextEvent}. - * @param source the {@code ApplicationContext} that the event is raised for + * @param source the {@link ApplicationContext} that the event is raised for * (must not be {@code null}) */ public ApplicationContextEvent(ApplicationContext source) { @@ -38,10 +39,22 @@ public abstract class ApplicationContextEvent extends ApplicationEvent { } /** - * Get the {@code ApplicationContext} that the event was raised for. + * Get the {@link ApplicationContext} that the event was raised for. + * @return the {@code ApplicationContext} that the event was raised for + * @since 7.0 + * @see #getApplicationContext() + */ + @Override + public ApplicationContext getSource() { + return getApplicationContext(); + } + + /** + * Get the {@link ApplicationContext} that the event was raised for. + * @see #getSource() */ public final ApplicationContext getApplicationContext() { - return (ApplicationContext) getSource(); + return (ApplicationContext) super.getSource(); } }