Преглед изворни кода

Fix #2328 (#3865)

* Fix #2328

case: beanDefinition of the bean defined in Configuration annotated class
in this case, the `beanDefinition.getBeanClassName()` and `beanClass.getName()` is not match

* Update CHANGES.md

Co-authored-by: Jason Song <nobodyiam@gmail.com>
Lonre Wang пре 3 година
родитељ
комит
8c0241cbb2

+ 1 - 0
CHANGES.md

@@ -71,6 +71,7 @@ Apollo 1.9.0
 * [support json/yaml/xml format for public namespace](https://github.com/ctripcorp/apollo/pull/3836)
 * [Translate application into 应用 not 项目](https://github.com/ctripcorp/apollo/pull/3877)
 * [add spring configuration metadata for property names cache](https://github.com/ctripcorp/apollo/pull/3879)
+* [Fix Multiple PropertySourcesPlaceholderConfigurer beans registered issue](https://github.com/ctripcorp/apollo/pull/3865)
 * [use jdk 8 to publish apollo-client-config-data](https://github.com/ctripcorp/apollo/pull/3880)
 
 ------------------

+ 14 - 4
apollo-client/src/main/java/com/ctrip/framework/apollo/spring/util/BeanRegistrationUtil.java

@@ -19,6 +19,7 @@ package com.ctrip.framework.apollo.spring.util;
 import java.util.Map;
 import java.util.Objects;
 
+import org.springframework.beans.factory.BeanFactory;
 import org.springframework.beans.factory.config.BeanDefinition;
 import org.springframework.beans.factory.support.BeanDefinitionBuilder;
 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
@@ -40,10 +41,19 @@ public class BeanRegistrationUtil {
 
     String[] candidates = registry.getBeanDefinitionNames();
 
-    for (String candidate : candidates) {
-      BeanDefinition beanDefinition = registry.getBeanDefinition(candidate);
-      if (Objects.equals(beanDefinition.getBeanClassName(), beanClass.getName())) {
-        return false;
+    if (registry instanceof BeanFactory) {
+      final BeanFactory beanFactory = (BeanFactory) registry;
+      for (String candidate : candidates) {
+        if (beanFactory.isTypeMatch(candidate, beanClass)) {
+          return false;
+        }
+      }
+    } else {
+      for (String candidate : candidates) {
+        BeanDefinition beanDefinition = registry.getBeanDefinition(candidate);
+        if (Objects.equals(beanDefinition.getBeanClassName(), beanClass.getName())) {
+          return false;
+        }
       }
     }