GH-587 - Avoid repeated lookup of ApplicationModules in ApplicationModulesRuntime.
This commit is contained in:
@@ -19,6 +19,7 @@ import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.modulith.core.ApplicationModules;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.function.SingletonSupplier;
|
||||
|
||||
/**
|
||||
* Bootstrap type to make sure we only bootstrap the initialization of a {@link ApplicationModules} instance once per
|
||||
@@ -43,7 +44,7 @@ public class ApplicationModulesRuntime implements Supplier<ApplicationModules> {
|
||||
Assert.notNull(modules, "ApplicationModules must not be null!");
|
||||
Assert.notNull(runtime, "ApplicationRuntime must not be null!");
|
||||
|
||||
this.modules = modules;
|
||||
this.modules = SingletonSupplier.of(modules);
|
||||
this.runtime = runtime;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ import different.moduleB.ModuleBType;
|
||||
import example.SampleApplication;
|
||||
import example.moduleA.ModuleAType;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -49,4 +50,37 @@ public class ApplicationModulesRuntimeIntegrationTests {
|
||||
Stream.of(ModuleAType.class, ModuleBType.class)
|
||||
.forEach(it -> assertThat(runtime.isApplicationClass(it)).isTrue());
|
||||
}
|
||||
|
||||
@Test // GH-587
|
||||
void onlyLooksUpApplicationModulesOnce() {
|
||||
|
||||
var context = SpringApplication.run(SampleApplication.class);
|
||||
var applicationRuntime = new TestSpringBootApplicationRuntime(context);
|
||||
var supplier = new CountingSupplier<>(() -> modules);
|
||||
|
||||
var runtime = new ApplicationModulesRuntime(supplier, applicationRuntime);
|
||||
|
||||
runtime.get();
|
||||
runtime.get();
|
||||
|
||||
assertThat(supplier.counter).isEqualTo(1);
|
||||
}
|
||||
|
||||
static class CountingSupplier<T> implements Supplier<T> {
|
||||
|
||||
private final Supplier<T> delegate;
|
||||
private int counter = 0;
|
||||
|
||||
CountingSupplier(Supplier<T> delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get() {
|
||||
|
||||
counter++;
|
||||
|
||||
return delegate.get();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user