You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by he...@apache.org on 2025/12/15 18:00:12 UTC
(dubbo) branch conditional-on-missing-bean-for-dubbo-service created (now d488743cb0)
This is an automated email from the ASF dual-hosted git repository.
heliang666s pushed a change to branch conditional-on-missing-bean-for-dubbo-service
in repository https://gitbox.apache.org/repos/asf/dubbo.git
at d488743cb0 feat: Support @ConditionalOnMissingBean for @DubboService
This branch includes the following new commits:
new d488743cb0 feat: Support @ConditionalOnMissingBean for @DubboService
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(dubbo) 01/01: feat: Support @ConditionalOnMissingBean for @DubboService
Posted by he...@apache.org.
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: 何亮 <35...@qq.com>
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;