Skip to content

Commit 2430dc9

Browse files
committed
GH-46 - Bootstrap dependencies must only contain DependencyType.USES_COMPONENT.
1 parent 1b06b8b commit 2430dc9

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
lines changed

spring-modulith-core/src/main/java/org/springframework/modulith/model/ApplicationModule.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public Stream<ApplicationModule> getBootstrapDependencies(ApplicationModules mod
177177
Assert.notNull(modules, "Modules must not be null!");
178178
Assert.notNull(depth, "Dependency depth must not be null!");
179179

180-
return streamDependencies(modules, depth);
180+
return streamBootstrapDependencies(modules, depth);
181181
}
182182

183183
/**
@@ -187,12 +187,12 @@ public Stream<ApplicationModule> getBootstrapDependencies(ApplicationModules mod
187187
* @param depth must not be {@literal null}.
188188
* @return
189189
*/
190-
public Stream<JavaPackage> getBasePackages(ApplicationModules modules, DependencyDepth depth) {
190+
public Stream<JavaPackage> getBootstrapBasePackages(ApplicationModules modules, DependencyDepth depth) {
191191

192192
Assert.notNull(modules, "Modules must not be null!");
193193
Assert.notNull(depth, "Dependency depth must not be null!");
194194

195-
Stream<ApplicationModule> dependencies = streamDependencies(modules, depth);
195+
Stream<ApplicationModule> dependencies = streamBootstrapDependencies(modules, depth);
196196

197197
return Stream.concat(Stream.of(this), dependencies) //
198198
.map(ApplicationModule::getBasePackage);
@@ -380,27 +380,28 @@ private Stream<ModuleDependency> getAllModuleDependencies(ApplicationModules mod
380380
.flatMap(it -> getModuleDependenciesOf(it, modules));
381381
}
382382

383-
private Stream<ApplicationModule> streamDependencies(ApplicationModules modules, DependencyDepth depth) {
383+
private Stream<ApplicationModule> streamBootstrapDependencies(ApplicationModules modules, DependencyDepth depth) {
384384

385385
switch (depth) {
386386

387387
case NONE:
388388
return Stream.empty();
389389
case IMMEDIATE:
390-
return getDirectModuleDependencies(modules);
390+
return getDirectModuleBootstrapDependencies(modules);
391391
case ALL:
392392
default:
393-
return getDirectModuleDependencies(modules) //
394-
.flatMap(it -> Stream.concat(Stream.of(it), it.streamDependencies(modules, DependencyDepth.ALL))) //
393+
return getDirectModuleBootstrapDependencies(modules) //
394+
.flatMap(it -> Stream.concat(Stream.of(it), it.streamBootstrapDependencies(modules, DependencyDepth.ALL))) //
395395
.distinct();
396396
}
397397
}
398398

399-
private Stream<ApplicationModule> getDirectModuleDependencies(ApplicationModules modules) {
399+
private Stream<ApplicationModule> getDirectModuleBootstrapDependencies(ApplicationModules modules) {
400400

401401
return getSpringBeansInternal().stream() //
402402
.flatMap(it -> ModuleDependency.fromType(it)) //
403403
.filter(it -> isDependencyToOtherModule(it.target, modules)) //
404+
.filter(it -> it.hasType(DependencyType.USES_COMPONENT)) //
404405
.map(it -> modules.getModuleByType(it.target)) //
405406
.distinct() //
406407
.flatMap(it -> it.map(Stream::of).orElseGet(Stream::empty));
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2022 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+
package com.acme.myproject.moduleD;
17+
18+
import org.springframework.context.event.EventListener;
19+
import org.springframework.stereotype.Component;
20+
21+
import com.acme.myproject.moduleA.SomeEventA;
22+
23+
/**
24+
* @author Oliver Drotbohm
25+
*/
26+
@Component
27+
class EventListenerD {
28+
29+
@EventListener
30+
void on(SomeEventA event) {}
31+
}

spring-modulith-integration-test/src/test/java/com/acme/myproject/ModulithTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.assertj.core.api.Assertions.*;
1919

2020
import org.junit.jupiter.api.Test;
21+
import org.springframework.modulith.model.ApplicationModule;
2122
import org.springframework.modulith.model.ApplicationModules;
2223
import org.springframework.modulith.model.ApplicationModules.Filters;
2324
import org.springframework.modulith.model.Violations;
@@ -71,4 +72,16 @@ void detectsCycleBetweenModules() {
7172
.withMessageContaining("CycleA") //
7273
.withMessageContaining("CycleB");
7374
}
75+
76+
@Test // GH-46
77+
void doesNotIncludeEventListenerDependencyInBootstrapOnes() {
78+
79+
var modules = ApplicationModules.of(Application.class, DEFAULT_EXCLUSIONS);
80+
81+
assertThat(modules.getModuleByName("moduleD")).hasValueSatisfying(it -> {
82+
assertThat(it.getBootstrapDependencies(modules))
83+
.map(ApplicationModule::getName)
84+
.doesNotContain("moduleA");
85+
});
86+
}
7487
}

spring-modulith-test/src/main/java/org/springframework/modulith/test/ModuleTestExecution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ private ModuleTestExecution(ApplicationModuleTest annotation, ApplicationModules
7272

7373
this.basePackages = Suppliers.memoize(() -> {
7474

75-
Stream<JavaPackage> moduleBasePackages = module.getBasePackages(modules, bootstrapMode.getDepth());
75+
Stream<JavaPackage> moduleBasePackages = module.getBootstrapBasePackages(modules, bootstrapMode.getDepth());
7676
Stream<JavaPackage> sharedBasePackages = modules.getSharedModules().stream().map(it -> it.getBasePackage());
7777
Stream<JavaPackage> extraPackages = extraIncludes.stream().map(ApplicationModule::getBasePackage);
7878

0 commit comments

Comments
 (0)