forked from apolloconfig/apollo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
client's SPI for customizing spring processors' loading (apolloconfig…
- Loading branch information
Showing
18 changed files
with
282 additions
and
66 deletions.
There are no files selected for viewing
39 changes: 6 additions & 33 deletions
39
...ent/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloConfigRegistrar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,20 @@ | ||
package com.ctrip.framework.apollo.spring.annotation; | ||
|
||
import com.ctrip.framework.apollo.spring.property.SpringValueDefinitionProcessor; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import com.ctrip.framework.apollo.spring.spi.ApolloConfigRegistrarHelper; | ||
import com.ctrip.framework.foundation.internals.ServiceBootstrap; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; | ||
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; | ||
import org.springframework.core.annotation.AnnotationAttributes; | ||
import org.springframework.core.type.AnnotationMetadata; | ||
|
||
import com.ctrip.framework.apollo.spring.config.PropertySourcesProcessor; | ||
import com.ctrip.framework.apollo.spring.util.BeanRegistrationUtil; | ||
import com.google.common.collect.Lists; | ||
|
||
/** | ||
* @author Jason Song([email protected]) | ||
*/ | ||
public class ApolloConfigRegistrar implements ImportBeanDefinitionRegistrar { | ||
@Override | ||
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { | ||
AnnotationAttributes attributes = AnnotationAttributes.fromMap(importingClassMetadata | ||
.getAnnotationAttributes(EnableApolloConfig.class.getName())); | ||
String[] namespaces = attributes.getStringArray("value"); | ||
int order = attributes.getNumber("order"); | ||
PropertySourcesProcessor.addNamespaces(Lists.newArrayList(namespaces), order); | ||
|
||
Map<String, Object> propertySourcesPlaceholderPropertyValues = new HashMap<>(); | ||
// to make sure the default PropertySourcesPlaceholderConfigurer's priority is higher than PropertyPlaceholderConfigurer | ||
propertySourcesPlaceholderPropertyValues.put("order", 0); | ||
|
||
BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, PropertySourcesPlaceholderConfigurer.class.getName(), | ||
PropertySourcesPlaceholderConfigurer.class, propertySourcesPlaceholderPropertyValues); | ||
|
||
BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, PropertySourcesProcessor.class.getName(), | ||
PropertySourcesProcessor.class); | ||
private ApolloConfigRegistrarHelper helper = ServiceBootstrap.loadPrimary(ApolloConfigRegistrarHelper.class); | ||
|
||
BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, ApolloAnnotationProcessor.class.getName(), | ||
ApolloAnnotationProcessor.class); | ||
|
||
BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, SpringValueProcessor.class.getName(), SpringValueProcessor.class); | ||
BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, SpringValueDefinitionProcessor.class.getName(), SpringValueDefinitionProcessor.class); | ||
|
||
BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, ApolloJsonValueProcessor.class.getName(), | ||
ApolloJsonValueProcessor.class); | ||
@Override | ||
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { | ||
helper.registerBeanDefinitions(importingClassMetadata, registry); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...ient/src/main/java/com/ctrip/framework/apollo/spring/spi/ApolloConfigRegistrarHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.ctrip.framework.apollo.spring.spi; | ||
|
||
import com.ctrip.framework.apollo.core.spi.Ordered; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
import org.springframework.core.type.AnnotationMetadata; | ||
|
||
public interface ApolloConfigRegistrarHelper extends Ordered { | ||
|
||
void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry); | ||
} |
10 changes: 10 additions & 0 deletions
10
...main/java/com/ctrip/framework/apollo/spring/spi/ConfigPropertySourcesProcessorHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.ctrip.framework.apollo.spring.spi; | ||
|
||
import com.ctrip.framework.apollo.core.spi.Ordered; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
|
||
public interface ConfigPropertySourcesProcessorHelper extends Ordered { | ||
|
||
void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException; | ||
} |
51 changes: 51 additions & 0 deletions
51
...c/main/java/com/ctrip/framework/apollo/spring/spi/DefaultApolloConfigRegistrarHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.ctrip.framework.apollo.spring.spi; | ||
|
||
import com.ctrip.framework.apollo.core.spi.Ordered; | ||
import com.ctrip.framework.apollo.spring.annotation.ApolloAnnotationProcessor; | ||
import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValueProcessor; | ||
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig; | ||
import com.ctrip.framework.apollo.spring.annotation.SpringValueProcessor; | ||
import com.ctrip.framework.apollo.spring.config.PropertySourcesProcessor; | ||
import com.ctrip.framework.apollo.spring.property.SpringValueDefinitionProcessor; | ||
import com.ctrip.framework.apollo.spring.util.BeanRegistrationUtil; | ||
import com.google.common.collect.Lists; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; | ||
import org.springframework.core.annotation.AnnotationAttributes; | ||
import org.springframework.core.type.AnnotationMetadata; | ||
|
||
public class DefaultApolloConfigRegistrarHelper implements ApolloConfigRegistrarHelper { | ||
|
||
@Override | ||
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { | ||
AnnotationAttributes attributes = AnnotationAttributes | ||
.fromMap(importingClassMetadata.getAnnotationAttributes(EnableApolloConfig.class.getName())); | ||
String[] namespaces = attributes.getStringArray("value"); | ||
int order = attributes.getNumber("order"); | ||
PropertySourcesProcessor.addNamespaces(Lists.newArrayList(namespaces), order); | ||
|
||
Map<String, Object> propertySourcesPlaceholderPropertyValues = new HashMap<>(); | ||
// to make sure the default PropertySourcesPlaceholderConfigurer's priority is higher than PropertyPlaceholderConfigurer | ||
propertySourcesPlaceholderPropertyValues.put("order", 0); | ||
|
||
BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, PropertySourcesPlaceholderConfigurer.class.getName(), | ||
PropertySourcesPlaceholderConfigurer.class, propertySourcesPlaceholderPropertyValues); | ||
BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, PropertySourcesProcessor.class.getName(), | ||
PropertySourcesProcessor.class); | ||
BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, ApolloAnnotationProcessor.class.getName(), | ||
ApolloAnnotationProcessor.class); | ||
BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, SpringValueProcessor.class.getName(), | ||
SpringValueProcessor.class); | ||
BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, SpringValueDefinitionProcessor.class.getName(), | ||
SpringValueDefinitionProcessor.class); | ||
BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, ApolloJsonValueProcessor.class.getName(), | ||
ApolloJsonValueProcessor.class); | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return Ordered.LOWEST_PRECEDENCE; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...va/com/ctrip/framework/apollo/spring/spi/DefaultConfigPropertySourcesProcessorHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.ctrip.framework.apollo.spring.spi; | ||
|
||
import com.ctrip.framework.apollo.core.spi.Ordered; | ||
import com.ctrip.framework.apollo.spring.annotation.ApolloAnnotationProcessor; | ||
import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValueProcessor; | ||
import com.ctrip.framework.apollo.spring.annotation.SpringValueProcessor; | ||
import com.ctrip.framework.apollo.spring.property.SpringValueDefinitionProcessor; | ||
import com.ctrip.framework.apollo.spring.util.BeanRegistrationUtil; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; | ||
|
||
public class DefaultConfigPropertySourcesProcessorHelper implements ConfigPropertySourcesProcessorHelper { | ||
|
||
@Override | ||
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { | ||
Map<String, Object> propertySourcesPlaceholderPropertyValues = new HashMap<>(); | ||
// to make sure the default PropertySourcesPlaceholderConfigurer's priority is higher than PropertyPlaceholderConfigurer | ||
propertySourcesPlaceholderPropertyValues.put("order", 0); | ||
|
||
BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, PropertySourcesPlaceholderConfigurer.class.getName(), | ||
PropertySourcesPlaceholderConfigurer.class, propertySourcesPlaceholderPropertyValues); | ||
BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, ApolloAnnotationProcessor.class.getName(), | ||
ApolloAnnotationProcessor.class); | ||
BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, SpringValueProcessor.class.getName(), | ||
SpringValueProcessor.class); | ||
BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, ApolloJsonValueProcessor.class.getName(), | ||
ApolloJsonValueProcessor.class); | ||
|
||
processSpringValueDefinition(registry); | ||
} | ||
|
||
/** | ||
* For Spring 3.x versions, the BeanDefinitionRegistryPostProcessor would not be instantiated if | ||
* it is added in postProcessBeanDefinitionRegistry phase, so we have to manually call the | ||
* postProcessBeanDefinitionRegistry method of SpringValueDefinitionProcessor here... | ||
*/ | ||
private void processSpringValueDefinition(BeanDefinitionRegistry registry) { | ||
SpringValueDefinitionProcessor springValueDefinitionProcessor = new SpringValueDefinitionProcessor(); | ||
|
||
springValueDefinitionProcessor.postProcessBeanDefinitionRegistry(registry); | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return Ordered.LOWEST_PRECEDENCE; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...urces/META-INF/services/com.ctrip.framework.apollo.spring.spi.ApolloConfigRegistrarHelper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.ctrip.framework.apollo.spring.spi.DefaultApolloConfigRegistrarHelper |
1 change: 1 addition & 0 deletions
1
...A-INF/services/com.ctrip.framework.apollo.spring.spi.ConfigPropertySourcesProcessorHelper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.ctrip.framework.apollo.spring.spi.DefaultConfigPropertySourcesProcessorHelper |
22 changes: 22 additions & 0 deletions
22
.../src/test/java/com/ctrip/framework/apollo/spring/spi/ApolloConfigRegistrarHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.ctrip.framework.apollo.spring.spi; | ||
|
||
import static org.springframework.test.util.AssertionErrors.assertEquals; | ||
|
||
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigRegistrar; | ||
import java.lang.reflect.Field; | ||
import org.junit.Test; | ||
import org.springframework.util.ReflectionUtils; | ||
|
||
public class ApolloConfigRegistrarHelperTest { | ||
|
||
@Test | ||
public void testHelperLoadingOrder() { | ||
ApolloConfigRegistrar apolloConfigRegistrar = new ApolloConfigRegistrar(); | ||
|
||
Field field = ReflectionUtils.findField(ApolloConfigRegistrar.class, "helper"); | ||
ReflectionUtils.makeAccessible(field); | ||
Object helper = ReflectionUtils.getField(field, apolloConfigRegistrar); | ||
|
||
assertEquals("helper is not TestRegistrarHelper instance", TestRegistrarHelper.class, helper.getClass()); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
.../java/com/ctrip/framework/apollo/spring/spi/ConfigPropertySourcesProcessorHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.ctrip.framework.apollo.spring.spi; | ||
|
||
import static org.springframework.test.util.AssertionErrors.assertEquals; | ||
|
||
import com.ctrip.framework.apollo.spring.config.ConfigPropertySourcesProcessor; | ||
import java.lang.reflect.Field; | ||
import org.junit.Test; | ||
import org.springframework.util.ReflectionUtils; | ||
|
||
public class ConfigPropertySourcesProcessorHelperTest { | ||
|
||
@Test | ||
public void testHelperLoadingOrder() { | ||
ConfigPropertySourcesProcessor processor = new ConfigPropertySourcesProcessor(); | ||
|
||
Field field = ReflectionUtils.findField(ConfigPropertySourcesProcessor.class, "helper"); | ||
ReflectionUtils.makeAccessible(field); | ||
Object helper = ReflectionUtils.getField(field, processor); | ||
|
||
assertEquals("helper is not TestProcessorHelper instance", TestProcessorHelper.class, helper.getClass()); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
apollo-client/src/test/java/com/ctrip/framework/apollo/spring/spi/TestProcessorHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.ctrip.framework.apollo.spring.spi; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
|
||
public class TestProcessorHelper extends DefaultConfigPropertySourcesProcessorHelper { | ||
|
||
@Override | ||
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) | ||
throws BeansException { | ||
super.postProcessBeanDefinitionRegistry(registry); | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return 0; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
apollo-client/src/test/java/com/ctrip/framework/apollo/spring/spi/TestRegistrarHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.ctrip.framework.apollo.spring.spi; | ||
|
||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
import org.springframework.core.type.AnnotationMetadata; | ||
|
||
public class TestRegistrarHelper extends DefaultApolloConfigRegistrarHelper { | ||
|
||
@Override | ||
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, | ||
BeanDefinitionRegistry registry) { | ||
super.registerBeanDefinitions(importingClassMetadata, registry); | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return 0; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...urces/META-INF/services/com.ctrip.framework.apollo.spring.spi.ApolloConfigRegistrarHelper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.ctrip.framework.apollo.spring.spi.TestRegistrarHelper |
1 change: 1 addition & 0 deletions
1
...A-INF/services/com.ctrip.framework.apollo.spring.spi.ConfigPropertySourcesProcessorHelper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.ctrip.framework.apollo.spring.spi.TestProcessorHelper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.