Top Description Fields Constructors Methods
org.springframework.context.support

public abstract Class ApplicationObjectSupport

extends Object
implements ApplicationContextAware
Class Inheritance
All Implemented Interfaces
org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.Aware
Imports
org.apache.commons.logging.Log, .LogFactory, org.springframework.beans.BeansException, org.springframework.context.ApplicationContext, .ApplicationContextAware, .ApplicationContextException, org.springframework.lang.Nullable, org.springframework.util.Assert

Convenient superclass for application objects that want to be aware of the application context, e.g. for custom lookup of collaborating beans or for context-specific resource access. It saves the application context reference and provides an initialization callback method. Furthermore, it offers numerous convenience methods for message lookup.

There is no requirement to subclass this class: It just makes things a little easier if you need access to the context, e.g. for access to file resources or to the message source. Note that many application objects do not need to be aware of the application context at all, as they can receive collaborating beans via bean references.

Many framework classes are derived from this class, particularly within the web support.

Authors
Rod Johnson, Juergen Hoeller
See Also
org.springframework.web.context.support.WebApplicationObjectSupport

Field Summary

Modifier and TypeField and Description
private ApplicationContext
applicationContext

ApplicationContext this object runs in.

protected final Log
logger

Logger that is available to subclasses.

private MessageSourceAccessor
messageSourceAccessor

MessageSourceAccessor for easy message access.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public final ApplicationContext
getApplicationContext()

Return the ApplicationContext that this object is associated with.

protected final MessageSourceAccessor
getMessageSourceAccessor()

Return a MessageSourceAccessor for the application context used by this object, for easy message access.

protected void
initApplicationContext(ApplicationContext
the containing ApplicationContext
context
)

Subclasses can override this for custom initialization behavior.

protected void
initApplicationContext()

Subclasses can override this for custom initialization behavior.

protected boolean
isContextRequired()

Determine whether this application object needs to run in an ApplicationContext.

protected final ApplicationContext

Returns:

the ApplicationContext (never null)
obtainApplicationContext
()

Obtain the ApplicationContext for actual use.

protected Class<?>
requiredContextClass()

Determine the context class that any context passed to setApplicationContext must be an instance of.

public final void
setApplicationContext(ApplicationContext
the ApplicationContext object to be used by this object
context
)

Implements org.springframework.context.ApplicationContextAware.setApplicationContext.

Set the ApplicationContext that this object runs in. Normally this call will be used to initialize the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

applicationContextback to summary
private ApplicationContext applicationContext

ApplicationContext this object runs in.

Annotations
@Nullable
loggerback to summary
protected final Log logger

Logger that is available to subclasses.

messageSourceAccessorback to summary
private MessageSourceAccessor messageSourceAccessor

MessageSourceAccessor for easy message access.

Annotations
@Nullable

Constructor Detail

ApplicationObjectSupportback to summary
public ApplicationObjectSupport()

Method Detail

getApplicationContextback to summary
public final ApplicationContext getApplicationContext() throws IllegalStateException

Return the ApplicationContext that this object is associated with.

Annotations
@Nullable
Exceptions
IllegalStateException:
if not running in an ApplicationContext
getMessageSourceAccessorback to summary
protected final MessageSourceAccessor getMessageSourceAccessor() throws IllegalStateException

Return a MessageSourceAccessor for the application context used by this object, for easy message access.

Annotations
@Nullable
Exceptions
IllegalStateException:
if not running in an ApplicationContext
initApplicationContextback to summary
protected void initApplicationContext(ApplicationContext context) throws BeansException

Subclasses can override this for custom initialization behavior. Gets called by setApplicationContext after setting the context instance.

Note

Does not get called on re-initialization of the context but rather just on first initialization of this object's context reference.

The default implementation calls the overloaded initApplicationContext() method without ApplicationContext reference.

Parameters
context:ApplicationContext

the containing ApplicationContext

Exceptions
BeansException:
if thrown by ApplicationContext methods
ApplicationContextException:
in case of initialization errors
See Also
setApplicationContext
initApplicationContextback to summary
protected void initApplicationContext() throws BeansException

Subclasses can override this for custom initialization behavior.

The default implementation is empty. Called by initApplicationContext(ApplicationContext).

Exceptions
BeansException:
if thrown by ApplicationContext methods
ApplicationContextException:
in case of initialization errors
See Also
setApplicationContext
isContextRequiredback to summary
protected boolean isContextRequired()

Determine whether this application object needs to run in an ApplicationContext.

Default is "false". Can be overridden to enforce running in a context (i.e. to throw IllegalStateException on accessors if outside a context).

See Also
getApplicationContext, getMessageSourceAccessor
obtainApplicationContextback to summary
protected final ApplicationContext obtainApplicationContext()

Obtain the ApplicationContext for actual use.

Returns:ApplicationContext

the ApplicationContext (never null)

Exceptions
IllegalStateException:
in case of no ApplicationContext set
Since
5.0
requiredContextClassback to summary
protected Class<?> requiredContextClass()

Determine the context class that any context passed to setApplicationContext must be an instance of. Can be overridden in subclasses.

See Also
setApplicationContext
setApplicationContextback to summary
public final void setApplicationContext(ApplicationContext context) throws BeansException

Implements org.springframework.context.ApplicationContextAware.setApplicationContext.

Doc from org.springframework.context.ApplicationContextAware.setApplicationContext.

Set the ApplicationContext that this object runs in. Normally this call will be used to initialize the object.

Invoked after population of normal bean properties but before an init callback such as org.springframework.beans.factory.InitializingBean#afterPropertiesSet() or a custom init-method. Invoked after ResourceLoaderAware#setResourceLoader, ApplicationEventPublisherAware#setApplicationEventPublisher and MessageSourceAware, if applicable.

Parameters
context:ApplicationContext

the ApplicationContext object to be used by this object

Annotations
@Override
Exceptions
BeansException:
if thrown by application context methods