-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-656 - Allow to configure the ApplicationModuleDetectionStrategy vi…
…a a configuration property. We now expose a configuration property spring.modulith.detection-strategy that can take either of the two prepared values "direct-sub-packages" (default) or "explicitly-annotated", or a fully qualified class name of the strategy to use. Removed ApplicationModuleStrategies enum to avoid exposing the enum values as additional implementations. Those are now held as inline lambda expression in the factory methods on ApplicationModuleStrategy. Extracted the lookup of the strategy to use into ApplicationModuleDetectionStrategyLookup for easier testability.
- Loading branch information
Showing
11 changed files
with
236 additions
and
102 deletions.
There are no files selected for viewing
60 changes: 0 additions & 60 deletions
60
...src/main/java/org/springframework/modulith/core/ApplicationModuleDetectionStrategies.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...main/java/org/springframework/modulith/core/ApplicationModuleDetectionStrategyLookup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.modulith.core; | ||
|
||
import java.util.List; | ||
import java.util.function.Supplier; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.BeanUtils; | ||
import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor; | ||
import org.springframework.core.env.StandardEnvironment; | ||
import org.springframework.core.io.support.SpringFactoriesLoader; | ||
import org.springframework.util.ClassUtils; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* A factory for the {@link ApplicationModuleDetectionStrategy} to be used when scanning code for | ||
* {@link ApplicationModule}s. | ||
* | ||
* @author Oliver Drotbohm | ||
*/ | ||
class ApplicationModuleDetectionStrategyLookup { | ||
|
||
private static final String DETECTION_STRATEGY_PROPERTY = "spring.modulith.detection-strategy"; | ||
private static final Logger LOG = LoggerFactory.getLogger(ApplicationModuleDetectionStrategyLookup.class); | ||
private static final Supplier<ApplicationModuleDetectionStrategy> FALLBACK_DETECTION_STRATEGY; | ||
|
||
static { | ||
|
||
FALLBACK_DETECTION_STRATEGY = () -> { | ||
|
||
List<ApplicationModuleDetectionStrategy> loadFactories = SpringFactoriesLoader.loadFactories( | ||
ApplicationModuleDetectionStrategy.class, ApplicationModules.class.getClassLoader()); | ||
|
||
var size = loadFactories.size(); | ||
|
||
if (size == 0) { | ||
return ApplicationModuleDetectionStrategy.directSubPackage(); | ||
} | ||
|
||
if (size > 1) { | ||
|
||
throw new IllegalStateException( | ||
"Multiple module detection strategies configured. Only one supported! %s".formatted(loadFactories)); | ||
} | ||
|
||
LOG.warn( | ||
"Configuring the application module detection strategy via spring.factories is deprecated! Please configure {} instead.", | ||
DETECTION_STRATEGY_PROPERTY); | ||
|
||
return loadFactories.get(0); | ||
}; | ||
} | ||
|
||
/** | ||
* Returns the {@link ApplicationModuleDetectionStrategy} to be used to detect {@link ApplicationModule}s. Will use | ||
* the following algorithm: | ||
* <ol> | ||
* <li>Use the prepared strategies if | ||
* | ||
* @return | ||
*/ | ||
static ApplicationModuleDetectionStrategy getStrategy() { | ||
|
||
var environment = new StandardEnvironment(); | ||
ConfigDataEnvironmentPostProcessor.applyTo(environment); | ||
|
||
var configuredStrategy = environment.getProperty(DETECTION_STRATEGY_PROPERTY, String.class); | ||
|
||
// Nothing configured? Use fallback. | ||
if (!StringUtils.hasText(configuredStrategy)) { | ||
return FALLBACK_DETECTION_STRATEGY.get(); | ||
} | ||
|
||
// Any of the prepared ones? | ||
switch (configuredStrategy) { | ||
case "direct-sub-packages": | ||
return ApplicationModuleDetectionStrategy.directSubPackage(); | ||
case "explicitly-annotated": | ||
return ApplicationModuleDetectionStrategy.explictlyAnnotated(); | ||
} | ||
|
||
try { | ||
|
||
// Lookup configured value as class | ||
var strategyType = ClassUtils.forName(configuredStrategy, ApplicationModules.class.getClassLoader()); | ||
return BeanUtils.instantiateClass(strategyType, ApplicationModuleDetectionStrategy.class); | ||
|
||
} catch (ClassNotFoundException | LinkageError o_O) { | ||
throw new IllegalStateException(o_O); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
spring-modulith-core/src/main/resources/META-INF/spring-configuration-metadata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"properties": [ | ||
{ | ||
"name": "spring.modulith.detection-strategy", | ||
"type": "java.lang.String", | ||
"description": "The strategy how to detect application modules." | ||
} | ||
], | ||
"hints": [ | ||
{ | ||
"name": "spring.modulith.detection-strategy", | ||
"values": [ | ||
{ | ||
"value": "direct-sub-packages", | ||
"description" : "Selects the direct sub-packages underneath the main application class as application module base interfaces." | ||
}, | ||
{ | ||
"value": "explicitly-annotated", | ||
"description" : "Only selects explicitly annotated packages as application module base packages (via @ApplicationModules or jMolecules' DDD @Module)." | ||
} | ||
], | ||
"providers": [ | ||
{ | ||
"name": "class-reference", | ||
"parameters": { | ||
"target": "org.springframework.modulith.core.ApplicationModuleDetectionStrategy" | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} |
65 changes: 65 additions & 0 deletions
65
...java/org/springframework/modulith/core/ApplicationModuleDetectionStrategyLookupTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.modulith.core; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Unit tests for {@link ApplicationModuleDetectionStrategy}. | ||
* | ||
* @author Oliver Drotbohm | ||
*/ | ||
class ApplicationModuleDetectionStrategyLookupTests { | ||
|
||
@Test // GH-656 | ||
void usesExplicitlyAnnotatedStrategyIfConfigured() { | ||
|
||
System.setProperty("spring.config.additional-location", "classpath:detection/explicitly-annotated.properties"); | ||
|
||
assertThat(ApplicationModuleDetectionStrategyLookup.getStrategy()) | ||
.isEqualTo(ApplicationModuleDetectionStrategy.explictlyAnnotated()); | ||
} | ||
|
||
@Test // GH-656 | ||
void usesDirectSubPackagesStrategyIfConfigured() { | ||
|
||
System.setProperty("spring.config.additional-location", "classpath:detection/direct-sub-packages.properties"); | ||
|
||
assertThat(ApplicationModuleDetectionStrategyLookup.getStrategy()) | ||
.isEqualTo(ApplicationModuleDetectionStrategy.directSubPackage()); | ||
} | ||
|
||
@Test // GH-656 | ||
void usesCustomStrategyIfConfigured() { | ||
|
||
System.setProperty("spring.config.additional-location", "classpath:detection/custom-type.properties"); | ||
|
||
assertThat(ApplicationModuleDetectionStrategyLookup.getStrategy()) | ||
.isInstanceOf(TestStrategy.class); | ||
} | ||
|
||
static class TestStrategy implements ApplicationModuleDetectionStrategy { | ||
|
||
@Override | ||
public Stream<JavaPackage> getModuleBasePackages(JavaPackage basePackage) { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
spring-modulith-core/src/test/resources/detection/custom-type.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
spring.modulith.detection-strategy=org.springframework.modulith.core.ApplicationModuleDetectionStrategyLookupTests.TestStrategy |
1 change: 1 addition & 0 deletions
1
spring-modulith-core/src/test/resources/detection/direct-sub-packages.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
spring.modulith.detection-strategy=direct-sub-packages |
1 change: 1 addition & 0 deletions
1
spring-modulith-core/src/test/resources/detection/explicitly-annotated.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
spring.modulith.detection-strategy=explicitly-annotated |
Oops, something went wrong.