Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dubbo-5049]support @Bean on Config class to config id as alias #5063

Merged
merged 1 commit into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}