Skip to content

Commit 866f79d

Browse files
committed
Preserve type-based user-override for root EventHubClientBuilder
The previous name-based @ConditionalOnMissingBean broke backward compatibility: a user-defined EventHubClientBuilder bean registered under a custom name no longer suppressed the auto-configured root builder. Add a custom condition that ignores only the three reserved auto-config bean names (root, consumer-dedicated, producer-dedicated) so any other EventHubClientBuilder bean is treated as a user override and suppresses auto-configuration, while still allowing the dedicated builders to coexist with the root one.
1 parent a85e947 commit 866f79d

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/implementation/eventhubs/AzureEventHubsClientBuilderConfiguration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
2020
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2121
import org.springframework.context.annotation.Bean;
22+
import org.springframework.context.annotation.Conditional;
2223
import org.springframework.context.annotation.Configuration;
2324
import org.springframework.context.annotation.Import;
2425

@@ -42,6 +43,7 @@ class AzureEventHubsClientBuilderConfiguration {
4243

4344
@Bean(EVENT_HUB_CLIENT_BUILDER_BEAN_NAME)
4445
@ConditionalOnMissingBean(name = EVENT_HUB_CLIENT_BUILDER_BEAN_NAME)
46+
@Conditional(OnMissingUserDefinedEventHubClientBuilderCondition.class)
4547
EventHubClientBuilder eventHubClientBuilder(@Qualifier(EVENT_HUB_CLIENT_BUILDER_FACTORY_BEAN_NAME)
4648
EventHubClientBuilderFactory factory) {
4749
return factory.build();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.spring.cloud.autoconfigure.implementation.eventhubs;
5+
6+
import com.azure.messaging.eventhubs.EventHubClientBuilder;
7+
import com.azure.spring.cloud.autoconfigure.implementation.context.AzureContextUtils;
8+
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
9+
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
10+
import org.springframework.context.annotation.ConditionContext;
11+
import org.springframework.context.annotation.ConfigurationCondition;
12+
import org.springframework.core.type.AnnotatedTypeMetadata;
13+
14+
import java.util.Set;
15+
16+
/**
17+
* Matches when no user-defined {@link EventHubClientBuilder} bean is present in the context.
18+
*
19+
* <p>Beans registered under the well-known auto-configuration bean names
20+
* ({@link AzureContextUtils#EVENT_HUB_CLIENT_BUILDER_BEAN_NAME},
21+
* {@link AzureContextUtils#EVENT_HUB_CONSUMER_CLIENT_BUILDER_BEAN_NAME},
22+
* {@link AzureContextUtils#EVENT_HUB_PRODUCER_CLIENT_BUILDER_BEAN_NAME}) are not considered
23+
* user-defined: the first is the root builder itself; the latter two are dedicated builders
24+
* created by the consumer/producer auto-configurations and must not suppress the root builder.
25+
*/
26+
class OnMissingUserDefinedEventHubClientBuilderCondition extends SpringBootCondition implements ConfigurationCondition {
27+
28+
private static final Set<String> RESERVED_BUILDER_BEAN_NAMES = Set.of(
29+
AzureContextUtils.EVENT_HUB_CLIENT_BUILDER_BEAN_NAME,
30+
AzureContextUtils.EVENT_HUB_CONSUMER_CLIENT_BUILDER_BEAN_NAME,
31+
AzureContextUtils.EVENT_HUB_PRODUCER_CLIENT_BUILDER_BEAN_NAME
32+
);
33+
34+
@Override
35+
public ConfigurationPhase getConfigurationPhase() {
36+
return ConfigurationPhase.REGISTER_BEAN;
37+
}
38+
39+
@Override
40+
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
41+
String[] beanNames = context.getBeanFactory().getBeanNamesForType(EventHubClientBuilder.class, true, false);
42+
for (String name : beanNames) {
43+
if (!RESERVED_BUILDER_BEAN_NAMES.contains(name)) {
44+
return ConditionOutcome.noMatch("found user-defined EventHubClientBuilder bean: " + name);
45+
}
46+
}
47+
return ConditionOutcome.match("no user-defined EventHubClientBuilder bean found");
48+
}
49+
}

0 commit comments

Comments
 (0)