diff --git a/org.springframework.context/src/main/java/org/springframework/context/ApplicationContext.java b/org.springframework.context/src/main/java/org/springframework/context/ApplicationContext.java index 16e9f36b6cd..df117fd5508 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/ApplicationContext.java +++ b/org.springframework.context/src/main/java/org/springframework/context/ApplicationContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -58,13 +58,13 @@ public interface ApplicationContext extends ListableBeanFactory, HierarchicalBea /** * Return the unique id of this application context. - * @return the unique id of the context + * @return the unique id of the context, or null if none */ String getId(); /** * Return a friendly name for this context. - * @return a display name for this context + * @return a display name for this context (never null) */ String getDisplayName(); diff --git a/org.springframework.context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java b/org.springframework.context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java index a905a93d490..d1deba73686 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java +++ b/org.springframework.context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java @@ -144,6 +144,9 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader /** Unique id for this context, if any */ private String id = ObjectUtils.identityToString(this); + /** Display name */ + private String displayName = ObjectUtils.identityToString(this); + /** Parent context */ private ApplicationContext parent; @@ -151,9 +154,6 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader private final List beanFactoryPostProcessors = new ArrayList(); - /** Display name */ - private String displayName; - /** System time in milliseconds when this context started */ private long startupDate; @@ -213,10 +213,32 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader this.id = id; } + /** + * Return the unique id of this application context. + * @return the unique id of the context, or null if none + */ public String getId() { return this.id; } + /** + * Set a friendly name for this context. + * Typically done during initialization of concrete context implementations. + *

Default is the object id of the context instance. + */ + public void setDisplayName(String displayName) { + Assert.hasLength(displayName, "Display name must not be empty"); + this.displayName = displayName; + } + + /** + * Return a friendly name for this context. + * @return a display name for this context (never null) + */ + public String getDisplayName() { + return this.displayName; + } + /** * Return the parent context, or null if there is no parent * (that is, this context is the root of the context hierarchy). @@ -234,21 +256,6 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader return getBeanFactory(); } - /** - * Set a friendly name for this context. - * Typically done during initialization of concrete context implementations. - */ - public void setDisplayName(String displayName) { - this.displayName = displayName; - } - - /** - * Return a friendly name for this context. - */ - public String getDisplayName() { - return (this.displayName != null ? this.displayName : getId()); - } - /** * Return the timestamp (ms) when this context was first loaded. */ @@ -267,7 +274,7 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader public void publishEvent(ApplicationEvent event) { Assert.notNull(event, "Event must not be null"); if (logger.isTraceEnabled()) { - logger.trace("Publishing event in context [" + getId() + "]: " + event); + logger.trace("Publishing event in " + getDisplayName() + ": " + event); } getApplicationEventMulticaster().multicastEvent(event); if (this.parent != null) { @@ -417,15 +424,9 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader protected ConfigurableListableBeanFactory obtainFreshBeanFactory() { refreshBeanFactory(); ConfigurableListableBeanFactory beanFactory = getBeanFactory(); - - if (logger.isInfoEnabled()) { - logger.info("Bean factory for application context [" + getId() + "]: " + - ObjectUtils.identityToString(beanFactory)); - } if (logger.isDebugEnabled()) { - logger.debug(beanFactory.getBeanDefinitionCount() + " beans defined in " + this); + logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory); } - return beanFactory; } @@ -1161,16 +1162,15 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader */ @Override public String toString() { - StringBuilder sb = new StringBuilder(getId()); - sb.append(": display name [").append(getDisplayName()); - sb.append("]; startup date [").append(new Date(getStartupDate())); + StringBuilder sb = new StringBuilder(getDisplayName()); + sb.append(": startup date [").append(new Date(getStartupDate())); sb.append("]; "); ApplicationContext parent = getParent(); if (parent == null) { sb.append("root of context hierarchy"); } else { - sb.append("parent: ").append(parent.getId()); + sb.append("parent: ").append(parent.getDisplayName()); } return sb.toString(); } diff --git a/org.springframework.context/src/main/java/org/springframework/context/support/AbstractRefreshableApplicationContext.java b/org.springframework.context/src/main/java/org/springframework/context/support/AbstractRefreshableApplicationContext.java index 161ecdce94e..839fd59b7ef 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/support/AbstractRefreshableApplicationContext.java +++ b/org.springframework.context/src/main/java/org/springframework/context/support/AbstractRefreshableApplicationContext.java @@ -130,8 +130,7 @@ public abstract class AbstractRefreshableApplicationContext extends AbstractAppl } } catch (IOException ex) { - throw new ApplicationContextException( - "I/O error parsing XML document for application context [" + getDisplayName() + "]", ex); + throw new ApplicationContextException("I/O error parsing XML document for " + getDisplayName(), ex); } }