From dae67c5eac5f111ef9ebbfd441f7ebf39892e0f8 Mon Sep 17 00:00:00 2001 From: mercyblitz Date: Thu, 27 Jun 2019 13:11:57 +0800 Subject: [PATCH] Polish apache/incubator-dubbo#4409 : [Enhancement] @EnableDubboConfig reduces the duplicated DubboConfigConfiguration registration --- .../AnnotatedBeanDefinitionRegistryUtils.java | 62 +++++++++++++++- ...otatedBeanDefinitionRegistryUtilsTest.java | 74 +++++++++++++++++++ 2 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtilsTest.java diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtils.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtils.java index bf065f8b085..c46ca8adea7 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtils.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtils.java @@ -18,18 +18,27 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.context.annotation.AnnotatedBeanDefinitionReader; +import org.springframework.core.type.AnnotationMetadata; import org.springframework.util.ObjectUtils; import java.lang.annotation.Annotation; -import java.util.Arrays; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Objects; + +import static java.lang.String.format; +import static java.util.Arrays.asList; +import static org.springframework.util.ClassUtils.resolveClassName; /** * Annotated {@link BeanDefinition} Utilities *

* The source code is cloned from https://github.com/alibaba/spring-context-support/blob/1.0.2/src/main/java/com/alibaba/spring/util/AnnotatedBeanDefinitionRegistryUtils.java + * * @since 2.6.6 */ public abstract class AnnotatedBeanDefinitionRegistryUtils { @@ -37,10 +46,47 @@ public abstract class AnnotatedBeanDefinitionRegistryUtils { private static final Log logger = LogFactory.getLog(AnnotatedBeanDefinitionRegistryUtils.class); /** - * Register Beans + * Is present bean that was registered by the specified {@link Annotation annotated} {@link Class class} + * + * @param registry {@link BeanDefinitionRegistry} + * @param annotatedClass the {@link Annotation annotated} {@link Class class} + * @return if present, return true, or false + * @since 2.7.3 + */ + public static boolean isPresentBean(BeanDefinitionRegistry registry, Class annotatedClass) { + + boolean present = false; + + String[] beanNames = registry.getBeanDefinitionNames(); + + ClassLoader classLoader = annotatedClass.getClassLoader(); + + for (String beanName : beanNames) { + BeanDefinition beanDefinition = registry.getBeanDefinition(beanName); + if (beanDefinition instanceof AnnotatedBeanDefinition) { + AnnotationMetadata annotationMetadata = ((AnnotatedBeanDefinition) beanDefinition).getMetadata(); + String className = annotationMetadata.getClassName(); + Class targetClass = resolveClassName(className, classLoader); + present = Objects.equals(targetClass, annotatedClass); + if (present) { + if (logger.isDebugEnabled()) { + logger.debug(format("The annotatedClass[class : %s , bean name : %s] was present in registry[%s]", + className, beanName, registry)); + } + break; + } + } + } + + return present; + } + + /** + * Register Beans if not present in {@link BeanDefinitionRegistry registry} * * @param registry {@link BeanDefinitionRegistry} * @param annotatedClasses {@link Annotation annotation} class + * @revision 2.7.3 {@link #isPresentBean(BeanDefinitionRegistry, Class)} */ public static void registerBeans(BeanDefinitionRegistry registry, Class... annotatedClasses) { @@ -50,10 +96,20 @@ public static void registerBeans(BeanDefinitionRegistry registry, Class... an boolean debugEnabled = logger.isDebugEnabled(); + // Remove all annotated-classes that have been registered + Iterator> iterator = new ArrayList<>(asList(annotatedClasses)).iterator(); + + while (iterator.hasNext()) { + Class annotatedClass = iterator.next(); + if (isPresentBean(registry, annotatedClass)) { + iterator.remove(); + } + } + AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(registry); if (debugEnabled) { - logger.debug(registry.getClass().getSimpleName() + " will register annotated classes : " + Arrays.asList(annotatedClasses) + " ."); + logger.debug(registry.getClass().getSimpleName() + " will register annotated classes : " + asList(annotatedClasses) + " ."); } reader.register(annotatedClasses); diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtilsTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtilsTest.java new file mode 100644 index 00000000000..05c4a78a7bc --- /dev/null +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtilsTest.java @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dubbo.config.spring.util; + +import org.junit.Assert; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.stereotype.Service; + +import static org.apache.dubbo.config.spring.util.AnnotatedBeanDefinitionRegistryUtils.isPresentBean; +import static org.apache.dubbo.config.spring.util.AnnotatedBeanDefinitionRegistryUtils.registerBeans; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * {@link AnnotatedBeanDefinitionRegistryUtils} Test + * + * @since 2.7.3 + */ +public class AnnotatedBeanDefinitionRegistryUtilsTest { + + private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + + @AfterEach + public void destroy() { + context.close(); + } + + @Test + public void testIsPresentBean() { + + assertFalse(isPresentBean(context, A.class)); + + context.register(A.class); + + for (int i = 0; i < 9; i++) { + assertTrue(isPresentBean(context, A.class)); + } + + } + + @Test + public void testRegisterBeans() { + + registerBeans(context, A.class, A.class); + + context.refresh(); + + A a = context.getBean(A.class); + + Assert.assertNotNull(a); + } + + + @Service + static class A { + + } +}