Skip to content

Commit

Permalink
ContainerConnectionDetails should inherit qualifiers from Container bean
Browse files Browse the repository at this point in the history
  • Loading branch information
quaff committed Nov 4, 2024
1 parent fdf24c6 commit 792a6f3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,21 @@
* {@link Origin} backed by a Spring Bean.
*
* @author Phillip Webb
* @author Yanming Zhou
*/
class BeanOrigin implements Origin {

private final String beanName;

private final String resourceDescription;
private final BeanDefinition beanDefinition;

BeanOrigin(String beanName, BeanDefinition beanDefinition) {
this.beanName = beanName;
this.resourceDescription = (beanDefinition != null) ? beanDefinition.getResourceDescription() : null;
this.beanDefinition = beanDefinition;
}

BeanDefinition getBeanDefinition() {
return this.beanDefinition;
}

@Override
Expand All @@ -60,9 +65,9 @@ public String toString() {
result.append("Bean '");
result.append(this.beanName);
result.append("'");
if (this.resourceDescription != null) {
if (this.beanDefinition != null && this.beanDefinition.getResourceDescription() != null) {
result.append(" defined in ");
result.append(this.resourceDescription);
result.append(this.beanDefinition.getResourceDescription());
}
return result.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.aot.BeanRegistrationExcludeFilter;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RegisteredBean;
import org.springframework.beans.factory.support.RootBeanDefinition;
Expand All @@ -49,6 +50,7 @@
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Yanming Zhou
*/
class ConnectionDetailsRegistrar {

Expand Down Expand Up @@ -109,9 +111,22 @@ private <T> void registerBeanDefinition(BeanDefinitionRegistry registry, Contain
RootBeanDefinition beanDefinition = new RootBeanDefinition(beanType, beanSupplier);
beanDefinition.setAttribute(ServiceConnection.class.getName(), true);
containerMetadata.addTo(beanDefinition);
if (source.getOrigin() instanceof BeanOrigin beanOrigin) {
inheritQualifiers(beanOrigin.getBeanDefinition(), beanDefinition);
}
registry.registerBeanDefinition(beanName, beanDefinition);
}

private void inheritQualifiers(BeanDefinition origin, RootBeanDefinition derived) {
if (origin instanceof RootBeanDefinition rbd) {
derived.setPrimary(rbd.isPrimary());
derived.setFallback(rbd.isFallback());
derived.setAutowireCandidate(rbd.isAutowireCandidate());
derived.setDefaultCandidate(rbd.isDefaultCandidate());
derived.setQualifiedElement(rbd.getQualifiedElement());
}
}

private String getBeanName(ContainerConnectionSource<?> source, ConnectionDetails connectionDetails) {
List<String> parts = new ArrayList<>();
parts.add(ClassUtils.getShortNameAsProperty(connectionDetails.getClass()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
* Tests for {@link ConnectionDetailsRegistrar}.
*
* @author Phillip Webb
* @author Yanming Zhou
*/
class ConnectionDetailsRegistrarTests {

Expand Down Expand Up @@ -106,6 +107,32 @@ void registerBeanDefinitionsRegistersDefinition() {
assertThat(beanFactory.getBean(TestConnectionDetails.class)).isNotNull();
}

@Test
void containerConnectionDetailsBeanShouldInheritQualifiersFromContainerBean() {
RootBeanDefinition originBeanDefinition = new RootBeanDefinition();
originBeanDefinition.setPrimary(true);
originBeanDefinition.setFallback(false);
originBeanDefinition.setAutowireCandidate(true);
originBeanDefinition.setDefaultCandidate(true);
originBeanDefinition.setQualifiedElement(ConnectionDetailsRegistrarTests.class);
Origin origin = new BeanOrigin("test", originBeanDefinition);
ContainerConnectionSource<?> source = new ContainerConnectionSource<>("test", origin, PostgreSQLContainer.class,
null, this.annotation, () -> this.container);
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
ConnectionDetailsRegistrar registrar = new ConnectionDetailsRegistrar(beanFactory, this.factories);
given(this.factories.getConnectionDetails(source, true))
.willReturn(Map.of(TestConnectionDetails.class, new TestConnectionDetails()));
registrar.registerBeanDefinitions(beanFactory, source);
String[] beanNames = beanFactory.getBeanNamesForType(TestConnectionDetails.class);
assertThat(beanNames).hasSize(1);
RootBeanDefinition beanDefinition = (RootBeanDefinition) beanFactory.getBeanDefinition(beanNames[0]);
assertThat(beanDefinition.isPrimary()).isEqualTo(originBeanDefinition.isPrimary());
assertThat(beanDefinition.isFallback()).isEqualTo(originBeanDefinition.isFallback());
assertThat(beanDefinition.isAutowireCandidate()).isEqualTo(originBeanDefinition.isAutowireCandidate());
assertThat(beanDefinition.isDefaultCandidate()).isEqualTo(originBeanDefinition.isDefaultCandidate());
assertThat(beanDefinition.getQualifiedElement()).isEqualTo(originBeanDefinition.getQualifiedElement());
}

static class TestConnectionDetails implements ConnectionDetails {

}
Expand Down

0 comments on commit 792a6f3

Please sign in to comment.