diff --git a/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/context/annotation/DubboConfigConfigurationSelector.java b/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/context/annotation/DubboConfigConfigurationSelector.java index 24f581b4d46..aa205fb9311 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/context/annotation/DubboConfigConfigurationSelector.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/context/annotation/DubboConfigConfigurationSelector.java @@ -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) { @@ -47,14 +57,26 @@ public String[] selectImports(AnnotationMetadata importingClassMetadata) { } } - private static 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[] of(T... values) { + return values; + } +} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/context/annotation/EnableDubboConfigTest.java b/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/context/annotation/EnableDubboConfigTest.java index d5c41b05bff..445cd2ba310 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/context/annotation/EnableDubboConfigTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/context/annotation/EnableDubboConfigTest.java @@ -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 @@ -36,6 +44,26 @@ */ public class EnableDubboConfigTest { + @Test + public void testOrder() { + Map source = new HashMap(); + 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() {