public class ApplicationContextHolder extends Object implements org.springframework.context.ApplicationContextAware, org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent>
ApplicationContext to make the context available statically
to objects which, for whatever reason, cannot be wired up in Spring (for example, logging appenders which must be
defined in XML or properties files used to initialize the logging system).
To use this holder, exactly one bean should be declared as follows:
<bean class="ch.qos.logback.ext.spring.ApplicationContextHolder"/>
Note that no ID is necessary because this holder should always be used via its static accessors, rather than being
injected. Any Spring bean which wishes to access the ApplicationContext should not rely on this holder; it
should simply implement ApplicationContextAware.
WARNING: This object uses static memory to retain the ApplicationContext. This means this bean (and the
related configuration strategy) is only usable when no other Logback-enabled Spring applications exist in the same
JVM.| Constructor and Description |
|---|
ApplicationContextHolder() |
| Modifier and Type | Method and Description |
|---|---|
static org.springframework.context.ApplicationContext |
getApplicationContext()
Retrieves the
ApplicationContext set when Spring created and initialized the holder bean. |
static boolean |
hasApplicationContext()
Ensures that the
ApplicationContext has been set and that it has been refreshed. |
static boolean |
isRefreshed()
Returns a flag indicating whether the
ApplicationContext has been refreshed. |
void |
onApplicationEvent(org.springframework.context.event.ContextRefreshedEvent event) |
void |
setApplicationContext(org.springframework.context.ApplicationContext context) |
public void onApplicationEvent(org.springframework.context.event.ContextRefreshedEvent event)
onApplicationEvent in interface org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent>public static boolean hasApplicationContext()
ApplicationContext has been set and that it has been refreshed. The refresh
event is sent when the context has completely finished starting up, meaning all beans have been created and
initialized successfully.
This method has a loosely defined relationship with getApplicationContext(). When this method returns
true, calling getApplicationContext() is guaranteed to return a non-null context which
has been completely initialized. When this method returns false, getApplicationContext() may
return null, or it may return a non-null context which is not yet completely initialized.true if the context has been set and refreshed; otherwise, falsepublic static org.springframework.context.ApplicationContext getApplicationContext()
ApplicationContext set when Spring created and initialized the holder bean. If the
holder has not been created (see the class documentation for details on how to wire up the holder), or if
the holder has not been initialized, this accessor may return null.
As a general usage pattern, callers should wrap this method in a check for hasApplicationContext().
That ensures both that the context is set and also that it has fully initialized. Using a context which has
not been fully initialized can result in unexpected initialization behaviors for some beans. The most common
example of this behavior is receiving unproxied references to some beans, such as beans which were supposed
to have transactional semantics applied by AOP. By waiting for the context refresh event, the likelihood of
encountering such behavior is greatly reduced.null if the holder bean has not been initializedpublic void setApplicationContext(org.springframework.context.ApplicationContext context)
throws org.springframework.beans.BeansException
setApplicationContext in interface org.springframework.context.ApplicationContextAwareorg.springframework.beans.BeansExceptionpublic static boolean isRefreshed()
ApplicationContext has been refreshed. Theoretically, it is
possible for this method to return true when hasApplicationContext() returns false,
but in practice that is very unlikely since the bean for the holder should have been created and initialized
before the refresh event was raised.true if the context refresh event has been received; otherwise, falseCopyright © 2014. All Rights Reserved.