Filter out duplicates in SpringFactoriesLoader

Prior to this commit, SpringFactoriesLoader discovered all registered
factory implementations for a given factory type even if duplicates
were registered within a single META-INF/spring.factories file or in
multiple such files in the classpath.

This commit updates the internals of SpringFactoriesLoader so that
duplicate registrations are ignored, thereby aligning with the
well-known semantics for java.util.ServiceLoader in this regard.

Closes gh-24985
This commit is contained in:
Sam Brannen
2020-04-28 14:55:37 +02:00
parent afc398333e
commit 4e32615b22
3 changed files with 33 additions and 12 deletions

View File

@@ -34,7 +34,13 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
class SpringFactoriesLoaderTests {
@Test
void loadFactoriesInCorrectOrder() {
void loadFactoriesWithNoRegisteredImplementations() {
List<Integer> factories = SpringFactoriesLoader.loadFactories(Integer.class, null);
assertThat(factories).isEmpty();
}
@Test
void loadFactoriesInCorrectOrderWithDuplicateRegistrationsPresent() {
List<DummyFactory> factories = SpringFactoriesLoader.loadFactories(DummyFactory.class, null);
assertThat(factories).hasSize(2);
assertThat(factories.get(0)).isInstanceOf(MyDummyFactory1.class);