public abstract class ApplicationObjectSupport extends java.lang.Object implements ApplicationContextAware
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.
WebApplicationObjectSupport
Modifier and Type | Field and Description |
---|---|
private ApplicationContext |
applicationContext
ApplicationContext this object runs in
|
protected Log |
logger
Logger that is available to subclasses
|
private MessageSourceAccessor |
messageSourceAccessor
MessageSourceAccessor for easy message access
|
Constructor and Description |
---|
ApplicationObjectSupport() |
Modifier and Type | Method and Description |
---|---|
ApplicationContext |
getApplicationContext()
Return the ApplicationContext that this object is associated with.
|
protected MessageSourceAccessor |
getMessageSourceAccessor()
Return a MessageSourceAccessor for the application context
used by this object, for easy message access.
|
protected void |
initApplicationContext()
Subclasses can override this for custom initialization behavior.
|
protected void |
initApplicationContext(ApplicationContext context)
Subclasses can override this for custom initialization behavior.
|
protected boolean |
isContextRequired()
Determine whether this application object needs to run in an ApplicationContext.
|
protected java.lang.Class<?> |
requiredContextClass()
Determine the context class that any context passed to
setApplicationContext must be an instance of. |
void |
setApplicationContext(ApplicationContext context)
Set the ApplicationContext that this object runs in.
|
protected final Log logger
private ApplicationContext applicationContext
private MessageSourceAccessor messageSourceAccessor
public final void setApplicationContext(ApplicationContext context) throws BeansException
ApplicationContextAware
Invoked after population of normal bean properties but before an init callback such
as InitializingBean.afterPropertiesSet()
or a custom init-method. Invoked after ResourceLoaderAware.setResourceLoader(org.springframework.core.io.ResourceLoader)
,
ApplicationEventPublisherAware.setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher)
and
MessageSourceAware
, if applicable.
setApplicationContext
in interface ApplicationContextAware
context
- the ApplicationContext object to be used by this objectApplicationContextException
- in case of context initialization errorsBeansException
- if thrown by application context methodsBeanInitializationException
protected boolean isContextRequired()
Default is "false". Can be overridden to enforce running in a context (i.e. to throw IllegalStateException on accessors if outside a context).
protected java.lang.Class<?> requiredContextClass()
setApplicationContext
must be an instance of.
Can be overridden in subclasses.protected void initApplicationContext(ApplicationContext context) throws BeansException
setApplicationContext
after setting the context instance.
Note: Does not get called on reinitialization 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.
context
- the containing ApplicationContextApplicationContextException
- in case of initialization errorsBeansException
- if thrown by ApplicationContext methodssetApplicationContext(org.springframework.context.ApplicationContext)
protected void initApplicationContext() throws BeansException
The default implementation is empty. Called by
initApplicationContext(org.springframework.context.ApplicationContext)
.
ApplicationContextException
- in case of initialization errorsBeansException
- if thrown by ApplicationContext methodssetApplicationContext(org.springframework.context.ApplicationContext)
public final ApplicationContext getApplicationContext() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if not running in an ApplicationContextprotected final MessageSourceAccessor getMessageSourceAccessor() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if not running in an ApplicationContext