ch.qos.logback.ext.spring
Class ApplicationContextHolder

java.lang.Object
  extended by ch.qos.logback.ext.spring.ApplicationContextHolder
All Implemented Interfaces:
EventListener, org.springframework.beans.factory.Aware, org.springframework.context.ApplicationContextAware, org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent>

public class ApplicationContextHolder
extends Object
implements org.springframework.context.ApplicationContextAware, org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent>

A special bean which may be defined in the Spring 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.

Since:
0.1
Author:
Bryan Turner, Les Hazlewood

Constructor Summary
ApplicationContextHolder()
           
 
Method Summary
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)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ApplicationContextHolder

public ApplicationContextHolder()
Method Detail

onApplicationEvent

public void onApplicationEvent(org.springframework.context.event.ContextRefreshedEvent event)
Specified by:
onApplicationEvent in interface org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent>

hasApplicationContext

public static boolean hasApplicationContext()
Ensures that the 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.

Returns:
true if the context has been set and refreshed; otherwise, false

getApplicationContext

public static org.springframework.context.ApplicationContext getApplicationContext()
Retrieves the 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.

Returns:
the set context, or null if the holder bean has not been initialized

setApplicationContext

public void setApplicationContext(org.springframework.context.ApplicationContext context)
                           throws org.springframework.beans.BeansException
Specified by:
setApplicationContext in interface org.springframework.context.ApplicationContextAware
Throws:
org.springframework.beans.BeansException

isRefreshed

public static boolean isRefreshed()
Returns a flag indicating whether the 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.

Returns:
true if the context refresh event has been received; otherwise, false


Copyright © 2012-2013. All Rights Reserved.