This is an automated email from the ASF dual-hosted git repository. mercyblitz pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/dubbo.git
commit 4d8a1847e1509f0f707db4fc7eaba64b3ab2f009 Author: Mercy Ma <[email protected]> AuthorDate: Thu Nov 14 10:26:30 2019 +0800 [Refactor] Dubbo Spring based on Alibaba spring-context-support (#5324) * Polish apache/dubbo#5306 : [Migration] Upgrade the @since tags in Javadoc migration cloud native to master * Polish apache/dubbo#5306 : [Migration] Upgrade the @since tags in Javadoc migration cloud native to master * Polish apache/dubbo#5309 : [ISSURE] The beans of Dubbo's Config can't be found on the ReferenceBean's initialization * Polish apache/dubbo#5312 : Resolve the demos' issues of zookeeper and nacos * Polish apache/dubbo#5313 : [Migration] migrate the code in common module from cloud-native branch to master * Polish apache/dubbo#5316 : [Refactor] Replace @EnableDubboConfigBinding Using spring-context-support * Polish apache/dubbo#5317 : [Refactor] Refactor ReferenceAnnotationBeanPostProcessor using Alibaba spring-context-suuport API * Polish apache/dubbo#5321 : Remove BeanFactoryUtils * Polish apache/dubbo#5321 : Remove AnnotatedBeanDefinitionRegistryUtils * Polish apache/dubbo#5321 : Remove AnnotationUtils * Polish apache/dubbo#5321 : Remove ClassUtils * Polish apache/dubbo#5321 : Remove BeanRegistrar * Polish apache/dubbo#5321 : Remove ObjectUtils * Polish apache/dubbo#5321 : Remove PropertySourcesUtils --- dubbo-config/dubbo-config-spring/pom.xml | 6 + .../AbstractAnnotationConfigBeanBuilder.java | 10 +- .../AnnotatedInterfaceConfigBeanBuilder.java | 10 +- .../AnnotationInjectedBeanPostProcessor.java | 548 --------------------- .../AnnotationPropertyValuesAdapter.java | 5 +- .../annotation/DubboConfigAliasPostProcessor.java | 2 +- .../ReferenceAnnotationBeanPostProcessor.java | 9 +- .../factory/annotation/ReferenceBeanBuilder.java | 12 +- .../ServiceAnnotationBeanPostProcessor.java | 4 +- .../factory/annotation/ServiceBeanNameBuilder.java | 4 +- .../annotation/DubboComponentScanRegistrar.java | 4 +- .../annotation/DubboConfigBindingRegistrar.java | 17 +- .../annotation/DubboConfigBindingsRegistrar.java | 3 + .../annotation/DubboConfigConfiguration.java | 51 +- .../DubboConfigConfigurationRegistrar.java | 4 +- .../DubboLifecycleComponentRegistrar.java | 2 +- .../annotation/EnableDubboConfigBinding.java | 4 + .../annotation/EnableDubboConfigBindings.java | 5 +- ...pertyDefaultValueDubboConfigBeanCustomizer.java | 2 +- .../properties/DefaultDubboConfigBinder.java | 4 +- .../spring/extension/SpringExtensionFactory.java | 4 +- .../schema/AnnotationBeanDefinitionParser.java | 5 +- .../spring/schema/DubboBeanDefinitionParser.java | 2 +- .../spring/schema/DubboNamespaceHandler.java | 4 +- .../util/AnnotatedBeanDefinitionRegistryUtils.java | 120 ----- .../dubbo/config/spring/util/AnnotationUtils.java | 310 ------------ ...toryUtils.java => ApplicationContextUtils.java} | 56 +-- .../dubbo/config/spring/util/BeanRegistrar.java | 64 --- .../dubbo/config/spring/util/ClassUtils.java | 37 -- .../config/spring/util/DubboAnnotationUtils.java | 150 ++++++ .../config/spring/util/PropertySourcesUtils.java | 115 ----- .../ReferenceAnnotationBeanPostProcessorTest.java | 19 +- .../context/annotation/EnableDubboConfigTest.java | 3 +- .../AnnotatedBeanDefinitionRegistryUtilsTest.java | 83 ---- .../spring/util/ApplicationContextUtilsTest.java} | 33 +- .../config/spring/util/BeanFactoryUtilsTest.java | 111 ----- .../spring/util/PropertySourcesUtilsTest.java | 78 --- dubbo-dependencies-bom/pom.xml | 11 + 38 files changed, 291 insertions(+), 1620 deletions(-) diff --git a/dubbo-config/dubbo-config-spring/pom.xml b/dubbo-config/dubbo-config-spring/pom.xml index aa4b0b2..12bd8d2 100644 --- a/dubbo-config/dubbo-config-spring/pom.xml +++ b/dubbo-config/dubbo-config-spring/pom.xml @@ -56,6 +56,12 @@ <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> + + <dependency> + <groupId>com.alibaba.spring</groupId> + <artifactId>spring-context-support</artifactId> + </dependency> + <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-registry-default</artifactId> diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AbstractAnnotationConfigBeanBuilder.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AbstractAnnotationConfigBeanBuilder.java index 670d568..01186af 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AbstractAnnotationConfigBeanBuilder.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AbstractAnnotationConfigBeanBuilder.java @@ -30,8 +30,8 @@ import org.springframework.util.Assert; import java.lang.annotation.Annotation; import java.util.List; -import static org.apache.dubbo.config.spring.util.BeanFactoryUtils.getBeans; -import static org.apache.dubbo.config.spring.util.BeanFactoryUtils.getNullableBean; +import static com.alibaba.spring.util.BeanFactoryUtils.getBeans; +import static com.alibaba.spring.util.BeanFactoryUtils.getOptionalBean; /** * Abstract Configurable {@link Annotation} Bean Builder @@ -132,7 +132,7 @@ abstract class AbstractAnnotationConfigBeanBuilder<A extends Annotation, B exten String monitorBeanName = resolveMonitorConfigBeanName(annotation); - MonitorConfig monitorConfig = getNullableBean(applicationContext, monitorBeanName, MonitorConfig.class); + MonitorConfig monitorConfig = getOptionalBean(applicationContext, monitorBeanName, MonitorConfig.class); bean.setMonitor(monitorConfig); @@ -143,7 +143,7 @@ abstract class AbstractAnnotationConfigBeanBuilder<A extends Annotation, B exten String applicationConfigBeanName = resolveApplicationConfigBeanName(annotation); ApplicationConfig applicationConfig = - getNullableBean(applicationContext, applicationConfigBeanName, ApplicationConfig.class); + getOptionalBean(applicationContext, applicationConfigBeanName, ApplicationConfig.class); bean.setApplication(applicationConfig); @@ -154,7 +154,7 @@ abstract class AbstractAnnotationConfigBeanBuilder<A extends Annotation, B exten String moduleConfigBeanName = resolveModuleConfigBeanName(annotation); ModuleConfig moduleConfig = - getNullableBean(applicationContext, moduleConfigBeanName, ModuleConfig.class); + getOptionalBean(applicationContext, moduleConfigBeanName, ModuleConfig.class); bean.setModule(moduleConfig); diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotatedInterfaceConfigBeanBuilder.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotatedInterfaceConfigBeanBuilder.java index 97b6655..be951ae 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotatedInterfaceConfigBeanBuilder.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotatedInterfaceConfigBeanBuilder.java @@ -31,8 +31,8 @@ import org.springframework.util.Assert; import java.lang.annotation.Annotation; import java.util.List; -import static org.apache.dubbo.config.spring.util.BeanFactoryUtils.getBeans; -import static org.apache.dubbo.config.spring.util.BeanFactoryUtils.getNullableBean; +import static com.alibaba.spring.util.BeanFactoryUtils.getBeans; +import static com.alibaba.spring.util.BeanFactoryUtils.getOptionalBean; /** * An Abstract Builder to build {@link AbstractInterfaceConfig Interface Config} Bean that annotated @@ -133,7 +133,7 @@ public abstract class AnnotatedInterfaceConfigBeanBuilder<C extends AbstractInte String monitorBeanName = resolveMonitorConfigBeanName(attributes); - MonitorConfig monitorConfig = getNullableBean(applicationContext, monitorBeanName, MonitorConfig.class); + MonitorConfig monitorConfig = getOptionalBean(applicationContext, monitorBeanName, MonitorConfig.class); configBean.setMonitor(monitorConfig); @@ -144,7 +144,7 @@ public abstract class AnnotatedInterfaceConfigBeanBuilder<C extends AbstractInte String applicationConfigBeanName = resolveApplicationConfigBeanName(attributes); ApplicationConfig applicationConfig = - getNullableBean(applicationContext, applicationConfigBeanName, ApplicationConfig.class); + getOptionalBean(applicationContext, applicationConfigBeanName, ApplicationConfig.class); configBean.setApplication(applicationConfig); @@ -155,7 +155,7 @@ public abstract class AnnotatedInterfaceConfigBeanBuilder<C extends AbstractInte String moduleConfigBeanName = resolveModuleConfigBeanName(attributes); ModuleConfig moduleConfig = - getNullableBean(applicationContext, moduleConfigBeanName, ModuleConfig.class); + getOptionalBean(applicationContext, moduleConfigBeanName, ModuleConfig.class); configBean.setModule(moduleConfig); diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationInjectedBeanPostProcessor.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationInjectedBeanPostProcessor.java deleted file mode 100644 index cf87cf2..0000000 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationInjectedBeanPostProcessor.java +++ /dev/null @@ -1,548 +0,0 @@ -/* - * 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.beans.factory.annotation; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.springframework.beans.BeanUtils; -import org.springframework.beans.BeansException; -import org.springframework.beans.PropertyValues; -import org.springframework.beans.factory.BeanClassLoaderAware; -import org.springframework.beans.factory.BeanCreationException; -import org.springframework.beans.factory.BeanFactory; -import org.springframework.beans.factory.BeanFactoryAware; -import org.springframework.beans.factory.DisposableBean; -import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor; -import org.springframework.beans.factory.annotation.InjectionMetadata; -import org.springframework.beans.factory.config.BeanPostProcessor; -import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; -import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter; -import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor; -import org.springframework.beans.factory.support.RootBeanDefinition; -import org.springframework.context.EnvironmentAware; -import org.springframework.core.Ordered; -import org.springframework.core.PriorityOrdered; -import org.springframework.core.annotation.AnnotationAttributes; -import org.springframework.core.env.Environment; -import org.springframework.util.Assert; -import org.springframework.util.ClassUtils; -import org.springframework.util.ReflectionUtils; -import org.springframework.util.StringUtils; - -import java.beans.PropertyDescriptor; -import java.lang.annotation.Annotation; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; - -import static org.apache.dubbo.config.spring.util.AnnotationUtils.getMergedAttributes; -import static org.springframework.core.BridgeMethodResolver.findBridgedMethod; -import static org.springframework.core.BridgeMethodResolver.isVisibilityBridgeMethodPair; - -/** - * Abstract generic {@link BeanPostProcessor} implementation for customized annotation that annotated injected-object. - * <p> - * - * @revision 2.7.3 Uses {@link AnnotationAttributes} instead of {@link Annotation} - * @since 2.6.6 - */ -public abstract class AnnotationInjectedBeanPostProcessor extends - InstantiationAwareBeanPostProcessorAdapter implements MergedBeanDefinitionPostProcessor, PriorityOrdered, - BeanFactoryAware, BeanClassLoaderAware, EnvironmentAware, DisposableBean { - - private final static int CACHE_SIZE = Integer.getInteger("", 32); - - private final Log logger = LogFactory.getLog(getClass()); - - private final Class<? extends Annotation>[] annotationTypes; - - private final ConcurrentMap<String, AnnotationInjectedBeanPostProcessor.AnnotatedInjectionMetadata> injectionMetadataCache = - new ConcurrentHashMap<String, AnnotationInjectedBeanPostProcessor.AnnotatedInjectionMetadata>(CACHE_SIZE); - - private final ConcurrentMap<String, Object> injectedObjectsCache = new ConcurrentHashMap<>(CACHE_SIZE); - - private ConfigurableListableBeanFactory beanFactory; - - private Environment environment; - - private ClassLoader classLoader; - - /** - * make sure higher priority than {@link AutowiredAnnotationBeanPostProcessor} - * - * @revision 2.7.3 - */ - private int order = Ordered.LOWEST_PRECEDENCE - 3; - - /** - * @param annotationTypes the multiple types of {@link Annotation annotations} - * @since 2.7.3 - */ - public AnnotationInjectedBeanPostProcessor(Class<? extends Annotation>... annotationTypes) { - Assert.notEmpty(annotationTypes, "The argument of annotations' types must not empty"); - this.annotationTypes = annotationTypes; - } - - @SafeVarargs - private static <T> Collection<T> combine(Collection<? extends T>... elements) { - List<T> allElements = new ArrayList<>(); - for (Collection<? extends T> e : elements) { - allElements.addAll(e); - } - return allElements; - } - - /** - * Annotation type - * - * @return non-null - * @deprecated 2.7.3, uses {@link #getAnnotationTypes()} - */ - @Deprecated - public final Class<? extends Annotation> getAnnotationType() { - return annotationTypes[0]; - } - - protected final Class<? extends Annotation>[] getAnnotationTypes() { - return annotationTypes; - } - - @Override - public void setBeanFactory(BeanFactory beanFactory) throws BeansException { - Assert.isInstanceOf(ConfigurableListableBeanFactory.class, beanFactory, - "AnnotationInjectedBeanPostProcessor requires a ConfigurableListableBeanFactory"); - this.beanFactory = (ConfigurableListableBeanFactory) beanFactory; - } - - @Override - public PropertyValues postProcessPropertyValues( - PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeanCreationException { - - InjectionMetadata metadata = findInjectionMetadata(beanName, bean.getClass(), pvs); - try { - metadata.inject(bean, beanName, pvs); - } catch (BeanCreationException ex) { - throw ex; - } catch (Throwable ex) { - throw new BeanCreationException(beanName, "Injection of @" + getAnnotationType().getSimpleName() - + " dependencies is failed", ex); - } - return pvs; - } - - - /** - * Finds {@link InjectionMetadata.InjectedElement} Metadata from annotated fields - * - * @param beanClass The {@link Class} of Bean - * @return non-null {@link List} - */ - private List<AnnotationInjectedBeanPostProcessor.AnnotatedFieldElement> findFieldAnnotationMetadata(final Class<?> beanClass) { - - final List<AnnotationInjectedBeanPostProcessor.AnnotatedFieldElement> elements = new LinkedList<AnnotationInjectedBeanPostProcessor.AnnotatedFieldElement>(); - - ReflectionUtils.doWithFields(beanClass, field -> { - - for (Class<? extends Annotation> annotationType : getAnnotationTypes()) { - - AnnotationAttributes attributes = getMergedAttributes(field, annotationType, getEnvironment(), true); - - if (attributes != null) { - - if (Modifier.isStatic(field.getModifiers())) { - if (logger.isWarnEnabled()) { - logger.warn("@" + annotationType.getName() + " is not supported on static fields: " + field); - } - return; - } - - elements.add(new AnnotatedFieldElement(field, attributes)); - } - } - }); - - return elements; - - } - - /** - * Finds {@link InjectionMetadata.InjectedElement} Metadata from annotated methods - * - * @param beanClass The {@link Class} of Bean - * @return non-null {@link List} - */ - private List<AnnotationInjectedBeanPostProcessor.AnnotatedMethodElement> findAnnotatedMethodMetadata(final Class<?> beanClass) { - - final List<AnnotationInjectedBeanPostProcessor.AnnotatedMethodElement> elements = new LinkedList<AnnotationInjectedBeanPostProcessor.AnnotatedMethodElement>(); - - ReflectionUtils.doWithMethods(beanClass, method -> { - - Method bridgedMethod = findBridgedMethod(method); - - if (!isVisibilityBridgeMethodPair(method, bridgedMethod)) { - return; - } - - - for (Class<? extends Annotation> annotationType : getAnnotationTypes()) { - - AnnotationAttributes attributes = getMergedAttributes(bridgedMethod, annotationType, getEnvironment(), true); - - if (attributes != null && method.equals(ClassUtils.getMostSpecificMethod(method, beanClass))) { - if (Modifier.isStatic(method.getModifiers())) { - if (logger.isWarnEnabled()) { - logger.warn("@" + annotationType.getName() + " annotation is not supported on static methods: " + method); - } - return; - } - if (method.getParameterTypes().length == 0) { - if (logger.isWarnEnabled()) { - logger.warn("@" + annotationType.getName() + " annotation should only be used on methods with parameters: " + - method); - } - } - PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, beanClass); - elements.add(new AnnotatedMethodElement(method, pd, attributes)); - } - } - }); - - return elements; - - } - - - private AnnotationInjectedBeanPostProcessor.AnnotatedInjectionMetadata buildAnnotatedMetadata(final Class<?> beanClass) { - Collection<AnnotationInjectedBeanPostProcessor.AnnotatedFieldElement> fieldElements = findFieldAnnotationMetadata(beanClass); - Collection<AnnotationInjectedBeanPostProcessor.AnnotatedMethodElement> methodElements = findAnnotatedMethodMetadata(beanClass); - return new AnnotationInjectedBeanPostProcessor.AnnotatedInjectionMetadata(beanClass, fieldElements, methodElements); - - } - - private InjectionMetadata findInjectionMetadata(String beanName, Class<?> clazz, PropertyValues pvs) { - // Fall back to class name as cache key, for backwards compatibility with custom callers. - String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName()); - // Quick check on the concurrent map first, with minimal locking. - AnnotationInjectedBeanPostProcessor.AnnotatedInjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey); - if (InjectionMetadata.needsRefresh(metadata, clazz)) { - synchronized (this.injectionMetadataCache) { - metadata = this.injectionMetadataCache.get(cacheKey); - if (InjectionMetadata.needsRefresh(metadata, clazz)) { - if (metadata != null) { - metadata.clear(pvs); - } - try { - metadata = buildAnnotatedMetadata(clazz); - this.injectionMetadataCache.put(cacheKey, metadata); - } catch (NoClassDefFoundError err) { - throw new IllegalStateException("Failed to introspect object class [" + clazz.getName() + - "] for annotation metadata: could not find class that it depends on", err); - } - } - } - } - return metadata; - } - - @Override - public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) { - if (beanType != null) { - InjectionMetadata metadata = findInjectionMetadata(beanName, beanType, null); - metadata.checkConfigMembers(beanDefinition); - } - } - - @Override - public int getOrder() { - return order; - } - - public void setOrder(int order) { - this.order = order; - } - - @Override - public void destroy() throws Exception { - - for (Object object : injectedObjectsCache.values()) { - if (logger.isInfoEnabled()) { - logger.info(object + " was destroying!"); - } - - if (object instanceof DisposableBean) { - ((DisposableBean) object).destroy(); - } - } - - injectionMetadataCache.clear(); - injectedObjectsCache.clear(); - - if (logger.isInfoEnabled()) { - logger.info(getClass() + " was destroying!"); - } - - } - - @Override - public void setBeanClassLoader(ClassLoader classLoader) { - this.classLoader = classLoader; - } - - @Override - public void setEnvironment(Environment environment) { - this.environment = environment; - } - - protected Environment getEnvironment() { - return environment; - } - - protected ClassLoader getClassLoader() { - return classLoader; - } - - protected ConfigurableListableBeanFactory getBeanFactory() { - return beanFactory; - } - - /** - * Gets all injected-objects. - * - * @return non-null {@link Collection} - */ - protected Collection<Object> getInjectedObjects() { - return this.injectedObjectsCache.values(); - } - - /** - * Get injected-object from specified {@link AnnotationAttributes annotation attributes} and Bean Class - * - * @param attributes {@link AnnotationAttributes the annotation attributes} - * @param bean Current bean that will be injected - * @param beanName Current bean name that will be injected - * @param injectedType the type of injected-object - * @param injectedElement {@link InjectionMetadata.InjectedElement} - * @return An injected object - * @throws Exception If getting is failed - */ - protected Object getInjectedObject(AnnotationAttributes attributes, Object bean, String beanName, Class<?> injectedType, - InjectionMetadata.InjectedElement injectedElement) throws Exception { - - String cacheKey = buildInjectedObjectCacheKey(attributes, bean, beanName, injectedType, injectedElement); - - Object injectedObject = injectedObjectsCache.get(cacheKey); - - if (injectedObject == null) { - injectedObject = doGetInjectedBean(attributes, bean, beanName, injectedType, injectedElement); - // Customized inject-object if necessary - injectedObjectsCache.putIfAbsent(cacheKey, injectedObject); - } - - return injectedObject; - - } - - /** - * Subclass must implement this method to get injected-object. The context objects could help this method if - * necessary : - * <ul> - * <li>{@link #getBeanFactory() BeanFactory}</li> - * <li>{@link #getClassLoader() ClassLoader}</li> - * <li>{@link #getEnvironment() Environment}</li> - * </ul> - * - * @param attributes {@link AnnotationAttributes the annotation attributes} - * @param bean Current bean that will be injected - * @param beanName Current bean name that will be injected - * @param injectedType the type of injected-object - * @param injectedElement {@link InjectionMetadata.InjectedElement} - * @return The injected object - * @throws Exception If resolving an injected object is failed. - */ - protected abstract Object doGetInjectedBean(AnnotationAttributes attributes, Object bean, String beanName, Class<?> injectedType, - InjectionMetadata.InjectedElement injectedElement) throws Exception; - - /** - * Build a cache key for injected-object. The context objects could help this method if - * necessary : - * <ul> - * <li>{@link #getBeanFactory() BeanFactory}</li> - * <li>{@link #getClassLoader() ClassLoader}</li> - * <li>{@link #getEnvironment() Environment}</li> - * </ul> - * - * @param attributes {@link AnnotationAttributes the annotation attributes} - * @param bean Current bean that will be injected - * @param beanName Current bean name that will be injected - * @param injectedType the type of injected-object - * @param injectedElement {@link InjectionMetadata.InjectedElement} - * @return Bean cache key - */ - protected abstract String buildInjectedObjectCacheKey(AnnotationAttributes attributes, Object bean, String beanName, - Class<?> injectedType, - InjectionMetadata.InjectedElement injectedElement); - - /** - * Get {@link Map} in injected field. - * - * @return non-null ready-only {@link Map} - */ - protected Map<InjectionMetadata.InjectedElement, Object> getInjectedFieldObjectsMap() { - - Map<InjectionMetadata.InjectedElement, Object> injectedElementBeanMap = - new LinkedHashMap<InjectionMetadata.InjectedElement, Object>(); - - for (AnnotationInjectedBeanPostProcessor.AnnotatedInjectionMetadata metadata : injectionMetadataCache.values()) { - - Collection<AnnotationInjectedBeanPostProcessor.AnnotatedFieldElement> fieldElements = metadata.getFieldElements(); - - for (AnnotationInjectedBeanPostProcessor.AnnotatedFieldElement fieldElement : fieldElements) { - - injectedElementBeanMap.put(fieldElement, fieldElement.bean); - - } - - } - - return Collections.unmodifiableMap(injectedElementBeanMap); - - } - - /** - * Get {@link Map} in injected method. - * - * @return non-null {@link Map} - */ - protected Map<InjectionMetadata.InjectedElement, Object> getInjectedMethodObjectsMap() { - - Map<InjectionMetadata.InjectedElement, Object> injectedElementBeanMap = - new LinkedHashMap<InjectionMetadata.InjectedElement, Object>(); - - for (AnnotationInjectedBeanPostProcessor.AnnotatedInjectionMetadata metadata : injectionMetadataCache.values()) { - - Collection<AnnotationInjectedBeanPostProcessor.AnnotatedMethodElement> methodElements = metadata.getMethodElements(); - - for (AnnotationInjectedBeanPostProcessor.AnnotatedMethodElement methodElement : methodElements) { - - injectedElementBeanMap.put(methodElement, methodElement.object); - - } - - } - - return Collections.unmodifiableMap(injectedElementBeanMap); - - } - - /** - * {@link Annotation Annotated} {@link InjectionMetadata} implementation - */ - private class AnnotatedInjectionMetadata extends InjectionMetadata { - - private final Collection<AnnotationInjectedBeanPostProcessor.AnnotatedFieldElement> fieldElements; - - private final Collection<AnnotationInjectedBeanPostProcessor.AnnotatedMethodElement> methodElements; - - public AnnotatedInjectionMetadata(Class<?> targetClass, Collection<AnnotationInjectedBeanPostProcessor.AnnotatedFieldElement> fieldElements, - Collection<AnnotationInjectedBeanPostProcessor.AnnotatedMethodElement> methodElements) { - super(targetClass, combine(fieldElements, methodElements)); - this.fieldElements = fieldElements; - this.methodElements = methodElements; - } - - public Collection<AnnotationInjectedBeanPostProcessor.AnnotatedFieldElement> getFieldElements() { - return fieldElements; - } - - public Collection<AnnotationInjectedBeanPostProcessor.AnnotatedMethodElement> getMethodElements() { - return methodElements; - } - } - - /** - * {@link Annotation Annotated} {@link Method} {@link InjectionMetadata.InjectedElement} - */ - private class AnnotatedMethodElement extends InjectionMetadata.InjectedElement { - - private final Method method; - - private final AnnotationAttributes attributes; - - private volatile Object object; - - protected AnnotatedMethodElement(Method method, PropertyDescriptor pd, AnnotationAttributes attributes) { - super(method, pd); - this.method = method; - this.attributes = attributes; - } - - @Override - protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable { - - Class<?> injectedType = pd.getPropertyType(); - - Object injectedObject = getInjectedObject(attributes, bean, beanName, injectedType, this); - - ReflectionUtils.makeAccessible(method); - - method.invoke(bean, injectedObject); - - } - - } - - /** - * {@link Annotation Annotated} {@link Field} {@link InjectionMetadata.InjectedElement} - */ - public class AnnotatedFieldElement extends InjectionMetadata.InjectedElement { - - private final Field field; - - private final AnnotationAttributes attributes; - - private volatile Object bean; - - protected AnnotatedFieldElement(Field field, AnnotationAttributes attributes) { - super(field, null); - this.field = field; - this.attributes = attributes; - } - - @Override - protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable { - - Class<?> injectedType = field.getType(); - - Object injectedObject = getInjectedObject(attributes, bean, beanName, injectedType, this); - - ReflectionUtils.makeAccessible(field); - - field.set(bean, injectedObject); - - } - - } -} diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationPropertyValuesAdapter.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationPropertyValuesAdapter.java index 5566c39..80cdfb7 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationPropertyValuesAdapter.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationPropertyValuesAdapter.java @@ -24,8 +24,7 @@ import org.springframework.core.env.PropertyResolver; import java.lang.annotation.Annotation; import java.util.Map; -import static org.apache.dubbo.config.spring.util.AnnotationUtils.getAttributes; -import static org.apache.dubbo.config.spring.util.AnnotationUtils.resolvePlaceholders; +import static com.alibaba.spring.util.AnnotationUtils.getAttributes; /** * {@link Annotation} {@link PropertyValues} Adapter @@ -46,7 +45,7 @@ class AnnotationPropertyValuesAdapter implements PropertyValues { */ public AnnotationPropertyValuesAdapter(Map<String, Object> attributes, PropertyResolver propertyResolver, String... ignoreAttributeNames) { - this.delegate = new MutablePropertyValues(resolvePlaceholders(attributes, propertyResolver, ignoreAttributeNames)); + this.delegate = new MutablePropertyValues(getAttributes(attributes, propertyResolver, ignoreAttributeNames)); } public AnnotationPropertyValuesAdapter(Annotation annotation, PropertyResolver propertyResolver, diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/DubboConfigAliasPostProcessor.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/DubboConfigAliasPostProcessor.java index 8293289..28b6a6c 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/DubboConfigAliasPostProcessor.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/DubboConfigAliasPostProcessor.java @@ -25,7 +25,7 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; -import static org.apache.dubbo.config.spring.util.BeanRegistrar.hasAlias; +import static com.alibaba.spring.util.BeanRegistrar.hasAlias; import static org.springframework.util.ObjectUtils.nullSafeEquals; import static org.springframework.util.StringUtils.hasText; 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 548ffe1..668af04 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 @@ -21,8 +21,8 @@ import org.apache.dubbo.config.annotation.Service; import org.apache.dubbo.config.spring.ReferenceBean; import org.apache.dubbo.config.spring.ServiceBean; import org.apache.dubbo.config.spring.context.event.ServiceBeanExportedEvent; -import org.apache.dubbo.config.spring.util.AnnotationUtils; +import com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor; import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.InjectionMetadata; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; @@ -44,9 +44,10 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import static com.alibaba.spring.util.AnnotationUtils.getAttribute; +import static com.alibaba.spring.util.AnnotationUtils.getAttributes; import static java.lang.reflect.Proxy.newProxyInstance; import static org.apache.dubbo.config.spring.beans.factory.annotation.ServiceBeanNameBuilder.create; -import static org.apache.dubbo.config.spring.util.AnnotationUtils.getAttribute; import static org.springframework.util.StringUtils.hasText; /** @@ -55,7 +56,7 @@ import static org.springframework.util.StringUtils.hasText; * * @since 2.5.7 */ -public class ReferenceAnnotationBeanPostProcessor extends AnnotationInjectedBeanPostProcessor implements +public class ReferenceAnnotationBeanPostProcessor extends AbstractAnnotationBeanPostProcessor implements ApplicationContextAware, ApplicationListener { /** @@ -299,7 +300,7 @@ public class ReferenceAnnotationBeanPostProcessor extends AnnotationInjectedBean Class<?> injectedType, InjectionMetadata.InjectedElement injectedElement) { return buildReferencedBeanName(attributes, injectedType) + "#source=" + (injectedElement.getMember()) + - "#attributes=" + AnnotationUtils.resolvePlaceholders(attributes, getEnvironment()); + "#attributes=" + getAttributes(attributes, getEnvironment()); } /** diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceBeanBuilder.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceBeanBuilder.java index 55cb646..f6aacf4 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceBeanBuilder.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceBeanBuilder.java @@ -34,11 +34,11 @@ import java.beans.PropertyEditorSupport; import java.util.List; import java.util.Map; -import static org.apache.dubbo.config.spring.util.AnnotationUtils.getAttribute; -import static org.apache.dubbo.config.spring.util.AnnotationUtils.getAttributes; -import static org.apache.dubbo.config.spring.util.AnnotationUtils.resolveServiceInterfaceClass; -import static org.apache.dubbo.config.spring.util.BeanFactoryUtils.getNullableBean; -import static org.apache.dubbo.config.spring.util.ObjectUtils.of; +import static com.alibaba.spring.util.AnnotationUtils.getAttribute; +import static com.alibaba.spring.util.AnnotationUtils.getAttributes; +import static com.alibaba.spring.util.BeanFactoryUtils.getOptionalBean; +import static com.alibaba.spring.util.ObjectUtils.of; +import static org.apache.dubbo.config.spring.util.DubboAnnotationUtils.resolveServiceInterfaceClass; import static org.springframework.core.annotation.AnnotationAttributes.fromMap; import static org.springframework.util.StringUtils.commaDelimitedListToStringArray; @@ -81,7 +81,7 @@ class ReferenceBeanBuilder extends AnnotatedInterfaceConfigBeanBuilder<Reference String consumerBeanName = getAttribute(attributes, "consumer"); - ConsumerConfig consumerConfig = getNullableBean(applicationContext, consumerBeanName, ConsumerConfig.class); + ConsumerConfig consumerConfig = getOptionalBean(applicationContext, consumerBeanName, ConsumerConfig.class); referenceBean.setConsumer(consumerConfig); diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java index ce8b196..c96f7b9 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java @@ -63,9 +63,9 @@ import java.util.List; import java.util.Map; import java.util.Set; +import static com.alibaba.spring.util.ObjectUtils.of; import static org.apache.dubbo.config.spring.beans.factory.annotation.ServiceBeanNameBuilder.create; -import static org.apache.dubbo.config.spring.util.AnnotationUtils.resolveServiceInterfaceClass; -import static org.apache.dubbo.config.spring.util.ObjectUtils.of; +import static org.apache.dubbo.config.spring.util.DubboAnnotationUtils.resolveServiceInterfaceClass; import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition; import static org.springframework.context.annotation.AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR; import static org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation; diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilder.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilder.java index 8aef87f..7cd04ec 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilder.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilder.java @@ -25,8 +25,8 @@ import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.env.Environment; import org.springframework.util.StringUtils; -import static org.apache.dubbo.config.spring.util.AnnotationUtils.getAttribute; -import static org.apache.dubbo.config.spring.util.AnnotationUtils.resolveInterfaceName; +import static com.alibaba.spring.util.AnnotationUtils.getAttribute; +import static org.apache.dubbo.config.spring.util.DubboAnnotationUtils.resolveInterfaceName; import static org.springframework.core.annotation.AnnotationUtils.getAnnotationAttributes; /** diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboComponentScanRegistrar.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboComponentScanRegistrar.java index 6e14d56..7d66b72 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboComponentScanRegistrar.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboComponentScanRegistrar.java @@ -19,7 +19,6 @@ package org.apache.dubbo.config.spring.context.annotation; import org.apache.dubbo.config.annotation.Service; import org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor; import org.apache.dubbo.config.spring.beans.factory.annotation.ServiceAnnotationBeanPostProcessor; -import org.apache.dubbo.config.spring.util.BeanRegistrar; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.config.BeanDefinition; @@ -37,6 +36,7 @@ import java.util.Collections; import java.util.LinkedHashSet; import java.util.Set; +import static com.alibaba.spring.util.BeanRegistrar.registerInfrastructureBean; import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition; /** @@ -87,7 +87,7 @@ public class DubboComponentScanRegistrar implements ImportBeanDefinitionRegistra private void registerReferenceAnnotationBeanPostProcessor(BeanDefinitionRegistry registry) { // Register @Reference Annotation Bean Processor - BeanRegistrar.registerInfrastructureBean(registry, + registerInfrastructureBean(registry, ReferenceAnnotationBeanPostProcessor.BEAN_NAME, ReferenceAnnotationBeanPostProcessor.class); } diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigBindingRegistrar.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigBindingRegistrar.java index 894a363..62f5b69 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigBindingRegistrar.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigBindingRegistrar.java @@ -21,6 +21,7 @@ import org.apache.dubbo.config.spring.beans.factory.annotation.DubboConfigBindin import org.apache.dubbo.config.spring.beans.factory.config.ConfigurableSourceBeanMetadataElement; import org.apache.dubbo.config.spring.context.config.NamePropertyDefaultValueDubboConfigBeanCustomizer; +import com.alibaba.spring.beans.factory.annotation.ConfigurationBeanBindingRegistrar; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.config.BeanDefinition; @@ -43,12 +44,13 @@ import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; +import static com.alibaba.spring.util.BeanRegistrar.registerInfrastructureBean; +import static com.alibaba.spring.util.PropertySourcesUtils.getSubProperties; +import static com.alibaba.spring.util.PropertySourcesUtils.normalizePrefix; import static org.apache.dubbo.config.spring.context.config.NamePropertyDefaultValueDubboConfigBeanCustomizer.BEAN_NAME; -import static org.apache.dubbo.config.spring.util.BeanRegistrar.registerInfrastructureBean; -import static org.apache.dubbo.config.spring.util.PropertySourcesUtils.buildPrefix; -import static org.apache.dubbo.config.spring.util.PropertySourcesUtils.getPrefixedProperties; import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition; import static org.springframework.beans.factory.support.BeanDefinitionReaderUtils.registerWithGeneratedName; +import static org.springframework.core.annotation.AnnotationAttributes.fromMap; /** * {@link AbstractConfig Dubbo Config} binding Bean registrar @@ -56,7 +58,9 @@ import static org.springframework.beans.factory.support.BeanDefinitionReaderUtil * @see EnableDubboConfigBinding * @see DubboConfigBindingBeanPostProcessor * @since 2.5.8 + * @deprecated it will be removed in future, please use {@link ConfigurationBeanBindingRegistrar} for replacement */ +@Deprecated public class DubboConfigBindingRegistrar implements ImportBeanDefinitionRegistrar, EnvironmentAware, ConfigurableSourceBeanMetadataElement { @@ -67,8 +71,7 @@ public class DubboConfigBindingRegistrar implements ImportBeanDefinitionRegistra @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { - AnnotationAttributes attributes = AnnotationAttributes.fromMap( - importingClassMetadata.getAnnotationAttributes(EnableDubboConfigBinding.class.getName())); + AnnotationAttributes attributes = fromMap(importingClassMetadata.getAnnotationAttributes(EnableDubboConfigBinding.class.getName())); registerBeanDefinitions(attributes, registry); @@ -91,7 +94,7 @@ public class DubboConfigBindingRegistrar implements ImportBeanDefinitionRegistra boolean multiple, BeanDefinitionRegistry registry) { - Map<String, Object> properties = getPrefixedProperties(environment.getPropertySources(), prefix); + Map<String, Object> properties = getSubProperties(environment.getPropertySources(), prefix); if (CollectionUtils.isEmpty(properties)) { if (log.isDebugEnabled()) { @@ -144,7 +147,7 @@ public class DubboConfigBindingRegistrar implements ImportBeanDefinitionRegistra BeanDefinitionBuilder builder = rootBeanDefinition(processorClass); - String actualPrefix = multiple ? buildPrefix(prefix) + beanName : prefix; + String actualPrefix = multiple ? normalizePrefix(prefix) + beanName : prefix; builder.addConstructorArgValue(actualPrefix).addConstructorArgValue(beanName); diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigBindingsRegistrar.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigBindingsRegistrar.java index b420fe1..d1ff887 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigBindingsRegistrar.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigBindingsRegistrar.java @@ -18,6 +18,7 @@ package org.apache.dubbo.config.spring.context.annotation; import org.apache.dubbo.config.AbstractConfig; +import com.alibaba.spring.beans.factory.annotation.ConfigurationBeanBindingsRegister; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.context.EnvironmentAware; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; @@ -33,7 +34,9 @@ import org.springframework.util.Assert; * @see EnableDubboConfigBindings * @see DubboConfigBindingRegistrar * @since 2.5.8 + * @deprecated it will be removed in future, please use {@link ConfigurationBeanBindingsRegister} for replacement */ +@Deprecated public class DubboConfigBindingsRegistrar implements ImportBeanDefinitionRegistrar, EnvironmentAware { private ConfigurableEnvironment environment; diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfiguration.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfiguration.java index 4f9b5a8..414b827 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfiguration.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfiguration.java @@ -28,14 +28,17 @@ import org.apache.dubbo.config.ProviderConfig; import org.apache.dubbo.config.RegistryConfig; import org.apache.dubbo.config.spring.ConfigCenterBean; +import com.alibaba.spring.beans.factory.annotation.EnableConfigurationBeanBinding; +import com.alibaba.spring.beans.factory.annotation.EnableConfigurationBeanBindings; import org.springframework.context.annotation.Configuration; /** * Dubbo {@link AbstractConfig Config} {@link Configuration} * + * @revised 2.7.5 * @see Configuration - * @see EnableDubboConfigBindings - * @see EnableDubboConfigBinding + * @see EnableConfigurationBeanBindings + * @see EnableConfigurationBeanBinding * @see ApplicationConfig * @see ModuleConfig * @see RegistryConfig @@ -51,17 +54,17 @@ public class DubboConfigConfiguration { /** * Single Dubbo {@link AbstractConfig Config} Bean Binding */ - @EnableDubboConfigBindings({ - @EnableDubboConfigBinding(prefix = "dubbo.application", type = ApplicationConfig.class), - @EnableDubboConfigBinding(prefix = "dubbo.module", type = ModuleConfig.class), - @EnableDubboConfigBinding(prefix = "dubbo.registry", type = RegistryConfig.class), - @EnableDubboConfigBinding(prefix = "dubbo.protocol", type = ProtocolConfig.class), - @EnableDubboConfigBinding(prefix = "dubbo.monitor", type = MonitorConfig.class), - @EnableDubboConfigBinding(prefix = "dubbo.provider", type = ProviderConfig.class), - @EnableDubboConfigBinding(prefix = "dubbo.consumer", type = ConsumerConfig.class), - @EnableDubboConfigBinding(prefix = "dubbo.config-center", type = ConfigCenterBean.class), - @EnableDubboConfigBinding(prefix = "dubbo.metadata-report", type = MetadataReportConfig.class), - @EnableDubboConfigBinding(prefix = "dubbo.metrics", type = MetricsConfig.class) + @EnableConfigurationBeanBindings({ + @EnableConfigurationBeanBinding(prefix = "dubbo.application", type = ApplicationConfig.class), + @EnableConfigurationBeanBinding(prefix = "dubbo.module", type = ModuleConfig.class), + @EnableConfigurationBeanBinding(prefix = "dubbo.registry", type = RegistryConfig.class), + @EnableConfigurationBeanBinding(prefix = "dubbo.protocol", type = ProtocolConfig.class), + @EnableConfigurationBeanBinding(prefix = "dubbo.monitor", type = MonitorConfig.class), + @EnableConfigurationBeanBinding(prefix = "dubbo.provider", type = ProviderConfig.class), + @EnableConfigurationBeanBinding(prefix = "dubbo.consumer", type = ConsumerConfig.class), + @EnableConfigurationBeanBinding(prefix = "dubbo.config-center", type = ConfigCenterBean.class), + @EnableConfigurationBeanBinding(prefix = "dubbo.metadata-report", type = MetadataReportConfig.class), + @EnableConfigurationBeanBinding(prefix = "dubbo.metrics", type = MetricsConfig.class) }) public static class Single { @@ -70,17 +73,17 @@ public class DubboConfigConfiguration { /** * Multiple Dubbo {@link AbstractConfig Config} Bean Binding */ - @EnableDubboConfigBindings({ - @EnableDubboConfigBinding(prefix = "dubbo.applications", type = ApplicationConfig.class, multiple = true), - @EnableDubboConfigBinding(prefix = "dubbo.modules", type = ModuleConfig.class, multiple = true), - @EnableDubboConfigBinding(prefix = "dubbo.registries", type = RegistryConfig.class, multiple = true), - @EnableDubboConfigBinding(prefix = "dubbo.protocols", type = ProtocolConfig.class, multiple = true), - @EnableDubboConfigBinding(prefix = "dubbo.monitors", type = MonitorConfig.class, multiple = true), - @EnableDubboConfigBinding(prefix = "dubbo.providers", type = ProviderConfig.class, multiple = true), - @EnableDubboConfigBinding(prefix = "dubbo.consumers", type = ConsumerConfig.class, multiple = true), - @EnableDubboConfigBinding(prefix = "dubbo.config-centers", type = ConfigCenterBean.class, multiple = true), - @EnableDubboConfigBinding(prefix = "dubbo.metadata-reports", type = MetadataReportConfig.class, multiple = true), - @EnableDubboConfigBinding(prefix = "dubbo.metricses", type = MetricsConfig.class, multiple = true) + @EnableConfigurationBeanBindings({ + @EnableConfigurationBeanBinding(prefix = "dubbo.applications", type = ApplicationConfig.class, multiple = true), + @EnableConfigurationBeanBinding(prefix = "dubbo.modules", type = ModuleConfig.class, multiple = true), + @EnableConfigurationBeanBinding(prefix = "dubbo.registries", type = RegistryConfig.class, multiple = true), + @EnableConfigurationBeanBinding(prefix = "dubbo.protocols", type = ProtocolConfig.class, multiple = true), + @EnableConfigurationBeanBinding(prefix = "dubbo.monitors", type = MonitorConfig.class, multiple = true), + @EnableConfigurationBeanBinding(prefix = "dubbo.providers", type = ProviderConfig.class, multiple = true), + @EnableConfigurationBeanBinding(prefix = "dubbo.consumers", type = ConsumerConfig.class, multiple = true), + @EnableConfigurationBeanBinding(prefix = "dubbo.config-centers", type = ConfigCenterBean.class, multiple = true), + @EnableConfigurationBeanBinding(prefix = "dubbo.metadata-reports", type = MetadataReportConfig.class, multiple = true), + @EnableConfigurationBeanBinding(prefix = "dubbo.metricses", type = MetricsConfig.class, multiple = true) }) public static class Multiple { diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfigurationRegistrar.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfigurationRegistrar.java index 18f260a..79db7f4 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfigurationRegistrar.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfigurationRegistrar.java @@ -25,8 +25,8 @@ import org.springframework.core.Ordered; import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.type.AnnotationMetadata; -import static org.apache.dubbo.config.spring.util.AnnotatedBeanDefinitionRegistryUtils.registerBeans; -import static org.apache.dubbo.config.spring.util.BeanRegistrar.registerInfrastructureBean; +import static com.alibaba.spring.util.AnnotatedBeanDefinitionRegistryUtils.registerBeans; +import static com.alibaba.spring.util.BeanRegistrar.registerInfrastructureBean; /** * Dubbo {@link AbstractConfig Config} {@link ImportBeanDefinitionRegistrar register}, which order can be configured diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboLifecycleComponentRegistrar.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboLifecycleComponentRegistrar.java index 6a23114..c192c8d 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboLifecycleComponentRegistrar.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboLifecycleComponentRegistrar.java @@ -23,7 +23,7 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.core.type.AnnotationMetadata; -import static org.apache.dubbo.config.spring.util.AnnotatedBeanDefinitionRegistryUtils.registerBeans; +import static com.alibaba.spring.util.AnnotatedBeanDefinitionRegistryUtils.registerBeans; /** * A {@link ImportBeanDefinitionRegistrar register} for the {@link Lifecycle Dubbo Lifecycle} components diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfigBinding.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfigBinding.java index 54fc337..14fabf9 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfigBinding.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfigBinding.java @@ -22,6 +22,7 @@ import org.apache.dubbo.config.ModuleConfig; import org.apache.dubbo.config.RegistryConfig; import org.apache.dubbo.config.spring.beans.factory.annotation.DubboConfigBindingBeanPostProcessor; +import com.alibaba.spring.beans.factory.annotation.EnableConfigurationBeanBinding; import org.springframework.context.annotation.Import; import org.springframework.core.env.PropertySources; @@ -44,13 +45,16 @@ import java.lang.annotation.Target; * @see DubboConfigBindingRegistrar * @see DubboConfigBindingBeanPostProcessor * @see EnableDubboConfigBindings + * @see EnableConfigurationBeanBinding * @since 2.5.8 + * @deprecated it will be removed in future, please use {@link EnableConfigurationBeanBinding} for replacement */ @Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Repeatable(EnableDubboConfigBindings.class) @Import(DubboConfigBindingRegistrar.class) +@Deprecated public @interface EnableDubboConfigBinding { /** diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfigBindings.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfigBindings.java index 8c26479..5fde1a4 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfigBindings.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfigBindings.java @@ -16,6 +16,7 @@ */ package org.apache.dubbo.config.spring.context.annotation; +import com.alibaba.spring.beans.factory.annotation.EnableConfigurationBeanBindings; import org.springframework.context.annotation.Import; import java.lang.annotation.Annotation; @@ -28,13 +29,15 @@ import java.lang.annotation.Target; /** * Multiple {@link EnableDubboConfigBinding} {@link Annotation} * - * @since 2.5.8 * @see EnableDubboConfigBinding + * @since 2.5.8 + * @deprecated it will be removed in future, please use {@link EnableConfigurationBeanBindings} for replacement */ @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Import(DubboConfigBindingsRegistrar.class) +@Deprecated public @interface EnableDubboConfigBindings { /** diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/config/NamePropertyDefaultValueDubboConfigBeanCustomizer.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/config/NamePropertyDefaultValueDubboConfigBeanCustomizer.java index 2f8c446..cc1753d 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/config/NamePropertyDefaultValueDubboConfigBeanCustomizer.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/config/NamePropertyDefaultValueDubboConfigBeanCustomizer.java @@ -24,7 +24,7 @@ import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.util.Arrays; -import static org.apache.dubbo.config.spring.util.ObjectUtils.of; +import static com.alibaba.spring.util.ObjectUtils.of; import static org.springframework.beans.BeanUtils.getPropertyDescriptor; /** diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/properties/DefaultDubboConfigBinder.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/properties/DefaultDubboConfigBinder.java index c989a88..1c87e95 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/properties/DefaultDubboConfigBinder.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/properties/DefaultDubboConfigBinder.java @@ -23,7 +23,7 @@ import org.springframework.validation.DataBinder; import java.util.Map; -import static org.apache.dubbo.config.spring.util.PropertySourcesUtils.getPrefixedProperties; +import static com.alibaba.spring.util.PropertySourcesUtils.getSubProperties; /** * Default {@link DubboConfigBinder} implementation based on Spring {@link DataBinder} @@ -37,7 +37,7 @@ public class DefaultDubboConfigBinder extends AbstractDubboConfigBinder { dataBinder.setIgnoreInvalidFields(isIgnoreInvalidFields()); dataBinder.setIgnoreUnknownFields(isIgnoreUnknownFields()); // Get properties under specified prefix from PropertySources - Map<String, Object> properties = getPrefixedProperties(getPropertySources(), prefix); + Map<String, Object> properties = getSubProperties(getPropertySources(), prefix); // Convert Map to MutablePropertyValues MutablePropertyValues propertyValues = new MutablePropertyValues(properties); // Bind diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/extension/SpringExtensionFactory.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/extension/SpringExtensionFactory.java index 1aed3a6..863a677 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/extension/SpringExtensionFactory.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/extension/SpringExtensionFactory.java @@ -22,7 +22,7 @@ import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.ConcurrentHashSet; import org.apache.dubbo.config.DubboShutdownHook; -import org.apache.dubbo.config.spring.util.BeanFactoryUtils; +import org.apache.dubbo.config.spring.util.ApplicationContextUtils; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoUniqueBeanDefinitionException; @@ -49,7 +49,7 @@ public class SpringExtensionFactory implements ExtensionFactory { ((ConfigurableApplicationContext) context).registerShutdownHook(); DubboShutdownHook.getDubboShutdownHook().unregister(); } - BeanFactoryUtils.addApplicationListener(context, SHUTDOWN_HOOK_LISTENER); + ApplicationContextUtils.addApplicationListener(context, SHUTDOWN_HOOK_LISTENER); } public static void removeApplicationContext(ApplicationContext context) { diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/AnnotationBeanDefinitionParser.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/AnnotationBeanDefinitionParser.java index 4a8e963..600197f 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/AnnotationBeanDefinitionParser.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/AnnotationBeanDefinitionParser.java @@ -18,7 +18,6 @@ package org.apache.dubbo.config.spring.schema; import org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor; import org.apache.dubbo.config.spring.beans.factory.annotation.ServiceAnnotationBeanPostProcessor; -import org.apache.dubbo.config.spring.util.BeanRegistrar; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.config.BeanDefinition; @@ -28,12 +27,12 @@ import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; import org.w3c.dom.Element; +import static com.alibaba.spring.util.BeanRegistrar.registerInfrastructureBean; import static org.springframework.util.StringUtils.commaDelimitedListToStringArray; import static org.springframework.util.StringUtils.trimArrayElements; /** * @link BeanDefinitionParser} - * * @see ServiceAnnotationBeanPostProcessor * @see ReferenceAnnotationBeanPostProcessor * @since 2.5.9 @@ -79,7 +78,7 @@ public class AnnotationBeanDefinitionParser extends AbstractSingleBeanDefinition private void registerReferenceAnnotationBeanPostProcessor(BeanDefinitionRegistry registry) { // Register @Reference Annotation Bean Processor - BeanRegistrar.registerInfrastructureBean(registry, + registerInfrastructureBean(registry, ReferenceAnnotationBeanPostProcessor.BEAN_NAME, ReferenceAnnotationBeanPostProcessor.class); } diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/DubboBeanDefinitionParser.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/DubboBeanDefinitionParser.java index 4bbced6..d2a3e08 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/DubboBeanDefinitionParser.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/DubboBeanDefinitionParser.java @@ -53,8 +53,8 @@ import java.util.HashSet; import java.util.Set; import java.util.regex.Pattern; +import static com.alibaba.spring.util.BeanRegistrar.registerInfrastructureBean; import static org.apache.dubbo.common.constants.CommonConstants.HIDE_KEY_PREFIX; -import static org.apache.dubbo.config.spring.util.BeanRegistrar.registerInfrastructureBean; /** * AbstractBeanDefinitionParser diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/DubboNamespaceHandler.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/DubboNamespaceHandler.java index ae6ea43..3013eaf 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/DubboNamespaceHandler.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/DubboNamespaceHandler.java @@ -31,8 +31,8 @@ import org.apache.dubbo.config.spring.ReferenceBean; import org.apache.dubbo.config.spring.ServiceBean; import org.apache.dubbo.config.spring.beans.factory.config.ConfigurableSourceBeanMetadataElement; import org.apache.dubbo.config.spring.context.DubboLifecycleComponentApplicationListener; -import org.apache.dubbo.config.spring.util.AnnotatedBeanDefinitionRegistryUtils; +import com.alibaba.spring.util.AnnotatedBeanDefinitionRegistryUtils; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.xml.NamespaceHandlerSupport; @@ -40,7 +40,7 @@ import org.springframework.beans.factory.xml.ParserContext; import org.springframework.context.annotation.AnnotationConfigUtils; import org.w3c.dom.Element; -import static org.apache.dubbo.config.spring.util.AnnotatedBeanDefinitionRegistryUtils.registerBeans; +import static com.alibaba.spring.util.AnnotatedBeanDefinitionRegistryUtils.registerBeans; /** * DubboNamespaceHandler 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 deleted file mode 100644 index 69cb7d8..0000000 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtils.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * 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.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.Iterator; -import java.util.LinkedHashSet; -import java.util.Objects; -import java.util.Set; - -import static java.lang.String.format; -import static java.util.Arrays.asList; -import static org.springframework.util.ClassUtils.resolveClassName; - -/** - * Annotated {@link BeanDefinition} Utilities - * <p> - * - * @since 2.6.6 - */ -public abstract class AnnotatedBeanDefinitionRegistryUtils { - - private static final Log logger = LogFactory.getLog(AnnotatedBeanDefinitionRegistryUtils.class); - - /** - * 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) { - - if (ObjectUtils.isEmpty(annotatedClasses)) { - return; - } - - Set<Class<?>> classesToRegister = new LinkedHashSet<>(asList(annotatedClasses)); - - // Remove all annotated-classes that have been registered - Iterator<Class<?>> iterator = classesToRegister.iterator(); - - while (iterator.hasNext()) { - Class<?> annotatedClass = iterator.next(); - if (isPresentBean(registry, annotatedClass)) { - iterator.remove(); - } - } - - AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(registry); - - if (logger.isDebugEnabled()) { - logger.debug(registry.getClass().getSimpleName() + " will register annotated classes : " + asList(annotatedClasses) + " ."); - } - - reader.register(classesToRegister.toArray(new Class[0])); - - // clear - classesToRegister.clear(); - } -} diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotationUtils.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotationUtils.java deleted file mode 100644 index 148048f..0000000 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotationUtils.java +++ /dev/null @@ -1,310 +0,0 @@ -/* - * 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.apache.dubbo.config.annotation.Reference; -import org.apache.dubbo.config.annotation.Service; - -import org.springframework.core.annotation.AnnotationAttributes; -import org.springframework.core.env.Environment; -import org.springframework.core.env.PropertyResolver; -import org.springframework.util.Assert; -import org.springframework.util.ClassUtils; - -import java.lang.annotation.Annotation; -import java.lang.reflect.AnnotatedElement; -import java.util.LinkedHashMap; -import java.util.Map; - -import static java.lang.String.valueOf; -import static java.util.Collections.emptyMap; -import static java.util.Collections.unmodifiableMap; -import static org.springframework.core.annotation.AnnotatedElementUtils.getMergedAnnotation; -import static org.springframework.core.annotation.AnnotationAttributes.fromMap; -import static org.springframework.core.annotation.AnnotationUtils.getAnnotationAttributes; -import static org.springframework.core.annotation.AnnotationUtils.getDefaultValue; -import static org.springframework.util.ClassUtils.getAllInterfacesForClass; -import static org.springframework.util.ClassUtils.resolveClassName; -import static org.springframework.util.CollectionUtils.isEmpty; -import static org.springframework.util.ObjectUtils.containsElement; -import static org.springframework.util.ObjectUtils.nullSafeEquals; -import static org.springframework.util.StringUtils.hasText; -import static org.springframework.util.StringUtils.trimWhitespace; - -/** - * Annotation Utilities Class - * - * @see org.springframework.core.annotation.AnnotationUtils - * @since 2.5.11 - */ -public class AnnotationUtils { - - - @Deprecated - public static String resolveInterfaceName(Service service, Class<?> defaultInterfaceClass) - throws IllegalStateException { - - String interfaceName; - if (hasText(service.interfaceName())) { - interfaceName = service.interfaceName(); - } else if (!void.class.equals(service.interfaceClass())) { - interfaceName = service.interfaceClass().getName(); - } else if (defaultInterfaceClass.isInterface()) { - interfaceName = defaultInterfaceClass.getName(); - } else { - throw new IllegalStateException( - "The @Service undefined interfaceClass or interfaceName, and the type " - + defaultInterfaceClass.getName() + " is not a interface."); - } - - return interfaceName; - - } - - /** - * Resolve the interface name from {@link AnnotationAttributes} - * - * @param attributes {@link AnnotationAttributes} instance, may be {@link Service @Service} or {@link Reference @Reference} - * @param defaultInterfaceClass the default {@link Class class} of interface - * @return the interface name if found - * @throws IllegalStateException if interface name was not found - */ - public static String resolveInterfaceName(AnnotationAttributes attributes, Class<?> defaultInterfaceClass) { - Boolean generic = getAttribute(attributes, "generic"); - if (generic != null && generic) { - // it's a generic reference - String interfaceClassName = getAttribute(attributes, "interfaceName"); - Assert.hasText(interfaceClassName, - "@Reference interfaceName() must be present when reference a generic service!"); - return interfaceClassName; - } - return resolveServiceInterfaceClass(attributes, defaultInterfaceClass).getName(); - } - - /** - * Get the attribute value - * - * @param attributes {@link AnnotationAttributes the annotation attributes} - * @param name the name of attribute - * @param <T> the type of attribute value - * @return the attribute value if found - * @since 2.7.3 - */ - public static <T> T getAttribute(AnnotationAttributes attributes, String name) { - return (T) attributes.get(name); - } - - /** - * Resolve the {@link Class class} of Dubbo Service interface from the specified - * {@link AnnotationAttributes annotation attributes} and annotated {@link Class class}. - * - * @param attributes {@link AnnotationAttributes annotation attributes} - * @param defaultInterfaceClass the annotated {@link Class class}. - * @return the {@link Class class} of Dubbo Service interface - * @throws IllegalArgumentException if can't resolved - */ - public static Class<?> resolveServiceInterfaceClass(AnnotationAttributes attributes, Class<?> defaultInterfaceClass) - throws IllegalArgumentException { - - ClassLoader classLoader = defaultInterfaceClass != null ? defaultInterfaceClass.getClassLoader() : Thread.currentThread().getContextClassLoader(); - - Class<?> interfaceClass = getAttribute(attributes, "interfaceClass"); - - if (void.class.equals(interfaceClass)) { // default or set void.class for purpose. - - interfaceClass = null; - - String interfaceClassName = getAttribute(attributes, "interfaceName"); - - if (hasText(interfaceClassName)) { - if (ClassUtils.isPresent(interfaceClassName, classLoader)) { - interfaceClass = resolveClassName(interfaceClassName, classLoader); - } - } - - } - - if (interfaceClass == null && defaultInterfaceClass != null) { - // Find all interfaces from the annotated class - // To resolve an issue : https://github.com/apache/dubbo/issues/3251 - Class<?>[] allInterfaces = getAllInterfacesForClass(defaultInterfaceClass); - - if (allInterfaces.length > 0) { - interfaceClass = allInterfaces[0]; - } - - } - - Assert.notNull(interfaceClass, - "@Service interfaceClass() or interfaceName() or interface class must be present!"); - - Assert.isTrue(interfaceClass.isInterface(), - "The annotated type must be an interface!"); - - return interfaceClass; - } - - @Deprecated - public static String resolveInterfaceName(Reference reference, Class<?> defaultInterfaceClass) - throws IllegalStateException { - - String interfaceName; - if (!"".equals(reference.interfaceName())) { - interfaceName = reference.interfaceName(); - } else if (!void.class.equals(reference.interfaceClass())) { - interfaceName = reference.interfaceClass().getName(); - } else if (defaultInterfaceClass.isInterface()) { - interfaceName = defaultInterfaceClass.getName(); - } else { - throw new IllegalStateException( - "The @Reference undefined interfaceClass or interfaceName, and the type " - + defaultInterfaceClass.getName() + " is not a interface."); - } - - return interfaceName; - - } - - /** - * Get the {@link Annotation} attributes - * - * @param annotation specified {@link Annotation} - * @param ignoreDefaultValue whether ignore default value or not - * @param ignoreAttributeNames the attribute names of annotation should be ignored - * @return non-null - * @since 2.6.6 - * @deprecated - */ - @Deprecated - public static Map<String, Object> getAttributes(Annotation annotation, boolean ignoreDefaultValue, - String... ignoreAttributeNames) { - return getAttributes(annotation, null, ignoreDefaultValue, ignoreAttributeNames); - } - - /** - * Get the {@link Annotation} attributes - * - * @param annotation specified {@link Annotation} - * @param propertyResolver {@link PropertyResolver} instance, e.g {@link Environment} - * @param ignoreDefaultValue whether ignore default value or not - * @param ignoreAttributeNames the attribute names of annotation should be ignored - * @return non-null - * @since 2.6.6 - */ - public static Map<String, Object> getAttributes(Annotation annotation, PropertyResolver propertyResolver, - boolean ignoreDefaultValue, String... ignoreAttributeNames) { - - if (annotation == null) { - return emptyMap(); - } - - Map<String, Object> attributes = getAnnotationAttributes(annotation); - - Map<String, Object> actualAttributes = new LinkedHashMap<>(); - - for (Map.Entry<String, Object> entry : attributes.entrySet()) { - - String attributeName = entry.getKey(); - Object attributeValue = entry.getValue(); - - // ignore default attribute value - if (ignoreDefaultValue && nullSafeEquals(attributeValue, getDefaultValue(annotation, attributeName))) { - continue; - } - actualAttributes.put(attributeName, attributeValue); - } - - return resolvePlaceholders(actualAttributes, propertyResolver, ignoreAttributeNames); - } - - /** - * Resolve the placeholders from the specified annotation attributes - * - * @param sourceAnnotationAttributes the source of annotation attributes - * @param propertyResolver {@link PropertyResolver} - * @param ignoreAttributeNames the attribute names to be ignored - * @return a new resolved annotation attributes , non-null and read-only - * @since 2.7.3 - */ - public static Map<String, Object> resolvePlaceholders(Map<String, Object> sourceAnnotationAttributes, - PropertyResolver propertyResolver, - String... ignoreAttributeNames) { - - if (isEmpty(sourceAnnotationAttributes)) { - return emptyMap(); - } - - Map<String, Object> resolvedAnnotationAttributes = new LinkedHashMap<>(); - - for (Map.Entry<String, Object> entry : sourceAnnotationAttributes.entrySet()) { - - String attributeName = entry.getKey(); - - // ignore attribute name to skip - if (containsElement(ignoreAttributeNames, attributeName)) { - continue; - } - - Object attributeValue = entry.getValue(); - - if (attributeValue instanceof String) { - attributeValue = resolvePlaceholders(valueOf(attributeValue), propertyResolver); - } else if (attributeValue instanceof String[]) { - String[] values = (String[]) attributeValue; - for (int i = 0; i < values.length; i++) { - values[i] = resolvePlaceholders(values[i], propertyResolver); - } - attributeValue = values; - } - - resolvedAnnotationAttributes.put(attributeName, attributeValue); - } - - return unmodifiableMap(resolvedAnnotationAttributes); - } - - /** - * Get {@link AnnotationAttributes the annotation attributes} after merging and resolving the placeholders - * - * @param annotatedElement {@link AnnotatedElement the annotated element} - * @param annotationType the {@link Class tyoe} pf {@link Annotation annotation} - * @param propertyResolver {@link PropertyResolver} instance, e.g {@link Environment} - * @param ignoreDefaultValue whether ignore default value or not - * @param ignoreAttributeNames the attribute names of annotation should be ignored - * @return If the specified annotation type is not found, return <code>null</code> - * @since 2.7.3 - */ - public static AnnotationAttributes getMergedAttributes(AnnotatedElement annotatedElement, - Class<? extends Annotation> annotationType, - PropertyResolver propertyResolver, - boolean ignoreDefaultValue, - String... ignoreAttributeNames) { - Annotation annotation = getMergedAnnotation(annotatedElement, annotationType); - return annotation == null ? null : fromMap(getAttributes(annotation, propertyResolver, ignoreDefaultValue, ignoreAttributeNames)); - - } - - private static String resolvePlaceholders(String attributeValue, PropertyResolver propertyResolver) { - String resolvedValue = attributeValue; - if (propertyResolver != null) { - resolvedValue = propertyResolver.resolvePlaceholders(resolvedValue); - resolvedValue = trimWhitespace(resolvedValue); - } - return resolvedValue; - } - -} diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/BeanFactoryUtils.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/ApplicationContextUtils.java similarity index 57% rename from dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/BeanFactoryUtils.java rename to dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/ApplicationContextUtils.java index 76f7379..72d1e1c 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/BeanFactoryUtils.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/ApplicationContextUtils.java @@ -17,19 +17,12 @@ package org.apache.dubbo.config.spring.util; import org.springframework.beans.factory.BeanFactory; -import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationListener; import org.springframework.context.support.AbstractApplicationContext; import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import static java.util.Collections.emptyList; -import static org.springframework.util.ObjectUtils.isEmpty; /** * {@link BeanFactory} Utilities class @@ -39,7 +32,7 @@ import static org.springframework.util.ObjectUtils.isEmpty; * @see org.springframework.beans.factory.BeanFactoryUtils * @since 2.5.7 */ -public class BeanFactoryUtils { +public class ApplicationContextUtils { public static boolean addApplicationListener(ApplicationContext applicationContext, ApplicationListener listener) { try { @@ -64,51 +57,4 @@ public class BeanFactoryUtils { } return false; } - - /** - * Get nullable Bean - * - * @param beanFactory {@link ListableBeanFactory} - * @param beanName the name of Bean - * @param beanType the {@link Class type} of Bean - * @param <T> the {@link Class type} of Bean - * @return A bean if present , or <code>null</code> - */ - public static <T> T getNullableBean(ListableBeanFactory beanFactory, String beanName, Class<T> beanType) { - T bean = null; - try { - bean = beanFactory.getBean(beanName, beanType); - } catch (Throwable ignored) { - // Any exception will be ignored to handle - } - return bean; - } - - - /** - * Gets name-matched Beans from {@link ListableBeanFactory BeanFactory} - * - * @param beanFactory {@link ListableBeanFactory BeanFactory} - * @param beanNames the names of Bean - * @param beanType the {@link Class type} of Bean - * @param <T> the {@link Class type} of Bean - * @return the read-only and non-null {@link List} of Bean names - */ - public static <T> List<T> getBeans(ListableBeanFactory beanFactory, String[] beanNames, Class<T> beanType) { - - if (isEmpty(beanNames)) { - return emptyList(); - } - - List<T> beans = new ArrayList<T>(beanNames.length); - - for (String beanName : beanNames) { - T bean = getNullableBean(beanFactory, beanName, beanType); - if (bean != null) { - beans.add(bean); - } - } - - return Collections.unmodifiableList(beans); - } } diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/BeanRegistrar.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/BeanRegistrar.java deleted file mode 100644 index 04dd601..0000000 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/BeanRegistrar.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * 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.springframework.beans.factory.config.BeanDefinition; -import org.springframework.beans.factory.support.BeanDefinitionRegistry; -import org.springframework.beans.factory.support.RootBeanDefinition; -import org.springframework.core.AliasRegistry; - -import static org.springframework.util.ObjectUtils.containsElement; -import static org.springframework.util.StringUtils.hasText; - -/** - * Bean Registrar - * - * @since 2.5.7 - */ -public class BeanRegistrar { - - /** - * Register Infrastructure Bean - * - * @param beanDefinitionRegistry {@link BeanDefinitionRegistry} - * @param beanType the type of bean - * @param beanName the name of bean - */ - public static void registerInfrastructureBean(BeanDefinitionRegistry beanDefinitionRegistry, - String beanName, - Class<?> beanType) { - - if (!beanDefinitionRegistry.containsBeanDefinition(beanName)) { - RootBeanDefinition beanDefinition = new RootBeanDefinition(beanType); - beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); - beanDefinitionRegistry.registerBeanDefinition(beanName, beanDefinition); - } - - } - - /** - * Detect the alias is present or not in the given bean name from {@link AliasRegistry} - * - * @param registry {@link AliasRegistry} - * @param beanName the bean name - * @param alias alias to test - * @return if present, return <code>true</code>, or <code>false</code> - */ - public static boolean hasAlias(AliasRegistry registry, String beanName, String alias) { - return hasText(beanName) && hasText(alias) && containsElement(registry.getAliases(beanName), alias); - } -} diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/ClassUtils.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/ClassUtils.java deleted file mode 100644 index 29cd637..0000000 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/ClassUtils.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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 java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; - -/** - * {@link Class} 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/ClassUtils.java - * - * @since 2.6.6 - */ -public abstract class ClassUtils { - - public static <T> Class<T> resolveGenericType(Class<?> declaredClass) { - ParameterizedType parameterizedType = (ParameterizedType) declaredClass.getGenericSuperclass(); - Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); - return (Class<T>) actualTypeArguments[0]; - } -} diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/DubboAnnotationUtils.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/DubboAnnotationUtils.java new file mode 100644 index 0000000..7cf3527 --- /dev/null +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/DubboAnnotationUtils.java @@ -0,0 +1,150 @@ +/* + * 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.apache.dubbo.config.annotation.Reference; +import org.apache.dubbo.config.annotation.Service; + +import org.springframework.core.annotation.AnnotationAttributes; +import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; + +import static com.alibaba.spring.util.AnnotationUtils.getAttribute; +import static org.springframework.util.ClassUtils.getAllInterfacesForClass; +import static org.springframework.util.ClassUtils.resolveClassName; +import static org.springframework.util.StringUtils.hasText; + +/** + * Dubbbo Annotation Utilities Class + * + * @see org.springframework.core.annotation.AnnotationUtils + * @since 2.5.11 + */ +public class DubboAnnotationUtils { + + + @Deprecated + public static String resolveInterfaceName(Service service, Class<?> defaultInterfaceClass) + throws IllegalStateException { + + String interfaceName; + if (hasText(service.interfaceName())) { + interfaceName = service.interfaceName(); + } else if (!void.class.equals(service.interfaceClass())) { + interfaceName = service.interfaceClass().getName(); + } else if (defaultInterfaceClass.isInterface()) { + interfaceName = defaultInterfaceClass.getName(); + } else { + throw new IllegalStateException( + "The @Service undefined interfaceClass or interfaceName, and the type " + + defaultInterfaceClass.getName() + " is not a interface."); + } + + return interfaceName; + + } + + /** + * Resolve the interface name from {@link AnnotationAttributes} + * + * @param attributes {@link AnnotationAttributes} instance, may be {@link Service @Service} or {@link Reference @Reference} + * @param defaultInterfaceClass the default {@link Class class} of interface + * @return the interface name if found + * @throws IllegalStateException if interface name was not found + */ + public static String resolveInterfaceName(AnnotationAttributes attributes, Class<?> defaultInterfaceClass) { + Boolean generic = getAttribute(attributes, "generic"); + if (generic != null && generic) { + // it's a generic reference + String interfaceClassName = getAttribute(attributes, "interfaceName"); + Assert.hasText(interfaceClassName, + "@Reference interfaceName() must be present when reference a generic service!"); + return interfaceClassName; + } + return resolveServiceInterfaceClass(attributes, defaultInterfaceClass).getName(); + } + + /** + * Resolve the {@link Class class} of Dubbo Service interface from the specified + * {@link AnnotationAttributes annotation attributes} and annotated {@link Class class}. + * + * @param attributes {@link AnnotationAttributes annotation attributes} + * @param defaultInterfaceClass the annotated {@link Class class}. + * @return the {@link Class class} of Dubbo Service interface + * @throws IllegalArgumentException if can't resolved + */ + public static Class<?> resolveServiceInterfaceClass(AnnotationAttributes attributes, Class<?> defaultInterfaceClass) + throws IllegalArgumentException { + + ClassLoader classLoader = defaultInterfaceClass != null ? defaultInterfaceClass.getClassLoader() : Thread.currentThread().getContextClassLoader(); + + Class<?> interfaceClass = getAttribute(attributes, "interfaceClass"); + + if (void.class.equals(interfaceClass)) { // default or set void.class for purpose. + + interfaceClass = null; + + String interfaceClassName = getAttribute(attributes, "interfaceName"); + + if (hasText(interfaceClassName)) { + if (ClassUtils.isPresent(interfaceClassName, classLoader)) { + interfaceClass = resolveClassName(interfaceClassName, classLoader); + } + } + + } + + if (interfaceClass == null && defaultInterfaceClass != null) { + // Find all interfaces from the annotated class + // To resolve an issue : https://github.com/apache/dubbo/issues/3251 + Class<?>[] allInterfaces = getAllInterfacesForClass(defaultInterfaceClass); + + if (allInterfaces.length > 0) { + interfaceClass = allInterfaces[0]; + } + + } + + Assert.notNull(interfaceClass, + "@Service interfaceClass() or interfaceName() or interface class must be present!"); + + Assert.isTrue(interfaceClass.isInterface(), + "The annotated type must be an interface!"); + + return interfaceClass; + } + + @Deprecated + public static String resolveInterfaceName(Reference reference, Class<?> defaultInterfaceClass) + throws IllegalStateException { + + String interfaceName; + if (!"".equals(reference.interfaceName())) { + interfaceName = reference.interfaceName(); + } else if (!void.class.equals(reference.interfaceClass())) { + interfaceName = reference.interfaceClass().getName(); + } else if (defaultInterfaceClass.isInterface()) { + interfaceName = defaultInterfaceClass.getName(); + } else { + throw new IllegalStateException( + "The @Reference undefined interfaceClass or interfaceName, and the type " + + defaultInterfaceClass.getName() + " is not a interface."); + } + + return interfaceName; + } +} diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/PropertySourcesUtils.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/PropertySourcesUtils.java deleted file mode 100644 index 007f17f..0000000 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/PropertySourcesUtils.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * 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.springframework.core.env.EnumerablePropertySource; -import org.springframework.core.env.MutablePropertySources; -import org.springframework.core.env.PropertyResolver; -import org.springframework.core.env.PropertySource; -import org.springframework.core.env.PropertySources; -import org.springframework.core.env.PropertySourcesPropertyResolver; - -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Properties; - -import static java.util.Collections.unmodifiableMap; - - -/** - * {@link PropertySources} Utilities - * <p> - * - * @since 2.6.6 - */ -public abstract class PropertySourcesUtils { - - /** - * Get prefixed {@link Properties} - * - * @param propertySources {@link PropertySource} Iterable - * @param prefix the prefix of property name - * @return Map - * @see Properties - */ - public static Map<String, Object> getPrefixedProperties(Iterable<PropertySource<?>> propertySources, String prefix) { - - MutablePropertySources mutablePropertySources = new MutablePropertySources(); - - for (PropertySource<?> source : propertySources) { - mutablePropertySources.addLast(source); - } - - return getPrefixedProperties(mutablePropertySources, prefix); - - } - - /** - * Get prefixed {@link Properties} - * - * @param propertySources {@link PropertySources} - * @param prefix the prefix of property name - * @return Map - * @see Properties - */ - public static Map<String, Object> getPrefixedProperties(PropertySources propertySources, String prefix) { - - PropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources); - - Map<String, Object> prefixedProperties = new LinkedHashMap<>(); - - String normalizedPrefix = buildPrefix(prefix); - - Iterator<PropertySource<?>> iterator = propertySources.iterator(); - - while (iterator.hasNext()) { - PropertySource<?> source = iterator.next(); - if (source instanceof EnumerablePropertySource) { - for (String name : ((EnumerablePropertySource<?>) source).getPropertyNames()) { - if (!prefixedProperties.containsKey(name) && name.startsWith(normalizedPrefix)) { - String subName = name.substring(normalizedPrefix.length()); - if (!prefixedProperties.containsKey(subName)) { // take first one - Object value = source.getProperty(name); - if (value instanceof String) { - // Resolve placeholder - value = propertyResolver.resolvePlaceholders((String) value); - } - prefixedProperties.put(subName, value); - } - } - } - } - } - - return unmodifiableMap(prefixedProperties); - } - - /** - * Build the prefix - * - * @param prefix the prefix - * @return the prefix - */ - public static String buildPrefix(String prefix) { - if (prefix.endsWith(".")) { - return prefix; - } else { - return prefix + "."; - } - } -} diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessorTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessorTest.java index 83ec889..efc18ea 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessorTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessorTest.java @@ -97,17 +97,19 @@ public class ReferenceAnnotationBeanPostProcessorTest { TestBean testBean = context.getBean(TestBean.class); DemoService demoService = testBean.getDemoService(); + Map<String, DemoService> demoServicesMap = context.getBeansOfType(DemoService.class); + + Assert.assertNotNull(testBean.getDemoServiceFromAncestor()); + Assert.assertNotNull(testBean.getDemoServiceFromParent()); + Assert.assertNotNull(testBean.getDemoService()); + Assert.assertNotNull(testBean.autowiredDemoService); + Assert.assertEquals(1, demoServicesMap.size()); Assert.assertEquals("Hello,Mercy", demoService.sayName("Mercy")); Assert.assertEquals("Greeting, Mercy", defaultHelloService.sayHello("Mercy")); Assert.assertEquals("Hello, Mercy", helloServiceImpl.sayHello("Mercy")); - Assert.assertEquals("Greeting, Mercy", helloService.sayHello("Mercy")); - Assert.assertNotNull(testBean.getDemoServiceFromAncestor()); - Assert.assertNotNull(testBean.getDemoServiceFromParent()); - Assert.assertNotNull(testBean.getDemoService()); - Assert.assertNotNull(testBean.autowiredDemoService); Assert.assertEquals("Hello,Mercy", testBean.getDemoServiceFromAncestor().sayName("Mercy")); Assert.assertEquals("Hello,Mercy", testBean.getDemoServiceFromParent().sayName("Mercy")); @@ -118,9 +120,6 @@ public class ReferenceAnnotationBeanPostProcessorTest { Assert.assertEquals("Hello,Mercy", myDemoService.sayName("Mercy")); - Map<String, DemoService> demoServicesMap = context.getBeansOfType(DemoService.class); - - Assert.assertEquals(1, demoServicesMap.size()); for (DemoService demoService1 : demoServicesMap.values()) { @@ -165,7 +164,7 @@ public class ReferenceAnnotationBeanPostProcessorTest { InjectionMetadata.InjectedElement injectedElement = entry.getKey(); - Assert.assertEquals("org.apache.dubbo.config.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor$AnnotatedFieldElement", + Assert.assertEquals("com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor$AnnotatedFieldElement", injectedElement.getClass().getName()); } @@ -187,7 +186,7 @@ public class ReferenceAnnotationBeanPostProcessorTest { InjectionMetadata.InjectedElement injectedElement = entry.getKey(); - Assert.assertEquals("org.apache.dubbo.config.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor$AnnotatedMethodElement", + Assert.assertEquals("com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor$AnnotatedMethodElement", injectedElement.getClass().getName()); } diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfigTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfigTest.java index 3086600..3aabd80 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfigTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfigTest.java @@ -32,10 +32,11 @@ import org.springframework.context.annotation.PropertySource; import java.util.Map; -import static org.apache.dubbo.config.spring.util.BeanRegistrar.hasAlias; +import static com.alibaba.spring.util.BeanRegistrar.hasAlias; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; + /** * {@link EnableDubboConfig} Test * 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 deleted file mode 100644 index 847cee0..0000000 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtilsTest.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * 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.BeforeEach; -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(); - - @BeforeEach - public void init() { - context.setAllowBeanDefinitionOverriding(false); - } - - @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); - - registerBeans(context, A.class); - - - context.refresh(); - - A a = context.getBean(A.class); - - Assert.assertNotNull(a); - } - - - @Service - static class A { - - } -} diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/ObjectUtils.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/ApplicationContextUtilsTest.java similarity index 53% rename from dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/ObjectUtils.java rename to dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/ApplicationContextUtilsTest.java index 57832cc..b7ebb00 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/ObjectUtils.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/ApplicationContextUtilsTest.java @@ -16,22 +16,31 @@ */ package org.apache.dubbo.config.spring.util; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.GenericApplicationContext; + +import static org.apache.dubbo.config.spring.util.ApplicationContextUtils.addApplicationListener; +import static org.junit.jupiter.api.Assertions.assertTrue; + /** - * Object Utilities + * {@link ApplicationContextUtils} Test * - * @since 2.6.6 + * @since 2.5.7 */ -public abstract class ObjectUtils { +public class ApplicationContextUtilsTest { - /** - * Convert from variable arguments to array - * - * @param values variable arguments - * @param <T> The class - * @return array - */ - public static <T> T[] of(T... values) { - return values; + private ApplicationContext applicationContext; + + @BeforeEach + public void init() { + applicationContext = new GenericApplicationContext(); } + @Test + public void testAddApplicationListener() { + assertTrue(addApplicationListener(applicationContext, event -> { + })); + } } diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/BeanFactoryUtilsTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/BeanFactoryUtilsTest.java deleted file mode 100644 index f265f28..0000000 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/BeanFactoryUtilsTest.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * 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.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.stereotype.Component; - -import java.util.List; - -/** - * {@link BeanFactoryUtils} Test - * - * @since 2.5.7 - */ -public class BeanFactoryUtilsTest { - - private AnnotationConfigApplicationContext applicationContext; - - @BeforeEach - public void init() { - applicationContext = new AnnotationConfigApplicationContext(); - } - - @Test - public void testGetNullableBean() { - - applicationContext.register(TestBean.class); - - applicationContext.refresh(); - - TestBean testBean = BeanFactoryUtils.getNullableBean(applicationContext, "testBean", TestBean.class); - - Assertions.assertNotNull(testBean); - - Assertions.assertEquals("Hello,World", testBean.getName()); - - } - - @Test - public void testGetNullableBeanIfAbsent() { - - applicationContext.refresh(); - - TestBean testBean = BeanFactoryUtils.getNullableBean(applicationContext, "testBean", TestBean.class); - - Assertions.assertNull(testBean); - } - - @Test - public void testGetBeans() { - - applicationContext.register(TestBean.class, TestBean2.class); - - applicationContext.refresh(); - - List<TestBean> testBeans = BeanFactoryUtils.getBeans(applicationContext, new String[]{"testBean"}, TestBean.class); - - Assertions.assertEquals(1, testBeans.size()); - - Assertions.assertEquals("Hello,World", testBeans.get(0).getName()); - - } - - @Test - public void testGetBeansIfAbsent() { - - applicationContext.refresh(); - - List<TestBean> testBeans = BeanFactoryUtils.getBeans(applicationContext, new String[]{"testBean"}, TestBean.class); - - Assertions.assertTrue(testBeans.isEmpty()); - - } - - - @Component("testBean2") - private static class TestBean2 extends TestBean { - - } - - @Component("testBean") - private static class TestBean { - - private String name = "Hello,World"; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - } -} diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/PropertySourcesUtilsTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/PropertySourcesUtilsTest.java deleted file mode 100644 index 01ce07d..0000000 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/PropertySourcesUtilsTest.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * 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.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.springframework.core.env.MapPropertySource; -import org.springframework.core.env.MutablePropertySources; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -/** - * {@link PropertySourcesUtils} Test - * - * @see PropertySourcesUtils - * @since 2.5.8 - */ -public class PropertySourcesUtilsTest { - - @Test - public void testGetPrefixedProperties() { - - MutablePropertySources propertySources = new MutablePropertySources(); - - Map<String, Object> source = new HashMap<String, Object>(); - Map<String, Object> source2 = new HashMap<String, Object>(); - - MapPropertySource propertySource = new MapPropertySource("propertySource", source); - MapPropertySource propertySource2 = new MapPropertySource("propertySource2", source2); - - propertySources.addLast(propertySource); - propertySources.addLast(propertySource2); - - Map<String, Object> result = PropertySourcesUtils.getPrefixedProperties(propertySources, "user"); - - Assertions.assertEquals(Collections.emptyMap(), result); - - source.put("age", "31"); - source.put("user.name", "Mercy"); - source.put("user.age", "${age}"); - - source2.put("user.name", "mercyblitz"); - source2.put("user.age", "32"); - - Map<String, Object> expected = new HashMap<String, Object>(); - expected.put("name", "Mercy"); - expected.put("age", "31"); - - result = PropertySourcesUtils.getPrefixedProperties(propertySources, "user"); - Assertions.assertEquals(expected, result); - - result = PropertySourcesUtils.getPrefixedProperties(propertySources, ""); - - Assertions.assertEquals(Collections.emptyMap(), result); - - result = PropertySourcesUtils.getPrefixedProperties(propertySources, "no-exists"); - - Assertions.assertEquals(Collections.emptyMap(), result); - - } - -} \ No newline at end of file diff --git a/dubbo-dependencies-bom/pom.xml b/dubbo-dependencies-bom/pom.xml index 76168b7..f0658ea 100644 --- a/dubbo-dependencies-bom/pom.xml +++ b/dubbo-dependencies-bom/pom.xml @@ -144,6 +144,9 @@ <!-- Eureka --> <eureka.version>1.9.12</eureka.version> + <!-- Alibaba --> + <alibaba_spring_context_support_version>1.0.4-SNAPSHOT</alibaba_spring_context_support_version> + <jaxb_version>2.2.7</jaxb_version> <activation_version>1.2.0</activation_version> <test_container_version>1.11.2</test_container_version> @@ -277,6 +280,14 @@ <artifactId>eureka-core</artifactId> <version>${eureka.version}</version> </dependency> + + <!-- Alibaba --> + <dependency> + <groupId>com.alibaba.spring</groupId> + <artifactId>spring-context-support</artifactId> + <version>${alibaba_spring_context_support_version}</version> + </dependency> + <dependency> <groupId>com.googlecode.xmemcached</groupId> <artifactId>xmemcached</artifactId>
