Skip to content

Commit d128dd2

Browse files
committed
Make StartupStep AutoCloseable
This commit mames `StartupStep` extend `AutoCloseable` in order to allow the try/with resources syntax and making the `step.end()` call transparent. Closes gh-35277
1 parent dc26aaa commit d128dd2

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

framework-docs/modules/ROOT/pages/core/beans/context-introduction.adoc

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -933,27 +933,25 @@ Java::
933933
[source,java,indent=0,subs="verbatim,quotes"]
934934
----
935935
// create a startup step and start recording
936-
StartupStep scanPackages = getApplicationStartup().start("spring.context.base-packages.scan");
937-
// add tagging information to the current step
938-
scanPackages.tag("packages", () -> Arrays.toString(basePackages));
939-
// perform the actual phase we're instrumenting
940-
this.scanner.scan(basePackages);
941-
// end the current step
942-
scanPackages.end();
936+
try (StartupStep scanPackages = getApplicationStartup().start("spring.context.base-packages.scan")) {
937+
// add tagging information to the current step
938+
scanPackages.tag("packages", () -> Arrays.toString(basePackages));
939+
// perform the actual phase we're instrumenting
940+
this.scanner.scan(basePackages);
941+
}
943942
----
944943
945944
Kotlin::
946945
+
947946
[source,kotlin,indent=0,subs="verbatim,quotes"]
948947
----
949948
// create a startup step and start recording
950-
val scanPackages = getApplicationStartup().start("spring.context.base-packages.scan")
951-
// add tagging information to the current step
952-
scanPackages.tag("packages", () -> Arrays.toString(basePackages))
953-
// perform the actual phase we're instrumenting
954-
this.scanner.scan(basePackages)
955-
// end the current step
956-
scanPackages.end()
949+
try (val scanPackages = getApplicationStartup().start("spring.context.base-packages.scan")) {
950+
// add tagging information to the current step
951+
scanPackages.tag("packages", () -> Arrays.toString(basePackages));
952+
// perform the actual phase we're instrumenting
953+
this.scanner.scan(basePackages);
954+
}
957955
----
958956
======
959957

spring-core/src/main/java/org/springframework/core/metrics/StartupStep.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* @author Brian Clozel
3737
* @since 5.3
3838
*/
39-
public interface StartupStep {
39+
public interface StartupStep extends AutoCloseable {
4040

4141
/**
4242
* Return the name of the startup step.
@@ -83,6 +83,10 @@ public interface StartupStep {
8383
*/
8484
void end();
8585

86+
@Override
87+
default void close() {
88+
this.end();
89+
}
8690

8791
/**
8892
* Immutable collection of {@link Tag}.

0 commit comments

Comments
 (0)