Skip to content

Commit

Permalink
Polish apache#4409 : [Enhancement] @EnableDubboConfig reduces the dup…
Browse files Browse the repository at this point in the history
…licated DubboConfigConfiguration registration
  • Loading branch information
mercyblitz committed Jun 27, 2019
1 parent 859f634 commit dae67c5
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,75 @@

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
* <p>
* 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 {

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 <code>true</code>, or <code>false</code>
* @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) {

Expand All @@ -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<Class<?>> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

}
}

0 comments on commit dae67c5

Please sign in to comment.