Skip to content

Commit

Permalink
Allow startup to contiune when ConnectionDetailsFactory load fails
Browse files Browse the repository at this point in the history
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 spring-projectsgh-35100
  • Loading branch information
wilkinsona committed Apr 20, 2023
1 parent ff36f52 commit cbc0378
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@
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;

/**
Expand All @@ -39,6 +43,8 @@
*/
public class ConnectionDetailsFactories {

private static final Log logger = LogFactory.getLog(ConnectionDetailsFactories.class);

private List<Registration<?, ?>> registrations = new ArrayList<>();

public ConnectionDetailsFactories() {
Expand All @@ -47,7 +53,8 @@ public 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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}.
Expand Down Expand Up @@ -87,6 +88,12 @@ void getRegistrationsReturnsOrderedDelegates() {
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 {

Expand Down

0 comments on commit cbc0378

Please sign in to comment.