Skip to content

Commit

Permalink
Polish apache#3193 : [Enhancement] Change the default behavior of @En…
Browse files Browse the repository at this point in the history
…ableDubboConfig.multiple()
  • Loading branch information
mercyblitz committed Jan 16, 2019
1 parent 143b342 commit 989963e
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 75 deletions.
Expand Up @@ -19,32 +19,23 @@
import com.alibaba.dubbo.config.AbstractConfig;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;

import static com.alibaba.dubbo.config.spring.util.AnnotatedBeanDefinitionRegistryUtils.registerBeans;


/**
* Dubbo {@link AbstractConfig Config} {@link ImportBeanDefinitionRegistrar register}, which order can be configured
* Dubbo {@link AbstractConfig Config} {@link ImportBeanDefinitionRegistrar register}
*
* @see EnableDubboConfig
* @see DubboConfigConfiguration
* @see Ordered
* @since 2.5.8
*/
public class DubboConfigConfigurationRegistrar implements ImportBeanDefinitionRegistrar, EnvironmentAware, Ordered {

/**
* The property name of {@link EnableDubboConfig}'s {@link Ordered order}
*/
public static final String DUBBO_CONFIG_ORDER_PROPERTY_NAME = "dubbo.config.order";

private int order;
public class DubboConfigConfigurationRegistrar implements ImportBeanDefinitionRegistrar {

@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
Expand All @@ -54,34 +45,11 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B

boolean multiple = attributes.getBoolean("multiple");

if (multiple) {
// Single Config Bindings
registerBeans(registry, DubboConfigConfiguration.Single.class);

if (multiple) { // Since 2.6.6 https://github.com/apache/incubator-dubbo/issues/3193
registerBeans(registry, DubboConfigConfiguration.Multiple.class);
} else {
registerBeans(registry, DubboConfigConfiguration.Single.class);
}
}

/**
* Set {@link Ordered order}, it may be changed in in the future.
*
* @param order {@link Ordered order}
*/
public void setOrder(int order) {
this.order = order;
}

@Override
public int getOrder() {
return order;
}

@Override
public void setEnvironment(Environment environment) {
this.order = environment.getProperty(DUBBO_CONFIG_ORDER_PROPERTY_NAME, int.class, LOWEST_PRECEDENCE);
}

private static <T> T[] of(T... values) {
return values;
}

}
Expand Up @@ -46,7 +46,7 @@
* <li>{@link ConsumerConfig} binding to property : "dubbo.consumer"</li>
* </ul>
* <p>
* In contrast, on multiple bean bindings that requires to set {@link #multiple()} to be <code>true</code> :
* On multiple bean bindings that requires to set {@link #multiple()} to be <code>true</code> :
* <ul>
* <li>{@link ApplicationConfig} binding to property : "dubbo.applications"</li>
* <li>{@link ModuleConfig} binding to property : "dubbo.modules"</li>
Expand All @@ -71,10 +71,13 @@

/**
* It indicates whether binding to multiple Spring Beans.
* <p>
* Please note that if {@link #multiple()} is <code>true</code> since 2.6.6, the multiple bean bindings will be
* enabled, works with single bean bindings, rather than they are mutually exclusive before.
*
* @return the default value is <code>false</code>
* @return the default value is <code>true</code> since 2.6.6, the value is inverse earlier.
* @revised 2.5.9
*/
boolean multiple() default false;
boolean multiple() default true;

}
Expand Up @@ -23,6 +23,11 @@
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.PropertySource;

import java.util.Set;
import java.util.TreeSet;

import static java.util.Arrays.asList;

/**
* {@link DubboConfigBindingRegistrar}
*
Expand Down Expand Up @@ -55,9 +60,14 @@ public void testRegisterBeanDefinitionsForMultiple() {

context.refresh();

ApplicationConfig applicationConfig = context.getBean("applicationBean", ApplicationConfig.class);
Set<String> expectedBeanNames = new TreeSet<String>(asList("applicationBean1", "applicationBean2", "applicationBean3"));
Set<String> actualBeanNames = new TreeSet<String>(asList(context.getBeanNamesForType(ApplicationConfig.class)));

Assert.assertEquals("dubbo-demo-application", applicationConfig.getName());
Assert.assertEquals(expectedBeanNames, actualBeanNames);

ApplicationConfig applicationConfig = context.getBean("applicationBean1", ApplicationConfig.class);

Assert.assertEquals("dubbo-demo-application1", applicationConfig.getName());

applicationConfig = context.getBean("applicationBean2", ApplicationConfig.class);

Expand Down
Expand Up @@ -50,7 +50,7 @@ public void test() {

@EnableDubboConfigBindings({
@EnableDubboConfigBinding(prefix = "${application.prefix}", type = ApplicationConfig.class),
@EnableDubboConfigBinding(prefix = "dubbo.applications.applicationBean", type = ApplicationConfig.class)
@EnableDubboConfigBinding(prefix = "dubbo.applications.applicationBean1", type = ApplicationConfig.class)
})
@PropertySource("META-INF/config.properties")
private static class TestConfig {
Expand Down
Expand Up @@ -84,8 +84,8 @@ public void testMultiple() {
context.refresh();

// application
ApplicationConfig applicationConfig = context.getBean("applicationBean", ApplicationConfig.class);
Assert.assertEquals("dubbo-demo-application", applicationConfig.getName());
ApplicationConfig applicationConfig = context.getBean("applicationBean1", ApplicationConfig.class);
Assert.assertEquals("dubbo-demo-application1", applicationConfig.getName());

ApplicationConfig applicationBean2 = context.getBean("applicationBean2", ApplicationConfig.class);
Assert.assertEquals("dubbo-demo-application2", applicationBean2.getName());
Expand Down
Expand Up @@ -23,19 +23,17 @@
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.util.ObjectUtils;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.StandardEnvironment;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import static com.alibaba.dubbo.config.spring.context.annotation.DubboConfigConfigurationRegistrar.DUBBO_CONFIG_ORDER_PROPERTY_NAME;
import static java.util.Arrays.asList;

/**
* {@link EnableDubboConfig} Test
Expand All @@ -44,26 +42,6 @@
*/
public class EnableDubboConfigTest {

@Test
public void testOrder() {
Map<String, Object> source = new HashMap<String, Object>();
source.put(DUBBO_CONFIG_ORDER_PROPERTY_NAME, "1");
MapPropertySource propertySource = new MapPropertySource("test-property-source", source);
ConfigurableEnvironment environment = new StandardEnvironment();

environment.getPropertySources().addFirst(propertySource);

DubboConfigConfigurationRegistrar selector = new DubboConfigConfigurationRegistrar();

selector.setEnvironment(environment);

Assert.assertEquals(1, selector.getOrder());

selector.setOrder(2);

Assert.assertEquals(2, selector.getOrder());
}

@Test
public void testSingle() {

Expand All @@ -75,6 +53,8 @@ public void testSingle() {
ApplicationConfig applicationConfig = context.getBean("applicationBean", ApplicationConfig.class);
Assert.assertEquals("dubbo-demo-application", applicationConfig.getName());

Assert.assertArrayEquals(ObjectUtils.of("applicationBean"), context.getBeanNamesForType(ApplicationConfig.class));

// module
ModuleConfig moduleConfig = context.getBean("moduleBean", ModuleConfig.class);
Assert.assertEquals("dubbo-demo-module", moduleConfig.getName());
Expand Down Expand Up @@ -110,10 +90,18 @@ public void testMultiple() {
context.register(TestMultipleConfig.class);
context.refresh();

Set<String> expectedBeanNames = new TreeSet<String>(asList("applicationBean", "applicationBean1", "applicationBean2", "applicationBean3"));
Set<String> actualBeanNames = new TreeSet<String>(asList(context.getBeanNamesForType(ApplicationConfig.class)));

Assert.assertEquals(expectedBeanNames, actualBeanNames);

// application
ApplicationConfig applicationConfig = context.getBean("applicationBean", ApplicationConfig.class);
Assert.assertEquals("dubbo-demo-application", applicationConfig.getName());

applicationConfig = context.getBean("applicationBean1", ApplicationConfig.class);
Assert.assertEquals("dubbo-demo-application1", applicationConfig.getName());

ApplicationConfig applicationBean2 = context.getBean("applicationBean2", ApplicationConfig.class);
Assert.assertEquals("dubbo-demo-application2", applicationBean2.getName());

Expand All @@ -122,13 +110,13 @@ public void testMultiple() {

}

@EnableDubboConfig(multiple = true)
@EnableDubboConfig
@PropertySource("META-INF/config.properties")
private static class TestMultipleConfig {

}

@EnableDubboConfig
@EnableDubboConfig(multiple = false)
@PropertySource("META-INF/config.properties")
private static class TestConfig {

Expand Down
Expand Up @@ -27,6 +27,6 @@ dubbo.provider.host = 127.0.0.1
dubbo.consumer.client = netty

# multiple Bean definition
dubbo.applications.applicationBean.name = dubbo-demo-application
dubbo.applications.applicationBean1.name = dubbo-demo-application1
dubbo.applications.applicationBean2.name = dubbo-demo-application2
dubbo.applications.applicationBean3.name = dubbo-demo-application3

0 comments on commit 989963e

Please sign in to comment.