This is an automated email from the ASF dual-hosted git repository. heliang666s pushed a commit to branch conditional-on-missing-bean-for-dubbo-service in repository https://gitbox.apache.org/repos/asf/dubbo.git
commit d488743cb07d5245fc14cbd2e162a8224e8ffa31 Author: 何亮 <[email protected]> AuthorDate: Tue Dec 16 01:59:21 2025 +0800 feat: Support @ConditionalOnMissingBean for @DubboService --- .../annotation/ServiceAnnotationPostProcessor.java | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationPostProcessor.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationPostProcessor.java index 9d22f4476a..8e44b32114 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationPostProcessor.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationPostProcessor.java @@ -618,6 +618,14 @@ public class ServiceAnnotationPostProcessor AnnotatedBeanDefinition refServiceBeanDefinition, Map<String, Object> attributes) { + if (shouldSkipDueToConditionalOnMissingBean(refServiceBeanDefinition)) { + if (logger.isDebugEnabled()) { + logger.debug("Skip registering ServiceBean for bean [" + refServiceBeanName + + "] due to @ConditionalOnMissingBean condition not satisfied"); + } + return; + } + Map<String, Object> serviceAnnotationAttributes = new LinkedHashMap<>(attributes); // get bean class from return type @@ -662,6 +670,43 @@ public class ServiceAnnotationPostProcessor } } + private boolean shouldSkipDueToConditionalOnMissingBean(AnnotatedBeanDefinition beanDefinition) { + MethodMetadata factoryMethod = SpringCompatUtils.getFactoryMethodMetadata(beanDefinition); + if (factoryMethod == null) { + return false; + } + + Map<String, Object> conditionalAttrs = factoryMethod.getAnnotationAttributes( + "org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean"); + + if (conditionalAttrs == null || conditionalAttrs.isEmpty()) { + return false; + } + + Class<?>[] beanTypes = (Class<?>[]) conditionalAttrs.get("value"); + if (beanTypes == null) { + return false; + } + + for (Class<?> beanType : beanTypes) { + if (hasExistingBeanOfType(beanType)) { + return true; + } + } + + return false; + } + + private boolean hasExistingBeanOfType(Class<?> beanType) { + if (registry instanceof ConfigurableListableBeanFactory) { + ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) registry; + + String[] beanNames = beanFactory.getBeanNamesForType(beanType); + return beanNames.length > 0; + } + return false; + } + @Override public void setEnvironment(Environment environment) { this.environment = environment;
