Skip to content

Commit

Permalink
Polish apache#2897 : DubboConfigConfigurationSelector precedence is t…
Browse files Browse the repository at this point in the history
…oo high
  • Loading branch information
mercyblitz committed Dec 7, 2018
1 parent 5fda4bc commit fa7130b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,29 @@

import com.alibaba.dubbo.config.AbstractConfig;

import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;

/**
* Dubbo {@link AbstractConfig Config} Registrar
* Dubbo {@link AbstractConfig Config} {@link ImportSelector} implementation, which order can be configured
*
* @see EnableDubboConfig
* @see DubboConfigConfiguration
* @see Ordered
* @since 2.5.8
*/
public class DubboConfigConfigurationSelector implements ImportSelector, Ordered {
public class DubboConfigConfigurationSelector implements ImportSelector, 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;

@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
Expand All @@ -47,14 +57,26 @@ public String[] selectImports(AnnotationMetadata importingClassMetadata) {
}
}

private static <T> T[] of(T... values) {
return values;
/**
* 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 HIGHEST_PRECEDENCE;
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
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 static com.alibaba.dubbo.config.spring.context.annotation.DubboConfigConfigurationSelector.DUBBO_CONFIG_ORDER_PROPERTY_NAME;

/**
* {@link EnableDubboConfig} Test
Expand All @@ -36,6 +44,26 @@
*/
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);

DubboConfigConfigurationSelector selector = new DubboConfigConfigurationSelector();

selector.setEnvironment(environment);

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

selector.setOrder(2);

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

@Test
public void testSingle() {

Expand Down

0 comments on commit fa7130b

Please sign in to comment.