Allow startup to contiune when ConnectionDetailsFactory load fails

Prior to this commit, a failure to load a ConnectionDetailsFactory
caused startup to fail. This causes problems when some of a
factory's required classes were not available, for examle when using
spring-boot-docker-compose without Actuator.

Fixes gh-35100
pull/34978/head
Andy Wilkinson 2 years ago
parent ff36f52f4e
commit cbc03783d0

@ -24,9 +24,13 @@ import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.io.support.SpringFactoriesLoader.FailureHandler;
import org.springframework.util.Assert;
/**
@ -39,6 +43,8 @@ import org.springframework.util.Assert;
*/
public class ConnectionDetailsFactories {
private static final Log logger = LogFactory.getLog(ConnectionDetailsFactories.class);
private List<Registration<?, ?>> registrations = new ArrayList<>();
public ConnectionDetailsFactories() {
@ -47,7 +53,8 @@ public class ConnectionDetailsFactories {
@SuppressWarnings({ "rawtypes", "unchecked" })
ConnectionDetailsFactories(SpringFactoriesLoader loader) {
List<ConnectionDetailsFactory> factories = loader.load(ConnectionDetailsFactory.class);
List<ConnectionDetailsFactory> factories = loader.load(ConnectionDetailsFactory.class,
FailureHandler.logging(logger));
Stream<Registration<?, ?>> registrations = factories.stream().map(Registration::get);
registrations.filter(Objects::nonNull).forEach(this.registrations::add);
}

@ -28,6 +28,7 @@ import org.springframework.core.test.io.support.MockSpringFactoriesLoader;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatNoException;
/**
* Tests for {@link ConnectionDetailsFactories}.
@ -87,6 +88,12 @@ class ConnectionDetailsFactoriesTests {
assertThat(registrations.get(2).factory()).isEqualTo(orderThree);
}
@Test
void factoryLoadFailureDoesNotPreventOtherFactoriesFromLoading() {
this.loader.add(ConnectionDetailsFactory.class.getName(), "com.example.NonExistentConnectionDetailsFactory");
assertThatNoException().isThrownBy(() -> new ConnectionDetailsFactories(this.loader));
}
private static final class TestConnectionDetailsFactory
implements ConnectionDetailsFactory<String, TestConnectionDetails>, Ordered {

Loading…
Cancel
Save