GH-613 - Add SPI to support external ApplicationModuleSource contributions.
We now expose ApplicationModuleSourceFactory as Spring Factories-based SPI interface to further contribute ApplicationModuleSource instances either from a provided root package subject for module detection through a (potentially customized) ApplicationModuleDetectionStrategy or by explicitly listing particular module base packages.
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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 contributed;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.modulith.core.ApplicationModuleDetectionStrategy;
|
||||
import org.springframework.modulith.core.ApplicationModuleSourceFactory;
|
||||
|
||||
/**
|
||||
* Example {@link ApplicationModuleSourceFactory}.
|
||||
*
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
public class ApplicationModuleSourceContribution implements ApplicationModuleSourceFactory {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.modulith.core.ApplicationModuleSourceFactory#getRootPackages()
|
||||
*/
|
||||
@Override
|
||||
public List<String> getRootPackages() {
|
||||
return List.of("contributed");
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.modulith.core.ApplicationModuleSourceFactory#getApplicationModuleDetectionStrategy()
|
||||
*/
|
||||
@Override
|
||||
public ApplicationModuleDetectionStrategy getApplicationModuleDetectionStrategy() {
|
||||
return ApplicationModuleDetectionStrategy.explicitlyAnnotated();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.modulith.core.ApplicationModuleSourceFactory#getModuleBasePackages()
|
||||
*/
|
||||
@Override
|
||||
public List<String> getModuleBasePackages() {
|
||||
return List.of("contributed.enumerated");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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 contributed.detected;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
public class DetectedContribution {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
@org.springframework.modulith.ApplicationModule
|
||||
package contributed.detected;
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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 contributed.enumerated;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
public class EnumeratedContribution {
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package contributed.enumerated;
|
||||
@@ -0,0 +1 @@
|
||||
org.springframework.modulith.core.ApplicationModuleSourceFactory=contributed.ApplicationModuleSourceContribution
|
||||
@@ -242,6 +242,25 @@ class ApplicationModulesIntegrationTest {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> ApplicationModules.of("non.existant"));
|
||||
}
|
||||
|
||||
@Test // GH-613
|
||||
void detectsContributedApplicationModules() {
|
||||
|
||||
var location = ApplicationModuleSourceContributions.LOCATION;
|
||||
|
||||
ApplicationModuleSourceContributions.LOCATION = "META-INF/spring-test.factories";
|
||||
|
||||
try {
|
||||
|
||||
var modules = org.springframework.modulith.test.TestUtils.createApplicationModules(Application.class);
|
||||
|
||||
assertThat(modules.getModuleByName("detected")).isNotEmpty();
|
||||
assertThat(modules.getModuleByName("enumerated")).isNotEmpty();
|
||||
|
||||
} finally {
|
||||
ApplicationModuleSourceContributions.LOCATION = location;
|
||||
}
|
||||
}
|
||||
|
||||
private static void verifyNamedInterfaces(NamedInterfaces interfaces, String name, Class<?>... types) {
|
||||
|
||||
Stream.of(types).forEach(type -> {
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.springframework.boot.test.context.SpringBootContextLoader;
|
||||
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
|
||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.modulith.core.ApplicationModules;
|
||||
import org.springframework.test.context.BootstrapContext;
|
||||
import org.springframework.test.context.CacheAwareContextLoaderDelegate;
|
||||
import org.springframework.test.context.ContextLoadException;
|
||||
@@ -30,11 +31,25 @@ import org.springframework.test.context.MergedContextConfiguration;
|
||||
import org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate;
|
||||
import org.springframework.test.context.support.DefaultBootstrapContext;
|
||||
|
||||
import com.tngtech.archunit.base.DescribedPredicate;
|
||||
import com.tngtech.archunit.core.domain.JavaClass;
|
||||
|
||||
/**
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
public class TestUtils {
|
||||
|
||||
/**
|
||||
* Creates a fresh {@link ApplicationModules} instance no matter the caching state.
|
||||
*
|
||||
* @param type must not be {@literal null}.
|
||||
* @return will never be {@literal null}.
|
||||
* @since 1.3
|
||||
*/
|
||||
public static ApplicationModules createApplicationModules(Class<?> type) {
|
||||
return ApplicationModules.of(type, cacheInvalidator());
|
||||
}
|
||||
|
||||
public static void assertDependencyMissing(Class<?> testClass, Class<?> expectedMissingDependency) {
|
||||
|
||||
CacheAwareContextLoaderDelegate delegate = new DefaultCacheAwareContextLoaderDelegate();
|
||||
@@ -72,4 +87,15 @@ public class TestUtils {
|
||||
private static RuntimeException asRuntimeException(Throwable o_O) {
|
||||
return o_O instanceof RuntimeException ? (RuntimeException) o_O : new RuntimeException(o_O);
|
||||
}
|
||||
|
||||
private static DescribedPredicate<JavaClass> cacheInvalidator() {
|
||||
|
||||
return new DescribedPredicate<JavaClass>("invalidator") {
|
||||
|
||||
@Override
|
||||
public boolean test(JavaClass t) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user