Skip to content

Commit 4656f82

Browse files
committed
GH-187 - Do not include non-exposed Spring beans that implement interfaces in Application Module Canvas by default.
We now properly check whether one of a Spring bean's implemented interfaces is exposed by the module before including it in the default rendering of the Application Module Canvas.
1 parent 0b6dccf commit 4656f82

File tree

5 files changed

+106
-4
lines changed

5 files changed

+106
-4
lines changed

spring-modulith-core/src/main/java/org/springframework/modulith/core/SpringBean.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ public String getFullyQualifiedTypeName() {
7777

7878
/**
7979
* Returns whether the bean is considered to be an API bean, which means it is either a public type exposed in an API
80-
* package of the module or implements a public API interface.
81-
*
82-
* @return
80+
* package of the module or implements an exposed API interface.
8381
*/
8482
public boolean isApiBean() {
85-
return module.isExposed(type) || !getInterfacesWithinModule().isEmpty();
83+
84+
return module.isExposed(type)
85+
|| getInterfacesWithinModule().stream().anyMatch(module::isExposed);
8686
}
8787

8888
/**
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2023 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 example.springbean.internal;
17+
18+
import org.springframework.stereotype.Component;
19+
20+
/**
21+
* @author Oliver Drotbohm
22+
*/
23+
@Component
24+
public class ServiceImplementation implements ServiceInterface {}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2023 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 example.springbean.internal;
17+
18+
/**
19+
* @author Oliver Drotbohm
20+
*/
21+
public interface ServiceInterface {}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2023 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 org.springframework.modulith.core;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import example.springbean.internal.ServiceImplementation;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
/**
25+
* Unit tests for {@link SpringBean}.
26+
*
27+
* @author Oliver Drotbohm
28+
*/
29+
class SpringBeanUnitTests {
30+
31+
@Test // GH-187
32+
void doesNotConsiderInternalBeanImplementingInterfaceApiBean() {
33+
34+
var module = TestUtils.getApplicationModule("example.springbean");
35+
36+
assertThat(module.getSpringBeans())
37+
.filteredOn(SpringBean::isApiBean)
38+
.extracting(SpringBean::getFullyQualifiedTypeName)
39+
.doesNotContain(ServiceImplementation.class.getName());
40+
}
41+
}

spring-modulith-core/src/test/java/org/springframework/modulith/core/TestUtils.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,20 @@ public static Classes getClasses(Class<?> packageType) {
7474
public static JavaPackage getPackage(Class<?> packageType) {
7575
return JavaPackage.of(TestUtils.getClasses(packageType), packageType.getPackageName());
7676
}
77+
78+
public static ApplicationModule getApplicationModule(String packageName) {
79+
return new ApplicationModule(getPackage(packageName), false);
80+
}
81+
82+
private static JavaPackage getPackage(String name) {
83+
return JavaPackage.of(getClasses(name), name);
84+
}
85+
86+
private static Classes getClasses(String packageName) {
87+
88+
Assert.hasText(packageName, "Package name must not be null or empty!");
89+
90+
return Classes.of(new ClassFileImporter()
91+
.importPackages(packageName));
92+
}
7793
}

0 commit comments

Comments
 (0)