Skip to content

Commit ff1e5a8

Browse files
committed
Introduce ContextRestartedEvent as a subtype of ContextStartedEvent
Prior to this commit, there was no way to differentiate between an ApplicationContext "start" and "restart" in terms of events. This was due to the fact that a ContextStartedEvent was fired for both AbstractApplicationContext.start() and AbstractApplicationContext.restart(). To assist developers who may wish to differentiate between "start" and "restart" events, this commit introduces a new ContextRestartedEvent as a subtype of ContextStartedEvent. In addition, AbstractApplicationContext.restart() now publishes a ContextRestartedEvent instead of a ContextStartedEvent. By making ContextRestartedEvent a subtype of ContextStartedEvent, applications can still expect ContextStoppedEvent/ContextStartedEvent event pairs for consistent lifecycle semantics, and they can optionally check if the ContextStartedEvent is a ContextRestartedEvent. Alternatively, applications can explicitly react to a ContextRestartedEvent. See gh-35168 See gh-35171 Closes gh-35194
1 parent 8eedfdf commit ff1e5a8

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2002-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.context.event;
18+
19+
import org.springframework.context.ApplicationContext;
20+
21+
/**
22+
* Event raised when an {@code ApplicationContext} gets restarted.
23+
*
24+
* <p>Note that {@code ContextRestartedEvent} is a specialization of
25+
* {@link ContextStartedEvent}.
26+
*
27+
* @author Sam Brannen
28+
* @since 7.0
29+
* @see ContextStartedEvent
30+
* @see ContextStoppedEvent
31+
*/
32+
@SuppressWarnings("serial")
33+
public class ContextRestartedEvent extends ContextStartedEvent {
34+
35+
/**
36+
* Create a new {@code ContextRestartedEvent}.
37+
* @param source the {@code ApplicationContext} that has been restarted
38+
* (must not be {@code null})
39+
*/
40+
public ContextRestartedEvent(ApplicationContext source) {
41+
super(source);
42+
}
43+
44+
}

spring-context/src/main/java/org/springframework/context/event/ContextStartedEvent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* @author Mark Fisher
2525
* @author Juergen Hoeller
2626
* @since 2.5
27+
* @see ContextRestartedEvent
2728
* @see ContextStoppedEvent
2829
*/
2930
@SuppressWarnings("serial")

spring-context/src/main/java/org/springframework/context/event/ContextStoppedEvent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* @author Juergen Hoeller
2626
* @since 2.5
2727
* @see ContextStartedEvent
28+
* @see ContextRestartedEvent
2829
*/
2930
@SuppressWarnings("serial")
3031
public class ContextStoppedEvent extends ApplicationContextEvent {

spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import org.springframework.context.event.ApplicationEventMulticaster;
6868
import org.springframework.context.event.ContextClosedEvent;
6969
import org.springframework.context.event.ContextRefreshedEvent;
70+
import org.springframework.context.event.ContextRestartedEvent;
7071
import org.springframework.context.event.ContextStartedEvent;
7172
import org.springframework.context.event.ContextStoppedEvent;
7273
import org.springframework.context.event.SimpleApplicationEventMulticaster;
@@ -1551,7 +1552,7 @@ public void stop() {
15511552
@Override
15521553
public void restart() {
15531554
getLifecycleProcessor().onRestart();
1554-
publishEvent(new ContextStartedEvent(this));
1555+
publishEvent(new ContextRestartedEvent(this));
15551556
}
15561557

15571558
@Override

0 commit comments

Comments
 (0)