Skip to content

Commit 9b06053

Browse files
committed
minor fix for Spring
1. make ApolloAnnotationProcessor run as late as possible 2. make PropertySourcesProcessor run as early as possible 3. fix PropertySourcesProcessor initialization logic to support multple Spring contexts
1 parent 6f30d52 commit 9b06053

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloAnnotationProcessor.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import org.springframework.beans.BeansException;
1111
import org.springframework.beans.factory.config.BeanPostProcessor;
12+
import org.springframework.core.Ordered;
13+
import org.springframework.core.PriorityOrdered;
1214
import org.springframework.core.annotation.AnnotationUtils;
1315
import org.springframework.util.ReflectionUtils;
1416

@@ -20,7 +22,7 @@
2022
*
2123
* @author Jason Song(song_s@ctrip.com)
2224
*/
23-
public class ApolloAnnotationProcessor implements BeanPostProcessor {
25+
public class ApolloAnnotationProcessor implements BeanPostProcessor, PriorityOrdered {
2426
@Override
2527
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
2628
Class clazz = bean.getClass();
@@ -80,4 +82,9 @@ public void onChange(ConfigChangeEvent changeEvent) {
8082
}
8183
}
8284

85+
@Override
86+
public int getOrder() {
87+
//make it as late as possible
88+
return Ordered.LOWEST_PRECEDENCE;
89+
}
8390
}

apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/PropertySourcesProcessor.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
1212
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
1313
import org.springframework.context.EnvironmentAware;
14+
import org.springframework.core.Ordered;
15+
import org.springframework.core.PriorityOrdered;
1416
import org.springframework.core.env.CompositePropertySource;
1517
import org.springframework.core.env.ConfigurableEnvironment;
1618
import org.springframework.core.env.Environment;
@@ -24,10 +26,9 @@
2426
*
2527
* @author Jason Song(song_s@ctrip.com)
2628
*/
27-
public class PropertySourcesProcessor implements BeanFactoryPostProcessor, EnvironmentAware {
29+
public class PropertySourcesProcessor implements BeanFactoryPostProcessor, EnvironmentAware, PriorityOrdered {
2830
private static final String APOLLO_PROPERTY_SOURCE_NAME = "ApolloPropertySources";
2931
private static final Multimap<Integer, String> NAMESPACE_NAMES = HashMultimap.create();
30-
private static final AtomicBoolean PROPERTY_SOURCES_INITIALIZED = new AtomicBoolean(false);
3132

3233
private ConfigurableEnvironment environment;
3334

@@ -37,15 +38,14 @@ public static boolean addNamespaces(Collection<String> namespaces, int order) {
3738

3839
@Override
3940
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
40-
if (!PROPERTY_SOURCES_INITIALIZED.compareAndSet(false, true)) {
41-
//already initialized
42-
return;
43-
}
44-
4541
initializePropertySources();
4642
}
4743

4844
protected void initializePropertySources() {
45+
if (environment.getPropertySources().contains(APOLLO_PROPERTY_SOURCE_NAME)) {
46+
//already initialized
47+
return;
48+
}
4949
CompositePropertySource composite = new CompositePropertySource(APOLLO_PROPERTY_SOURCE_NAME);
5050

5151
//sort by order asc
@@ -72,6 +72,11 @@ public void setEnvironment(Environment environment) {
7272
//only for test
7373
private static void reset() {
7474
NAMESPACE_NAMES.clear();
75-
PROPERTY_SOURCES_INITIALIZED.set(false);
75+
}
76+
77+
@Override
78+
public int getOrder() {
79+
//make it as early as possible
80+
return Ordered.HIGHEST_PRECEDENCE;
7681
}
7782
}

0 commit comments

Comments
 (0)