11
11
import org .springframework .beans .factory .config .BeanFactoryPostProcessor ;
12
12
import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
13
13
import org .springframework .context .EnvironmentAware ;
14
+ import org .springframework .core .Ordered ;
15
+ import org .springframework .core .PriorityOrdered ;
14
16
import org .springframework .core .env .CompositePropertySource ;
15
17
import org .springframework .core .env .ConfigurableEnvironment ;
16
18
import org .springframework .core .env .Environment ;
24
26
*
25
27
* @author Jason Song(song_s@ctrip.com)
26
28
*/
27
- public class PropertySourcesProcessor implements BeanFactoryPostProcessor , EnvironmentAware {
29
+ public class PropertySourcesProcessor implements BeanFactoryPostProcessor , EnvironmentAware , PriorityOrdered {
28
30
private static final String APOLLO_PROPERTY_SOURCE_NAME = "ApolloPropertySources" ;
29
31
private static final Multimap <Integer , String > NAMESPACE_NAMES = HashMultimap .create ();
30
- private static final AtomicBoolean PROPERTY_SOURCES_INITIALIZED = new AtomicBoolean (false );
31
32
32
33
private ConfigurableEnvironment environment ;
33
34
@@ -37,15 +38,14 @@ public static boolean addNamespaces(Collection<String> namespaces, int order) {
37
38
38
39
@ Override
39
40
public void postProcessBeanFactory (ConfigurableListableBeanFactory beanFactory ) throws BeansException {
40
- if (!PROPERTY_SOURCES_INITIALIZED .compareAndSet (false , true )) {
41
- //already initialized
42
- return ;
43
- }
44
-
45
41
initializePropertySources ();
46
42
}
47
43
48
44
protected void initializePropertySources () {
45
+ if (environment .getPropertySources ().contains (APOLLO_PROPERTY_SOURCE_NAME )) {
46
+ //already initialized
47
+ return ;
48
+ }
49
49
CompositePropertySource composite = new CompositePropertySource (APOLLO_PROPERTY_SOURCE_NAME );
50
50
51
51
//sort by order asc
@@ -72,6 +72,11 @@ public void setEnvironment(Environment environment) {
72
72
//only for test
73
73
private static void reset () {
74
74
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 ;
76
81
}
77
82
}
0 commit comments