2727import java .util .Queue ;
2828import java .util .Set ;
2929
30+ import org .apache .commons .logging .Log ;
31+ import org .apache .commons .logging .LogFactory ;
3032import org .springframework .beans .BeansException ;
3133import org .springframework .beans .factory .config .BeanFactoryPostProcessor ;
3234import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
@@ -106,6 +108,8 @@ public class ConfigFileApplicationListener implements
106108
107109 public static final int DEFAULT_ORDER = Ordered .HIGHEST_PRECEDENCE + 10 ;
108110
111+ private static Log logger = LogFactory .getLog (ConfigFileApplicationListener .class );
112+
109113 private String searchLocations ;
110114
111115 private String names ;
@@ -114,6 +118,8 @@ public class ConfigFileApplicationListener implements
114118
115119 private final ConversionService conversionService = new DefaultConversionService ();
116120
121+ private final List <Object > debug = new ArrayList <Object >();
122+
117123 @ Override
118124 public void onApplicationEvent (ApplicationEvent event ) {
119125 if (event instanceof ApplicationEnvironmentPreparedEvent ) {
@@ -140,9 +146,21 @@ private void onApplicationEnvironmentPreparedEvent(
140146 }
141147
142148 private void onApplicationPreparedEvent (ApplicationPreparedEvent event ) {
149+ logDebugMessages ();
143150 addPostProcessors (event .getApplicationContext ());
144151 }
145152
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+
146164 /**
147165 * Add config file property sources to the specified environment.
148166 * @param environment the environment to add source to
@@ -270,6 +288,8 @@ private class Loader {
270288
271289 private boolean activatedProfiles ;
272290
291+ private final List <Object > debug = ConfigFileApplicationListener .this .debug ;
292+
273293 public Loader (ConfigurableEnvironment environment , ResourceLoader resourceLoader ) {
274294 this .environment = environment ;
275295 this .resourceLoader = resourceLoader == null ? new DefaultResourceLoader ()
@@ -280,7 +300,6 @@ public void load() throws IOException {
280300 this .propertiesLoader = new PropertySourcesLoader ();
281301 this .profiles = Collections .asLifoQueue (new LinkedList <String >());
282302 this .activatedProfiles = false ;
283-
284303 if (this .environment .containsProperty (ACTIVE_PROFILES_PROPERTY )) {
285304 // Any pre-existing active profiles set via property sources (e.g. System
286305 // properties) take precedence over those added in config files.
@@ -354,29 +373,46 @@ private void load(String location, String name, String profile)
354373 private PropertySource <?> loadIntoGroup (String identifier , String location ,
355374 String profile ) throws IOException {
356375 Resource resource = this .resourceLoader .getResource (location );
376+ PropertySource <?> propertySource = null ;
357377 if (resource != null ) {
358378 String name = "applicationConfig: [" + location + "]" ;
359379 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 );
362382 if (propertySource != null ) {
363383 maybeActivateProfiles (propertySource
364384 .getProperty (ACTIVE_PROFILES_PROPERTY ));
365385 addIncludeProfiles (propertySource
366386 .getProperty (INCLUDE_PROFILES_PROPERTY ));
367387 }
368- return propertySource ;
369388 }
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 ;
371399 }
372400
373401 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" );
379406 }
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 ;
380416 }
381417 }
382418
0 commit comments