Skip to content

Commit

Permalink
[Dubbo-5049]support annotation config id property to be a alias for a…
Browse files Browse the repository at this point in the history
… config bean. Fixes apache#5049 (apache#5063)

LGTM
  • Loading branch information
sonymoon authored and mercyblitz committed Sep 19, 2019
1 parent 46486bb commit 87631aa
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.dubbo.config.spring.beans.factory.annotation;

import org.apache.dubbo.common.utils.Assert;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.AbstractConfig;
import org.apache.dubbo.config.spring.context.annotation.DubboConfigBindingRegistrar;
import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfigBinding;
Expand All @@ -29,6 +30,10 @@
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
Expand All @@ -49,7 +54,8 @@
* @since 2.5.8
*/

public class DubboConfigBindingBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware, InitializingBean {
public class DubboConfigBindingBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware, InitializingBean
, BeanDefinitionRegistryPostProcessor {

private final Log log = LogFactory.getLog(getClass());

Expand All @@ -67,6 +73,8 @@ public class DubboConfigBindingBeanPostProcessor implements BeanPostProcessor, A

private ApplicationContext applicationContext;

private BeanDefinitionRegistry beanDefinitionRegistry;

private boolean ignoreUnknownFields = true;

private boolean ignoreInvalidFields = true;
Expand Down Expand Up @@ -145,6 +153,15 @@ public void setDubboConfigBinder(DubboConfigBinder dubboConfigBinder) {

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof AbstractConfig) {
String id = ((AbstractConfig) bean).getId();
if (beanDefinitionRegistry != null && beanDefinitionRegistry instanceof DefaultListableBeanFactory) {
DefaultListableBeanFactory factory = (DefaultListableBeanFactory) beanDefinitionRegistry;
if (!StringUtils.isBlank(id) && !factory.hasAlias(beanName, id)) {
beanDefinitionRegistry.registerAlias(beanName, id);
}
}
}
return bean;
}

Expand Down Expand Up @@ -203,4 +220,15 @@ protected DubboConfigBinder createDubboConfigBinder(Environment environment) {
return defaultDubboConfigBinder;
}

@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
if (this.beanDefinitionRegistry == null) {
this.beanDefinitionRegistry = registry;
}
}

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
//do nothing here
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.dubbo.config.spring.beans.factory.annotation;

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.spring.context.config.NamePropertyDefaultValueDubboConfigBeanCustomizer;
import org.apache.dubbo.config.spring.context.properties.DefaultDubboConfigBinder;

Expand Down Expand Up @@ -52,6 +53,14 @@ public ApplicationConfig applicationConfig() {
return new ApplicationConfig();
}

@Bean("dubbo-demo-protocol")
public ProtocolConfig protocolConfig() {
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setName("dubbo");
protocolConfig.setId("customConfigAlias");
return protocolConfig;
}

@Bean
public DubboConfigBindingBeanPostProcessor bindingBeanPostProcessor() {
return new DubboConfigBindingBeanPostProcessor("dubbo.application", "dubbo-demo-application");
Expand All @@ -65,8 +74,15 @@ public void test() {

ApplicationConfig applicationConfig = applicationContext.getBean(ApplicationConfig.class);

String[] aliases = applicationContext.getAliases("dubbo-demo-protocol");
ProtocolConfig protocolConfigByName = applicationContext.getBean("dubbo-demo-protocol", ProtocolConfig.class);
ProtocolConfig protocolConfigById = applicationContext.getBean(protocolConfigByName.getId(), ProtocolConfig.class);

Assert.assertEquals("dubbo-demo-application", applicationConfig.getName());
Assert.assertEquals("mercyblitz", applicationConfig.getOwner());
Assert.assertEquals("Apache", applicationConfig.getOrganization());

Assert.assertArrayEquals(new String[]{"customConfigAlias"}, aliases);
Assert.assertEquals(protocolConfigByName, protocolConfigById);
}
}

0 comments on commit 87631aa

Please sign in to comment.