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 17:52:55 UTC
(dubbo) branch feature/conditional-on-missing-bean-for-dubbo-service created (now f139a602de)
This is an automated email from the ASF dual-hosted git repository.
heliang666s pushed a change to branch feature/conditional-on-missing-bean-for-dubbo-service
in repository https://gitbox.apache.org/repos/asf/dubbo.git
at f139a602de feat: Support @ConditionalOnMissingBean for @DubboService
This branch includes the following new commits:
new f139a602de 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 feature/conditional-on-missing-bean-for-dubbo-service
in repository https://gitbox.apache.org/repos/asf/dubbo.git
commit f139a602deb3be7da70f6cab1681eb568448ff29
Author: 何亮 <35...@qq.com>
AuthorDate: Tue Dec 16 01:52:35 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;