Polish 'Allow ConnectionDetailsFactories to use context class loader'

Refine the submitted pull-request to remove the configuration
property with the assumption that the context classloader will
work for all cases.

See gh-45014
This commit is contained in:
Phillip Webb
2025-04-08 13:03:03 -07:00
parent cc1e1cc92a
commit d15078d35f
6 changed files with 29 additions and 62 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 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.
@@ -46,11 +46,6 @@ public class DockerComposeProperties {
*/
private boolean enabled = true;
/**
* Whether to try to use the context class loader for connection details factories.
*/
private boolean useContextClassLoader = false;
/**
* Arguments to pass to the Docker Compose command.
*/
@@ -94,18 +89,10 @@ public class DockerComposeProperties {
return this.enabled;
}
public boolean isUseContextClassLoader() {
return this.useContextClassLoader;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public void setUseContextClassLoader(boolean useContextClassLoader) {
this.useContextClassLoader = useContextClassLoader;
}
public List<String> getArguments() {
return this.arguments;
}

View File

@@ -27,9 +27,7 @@ import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.autoconfigure.container.ContainerImageMetadata;
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsFactories;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.docker.compose.core.RunningService;
import org.springframework.boot.docker.compose.lifecycle.DockerComposeProperties;
import org.springframework.boot.docker.compose.lifecycle.DockerComposeServicesReadyEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
@@ -48,30 +46,32 @@ import org.springframework.util.StringUtils;
class DockerComposeServiceConnectionsApplicationListener
implements ApplicationListener<DockerComposeServicesReadyEvent> {
DockerComposeServiceConnectionsApplicationListener() {
private final ConnectionDetailsFactories factories;
DockerComposeServiceConnectionsApplicationListener() {
this(new ConnectionDetailsFactories(null));
}
DockerComposeServiceConnectionsApplicationListener(ConnectionDetailsFactories factories) {
this.factories = factories;
}
@Override
public void onApplicationEvent(DockerComposeServicesReadyEvent event) {
ApplicationContext applicationContext = event.getSource();
if (applicationContext instanceof BeanDefinitionRegistry registry) {
Binder binder = Binder.get(applicationContext.getEnvironment());
DockerComposeProperties properties = DockerComposeProperties.get(binder);
boolean useContextClassLoader = properties.isUseContextClassLoader();
ConnectionDetailsFactories factories = new ConnectionDetailsFactories(useContextClassLoader);
Environment environment = applicationContext.getEnvironment();
registerConnectionDetails(registry, environment, event.getRunningServices(), factories);
registerConnectionDetails(registry, environment, event.getRunningServices());
}
}
private void registerConnectionDetails(BeanDefinitionRegistry registry, Environment environment,
List<RunningService> runningServices, ConnectionDetailsFactories factories) {
List<RunningService> runningServices) {
for (RunningService runningService : runningServices) {
DockerComposeConnectionSource source = new DockerComposeConnectionSource(runningService, environment);
factories.getConnectionDetails(source, false).forEach((connectionDetailsType, connectionDetails) -> {
this.factories.getConnectionDetails(source, false).forEach((connectionDetailsType, connectionDetails) -> {
register(registry, runningService, connectionDetailsType, connectionDetails);
factories.getConnectionDetails(connectionDetails, false)
this.factories.getConnectionDetails(connectionDetails, false)
.forEach((adaptedType, adaptedDetails) -> register(registry, runningService, adaptedType,
adaptedDetails));
});