27
27
import java .util .Queue ;
28
28
import java .util .Set ;
29
29
30
+ import org .apache .commons .logging .Log ;
31
+ import org .apache .commons .logging .LogFactory ;
30
32
import org .springframework .beans .BeansException ;
31
33
import org .springframework .beans .factory .config .BeanFactoryPostProcessor ;
32
34
import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
@@ -106,6 +108,8 @@ public class ConfigFileApplicationListener implements
106
108
107
109
public static final int DEFAULT_ORDER = Ordered .HIGHEST_PRECEDENCE + 10 ;
108
110
111
+ private static Log logger = LogFactory .getLog (ConfigFileApplicationListener .class );
112
+
109
113
private String searchLocations ;
110
114
111
115
private String names ;
@@ -114,6 +118,8 @@ public class ConfigFileApplicationListener implements
114
118
115
119
private final ConversionService conversionService = new DefaultConversionService ();
116
120
121
+ private final List <Object > debug = new ArrayList <Object >();
122
+
117
123
@ Override
118
124
public void onApplicationEvent (ApplicationEvent event ) {
119
125
if (event instanceof ApplicationEnvironmentPreparedEvent ) {
@@ -140,9 +146,21 @@ private void onApplicationEnvironmentPreparedEvent(
140
146
}
141
147
142
148
private void onApplicationPreparedEvent (ApplicationPreparedEvent event ) {
149
+ logDebugMessages ();
143
150
addPostProcessors (event .getApplicationContext ());
144
151
}
145
152
153
+ private void logDebugMessages () {
154
+ // Debug logging is deferred because the Logging initialization might not have
155
+ // run at the time that config file decisions are taken
156
+ if (logger .isDebugEnabled ()) {
157
+ for (Object message : this .debug ) {
158
+ logger .debug (message );
159
+ }
160
+ }
161
+ this .debug .clear ();
162
+ }
163
+
146
164
/**
147
165
* Add config file property sources to the specified environment.
148
166
* @param environment the environment to add source to
@@ -270,6 +288,8 @@ private class Loader {
270
288
271
289
private boolean activatedProfiles ;
272
290
291
+ private final List <Object > debug = ConfigFileApplicationListener .this .debug ;
292
+
273
293
public Loader (ConfigurableEnvironment environment , ResourceLoader resourceLoader ) {
274
294
this .environment = environment ;
275
295
this .resourceLoader = resourceLoader == null ? new DefaultResourceLoader ()
@@ -280,7 +300,6 @@ public void load() throws IOException {
280
300
this .propertiesLoader = new PropertySourcesLoader ();
281
301
this .profiles = Collections .asLifoQueue (new LinkedList <String >());
282
302
this .activatedProfiles = false ;
283
-
284
303
if (this .environment .containsProperty (ACTIVE_PROFILES_PROPERTY )) {
285
304
// Any pre-existing active profiles set via property sources (e.g. System
286
305
// properties) take precedence over those added in config files.
@@ -354,29 +373,46 @@ private void load(String location, String name, String profile)
354
373
private PropertySource <?> loadIntoGroup (String identifier , String location ,
355
374
String profile ) throws IOException {
356
375
Resource resource = this .resourceLoader .getResource (location );
376
+ PropertySource <?> propertySource = null ;
357
377
if (resource != null ) {
358
378
String name = "applicationConfig: [" + location + "]" ;
359
379
String group = "applicationConfig: [" + identifier + "]" ;
360
- PropertySource <?> propertySource = this .propertiesLoader .load (resource ,
361
- group , name , profile );
380
+ propertySource = this .propertiesLoader .load (resource , group , name ,
381
+ profile );
362
382
if (propertySource != null ) {
363
383
maybeActivateProfiles (propertySource
364
384
.getProperty (ACTIVE_PROFILES_PROPERTY ));
365
385
addIncludeProfiles (propertySource
366
386
.getProperty (INCLUDE_PROFILES_PROPERTY ));
367
387
}
368
- return propertySource ;
369
388
}
370
- return null ;
389
+
390
+ StringBuilder msg = new StringBuilder ();
391
+ msg .append (propertySource == null ? "Skipped " : "Loaded " );
392
+ msg .append ("config file " );
393
+ msg .append ("'" + location + "' " );
394
+ msg .append (StringUtils .hasLength (profile ) ? "for profile " : "" );
395
+ msg .append (resource == null || !resource .exists () ? "resource not found" : "" );
396
+ this .debug .add (msg );
397
+
398
+ return propertySource ;
371
399
}
372
400
373
401
private void maybeActivateProfiles (Object value ) {
374
- if (!this .activatedProfiles == true ) {
375
- Set <String > profiles = getProfilesForValue (value );
376
- activateProfiles (profiles );
377
- if (profiles .size () > 0 ) {
378
- this .activatedProfiles = true ;
402
+ if (this .activatedProfiles ) {
403
+ if (value != null ) {
404
+ this .debug .add ("Profiles already activated, '" + value
405
+ + "' will not be applied" );
379
406
}
407
+ return ;
408
+ }
409
+
410
+ Set <String > profiles = getProfilesForValue (value );
411
+ activateProfiles (profiles );
412
+ if (profiles .size () > 0 ) {
413
+ this .debug .add ("Activated profiles "
414
+ + StringUtils .collectionToCommaDelimitedString (profiles ));
415
+ this .activatedProfiles = true ;
380
416
}
381
417
}
382
418
0 commit comments