revised use of id and display name (id may be null; SPR-5761)

This commit is contained in:
Juergen Hoeller 2009-05-19 23:48:44 +00:00
parent 762f1c632b
commit da4f1d0eb7
3 changed files with 34 additions and 35 deletions

View File

@ -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 <code>null</code> 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 <code>null</code>)
*/
String getDisplayName();

View File

@ -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<BeanFactoryPostProcessor> beanFactoryPostProcessors =
new ArrayList<BeanFactoryPostProcessor>();
/** 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 <code>null</code> if none
*/
public String getId() {
return this.id;
}
/**
* Set a friendly name for this context.
* Typically done during initialization of concrete context implementations.
* <p>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 <code>null</code>)
*/
public String getDisplayName() {
return this.displayName;
}
/**
* Return the parent context, or <code>null</code> 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();
}

View File

@ -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);
}
}