Browse Source

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
pull/35374/head
Brian Clozel 4 months ago
parent
commit
d128dd2616
  1. 26
      framework-docs/modules/ROOT/pages/core/beans/context-introduction.adoc
  2. 6
      spring-core/src/main/java/org/springframework/core/metrics/StartupStep.java

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

@ -933,13 +933,12 @@ Java::
[source,java,indent=0,subs="verbatim,quotes"] [source,java,indent=0,subs="verbatim,quotes"]
---- ----
// create a startup step and start recording // create a startup step and start recording
StartupStep scanPackages = getApplicationStartup().start("spring.context.base-packages.scan"); try (StartupStep scanPackages = getApplicationStartup().start("spring.context.base-packages.scan")) {
// add tagging information to the current step // add tagging information to the current step
scanPackages.tag("packages", () -> Arrays.toString(basePackages)); scanPackages.tag("packages", () -> Arrays.toString(basePackages));
// perform the actual phase we're instrumenting // perform the actual phase we're instrumenting
this.scanner.scan(basePackages); this.scanner.scan(basePackages);
// end the current step }
scanPackages.end();
---- ----
Kotlin:: Kotlin::
@ -947,13 +946,12 @@ Kotlin::
[source,kotlin,indent=0,subs="verbatim,quotes"] [source,kotlin,indent=0,subs="verbatim,quotes"]
---- ----
// create a startup step and start recording // create a startup step and start recording
val scanPackages = getApplicationStartup().start("spring.context.base-packages.scan") try (val scanPackages = getApplicationStartup().start("spring.context.base-packages.scan")) {
// add tagging information to the current step // add tagging information to the current step
scanPackages.tag("packages", () -> Arrays.toString(basePackages)) scanPackages.tag("packages", () -> Arrays.toString(basePackages));
// perform the actual phase we're instrumenting // perform the actual phase we're instrumenting
this.scanner.scan(basePackages) this.scanner.scan(basePackages);
// end the current step }
scanPackages.end()
---- ----
====== ======

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

@ -36,7 +36,7 @@ import org.jspecify.annotations.Nullable;
* @author Brian Clozel * @author Brian Clozel
* @since 5.3 * @since 5.3
*/ */
public interface StartupStep { public interface StartupStep extends AutoCloseable {
/** /**
* Return the name of the startup step. * Return the name of the startup step.
@ -83,6 +83,10 @@ public interface StartupStep {
*/ */
void end(); void end();
@Override
default void close() {
this.end();
}
/** /**
* Immutable collection of {@link Tag}. * Immutable collection of {@link Tag}.

Loading…
Cancel
Save