fix reference cache (#7412)

diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/DubboConfigInitializationPostProcessor.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/DubboConfigInitializationPostProcessor.java
index c6578d2..d9c5181 100644
--- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/DubboConfigInitializationPostProcessor.java
+++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/DubboConfigInitializationPostProcessor.java
@@ -75,14 +75,6 @@
 
     @Override
     public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
-//        try {
-//            if (bean instanceof ReferenceBean) {
-//                ReferenceBean referenceBean = (ReferenceBean) bean;
-//                referenceBeanManager.addReference(referenceBean);
-//            }
-//        } catch (Exception e) {
-//            throw new BeanInitializationException("Initialization reference bean failed", e);
-//        }
         return bean;
     }
 
diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBeanManager.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBeanManager.java
index 81afe4d..46be2e5 100644
--- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBeanManager.java
+++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBeanManager.java
@@ -94,13 +94,13 @@
      * @throws Exception
      */
     public void prepareReferenceBeans() throws Exception {
-        for (ReferenceBean referenceBean : getReferences()) {
-            initReferenceBean(referenceBean);
-        }
-
         // prepare all reference beans
         Map<String, ReferenceBean> referenceBeanMap = applicationContext.getBeansOfType(ReferenceBean.class, true, false);
         for (ReferenceBean referenceBean : referenceBeanMap.values()) {
+            addReference(referenceBean);
+        }
+
+        for (ReferenceBean referenceBean : getReferences()) {
             initReferenceBean(referenceBean);
         }
         initialized = true;
diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java
index 2379f54..e6a9715 100644
--- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java
+++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java
@@ -252,8 +252,8 @@
 
                 beanDefinitionRegistry.registerBeanDefinition(referenceBeanName, beanDefinition);
                 getBeanFactory().registerSingleton(referenceBeanName, referenceBean);
-
-                //referenceBeanManager.addReference(referenceBean);
+                //cache reference bean, avoid re-inject same element after prepare reference bean
+                referenceBeanManager.addReference(referenceBean);
             } catch (Exception e) {
                 throw new Exception("Create dubbo reference bean failed", e);
             }