Class SpringContextBootstrappingInitializer
java.lang.Object
org.springframework.data.gemfire.support.SpringContextBootstrappingInitializer
- All Implemented Interfaces:
EventListener
,org.apache.geode.cache.Declarable
,org.springframework.context.ApplicationListener<org.springframework.context.event.ApplicationContextEvent>
public class SpringContextBootstrappingInitializer
extends Object
implements org.springframework.context.ApplicationListener<org.springframework.context.event.ApplicationContextEvent>, org.apache.geode.cache.Declarable
The
SpringContextBootstrappingInitializer
class is a GemFire configuration initializer used to bootstrap
a Spring ApplicationContext
inside a GemFire Server JVM-based process. This enables a GemFire Server
resource to be mostly configured with Spring Data GemFire's configuration meta-data. The GemFire Cache
itself is the only resource that cannot be configured and initialized in a Spring context since the initializer
is not invoked until after GemFire creates and initializes the GemFire Cache
for use.- See Also:
-
Properties
Cache
Declarable
ApplicationContext
ApplicationListener
ConfigurableApplicationContext
AnnotationConfigApplicationContext
ApplicationContextEvent
ApplicationEventMulticaster
ClassPathXmlApplicationContext
DefaultResourceLoader
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected org.springframework.context.ConfigurableApplicationContext
createApplicationContext
(String[] basePackages, String[] configLocations) Creates (constructs and configures) an instance of the ConfigurableApplicationContext based on either the specified base packages containing @Configuration, @Component or JSR 330 annotated classes to scan, or the specified locations of context configuration meta-data files.static void
destroy()
Destroy the state of theSpringContextBootstrappingInitializer
.static org.springframework.context.ConfigurableApplicationContext
Gets a reference to the Spring ApplicationContext constructed, configured and initialized inside the GemFire Server-based JVM process.void
init
(Properties parameters) void
init
(org.apache.geode.cache.Cache cache, Properties parameters) Initializes a SpringApplicationContext
with the given parameters specified with an Apache Geode or Pivotal GemFire <initializer> block in cache.xml.protected org.springframework.context.ConfigurableApplicationContext
initApplicationContext
(org.springframework.context.ConfigurableApplicationContext applicationContext) Initializes the given ApplicationContext by registering this SpringContextBootstrappingInitializer as an ApplicationListener and registering a runtime shutdown hook.protected org.slf4j.Logger
Initialization method for the logger used to log important messages from this initializer.protected static void
notifyOnExistingContextRefreshedEvent
(org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent> listener) Notifies any Spring ApplicationListeners of a current and existing ContextRefreshedEvent if the ApplicationContext had been previously created, initialized and refreshed before any ApplicationListeners interested in ContextRefreshedEvents were registered so that application components (such as the GemFire CacheLoaders extending LazyWiringDeclarableSupport objects) registered late, requiring configuration (auto-wiring), also get notified and wired accordingly.void
onApplicationEvent
(org.springframework.context.event.ApplicationContextEvent event) Gets notified when the Spring ApplicationContext gets created and refreshed by GemFire, once the <initializer> block is processed and the SpringContextBootstrappingInitializer Declarable component is initialized.protected org.springframework.context.ConfigurableApplicationContext
refreshApplicationContext
(org.springframework.context.ConfigurableApplicationContext applicationContext) Refreshes the given ApplicationContext making the context active.static boolean
Registers the specified Spring annotated POJO class, which will be used to configure and initialize the Spring ApplicationContext.static <T extends org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent>>
Tregister
(T listener) Registers a Spring ApplicationListener to be notified when the Spring ApplicationContext is created by GemFire when instantiating and initializing Declarables declared inside the <initializer> block inside GemFire's cache.xml file.static void
setBeanClassLoader
(ClassLoader beanClassLoader) Sets the ClassLoader used by the Spring ApplicationContext, created by this GemFire Initializer, when creating bean definition classes.static boolean
unregister
(Class<?> annotatedClass) Un-registers the specified Spring annotated POJO class used to configure and initialize the Spring ApplicationContext.static <T extends org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent>>
Tunregister
(T listener) Un-registers the Spring ApplicationListener from this SpringContextBootstrappingInitializer in order to stop receiving ApplicationEvents on Spring context refreshes.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.apache.geode.cache.Declarable
initialize
-
Field Details
-
BASE_PACKAGES_PARAMETER
- See Also:
-
CONTEXT_CONFIG_LOCATIONS_PARAMETER
- See Also:
-
CHARS_TO_DELETE
- See Also:
-
COMMA_DELIMITER
- See Also:
-
logger
protected final org.slf4j.Logger logger
-
-
Constructor Details
-
SpringContextBootstrappingInitializer
public SpringContextBootstrappingInitializer()
-
-
Method Details
-
getApplicationContext
public static org.springframework.context.ConfigurableApplicationContext getApplicationContext()Gets a reference to the Spring ApplicationContext constructed, configured and initialized inside the GemFire Server-based JVM process.- Returns:
- a reference to the Spring ApplicationContext bootstrapped by GemFire.
- See Also:
-
ConfigurableApplicationContext
-
setBeanClassLoader
Sets the ClassLoader used by the Spring ApplicationContext, created by this GemFire Initializer, when creating bean definition classes.- Parameters:
beanClassLoader
- the ClassLoader used by the Spring ApplicationContext to load bean definition classes.- Throws:
IllegalStateException
- if the Spring ApplicationContext has already been created and initialized.- See Also:
-
destroy
public static void destroy()Destroy the state of theSpringContextBootstrappingInitializer
. -
notifyOnExistingContextRefreshedEvent
protected static void notifyOnExistingContextRefreshedEvent(org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent> listener) Notifies any Spring ApplicationListeners of a current and existing ContextRefreshedEvent if the ApplicationContext had been previously created, initialized and refreshed before any ApplicationListeners interested in ContextRefreshedEvents were registered so that application components (such as the GemFire CacheLoaders extending LazyWiringDeclarableSupport objects) registered late, requiring configuration (auto-wiring), also get notified and wired accordingly.- Parameters:
listener
- a Spring ApplicationListener requiring notification of any ContextRefreshedEvents after the ApplicationContext has already been created, initialized and/or refreshed.- See Also:
-
ApplicationListener.onApplicationEvent(org.springframework.context.ApplicationEvent)
ContextRefreshedEvent
-
register
public static <T extends org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent>> T register(T listener) Registers a Spring ApplicationListener to be notified when the Spring ApplicationContext is created by GemFire when instantiating and initializing Declarables declared inside the <initializer> block inside GemFire's cache.xml file.- Type Parameters:
T
- the Class type of the Spring ApplicationListener.- Parameters:
listener
- the ApplicationListener to register for ContextRefreshedEvents multi-casted by this SpringContextBootstrappingInitializer.- Returns:
- the reference to the ApplicationListener for method call chaining purposes.
- See Also:
-
notifyOnExistingContextRefreshedEvent(ApplicationListener)
unregister(ApplicationListener)
ApplicationListener
ContextRefreshedEvent
#addApplicationListener(org.springframework.context.ApplicationListener)
-
register
Registers the specified Spring annotated POJO class, which will be used to configure and initialize the Spring ApplicationContext.- Parameters:
annotatedClass
- the Spring annotated (@Configuration) POJO class to register.- Returns:
- a boolean value indicating whether the Spring annotated POJO class was successfully registered.
- See Also:
-
unregister
public static <T extends org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent>> T unregister(T listener) Un-registers the Spring ApplicationListener from this SpringContextBootstrappingInitializer in order to stop receiving ApplicationEvents on Spring context refreshes.- Type Parameters:
T
- the Class type of the Spring ApplicationListener.- Parameters:
listener
- the ApplicationListener to unregister from receiving ContextRefreshedEvents by this SpringContextBootstrappingInitializer.- Returns:
- the reference to the ApplicationListener for method call chaining purposes.
- See Also:
-
register(ApplicationListener)
ApplicationListener
ContextRefreshedEvent
#removeApplicationListener(org.springframework.context.ApplicationListener)
-
unregister
Un-registers the specified Spring annotated POJO class used to configure and initialize the Spring ApplicationContext.- Parameters:
annotatedClass
- the Spring annotated (@Configuration) POJO class to unregister.- Returns:
- a boolean value indicating whether the Spring annotated POJO class was successfully un-registered.
- See Also:
-
initLogger
protected org.slf4j.Logger initLogger()Initialization method for the logger used to log important messages from this initializer.- Returns:
- a Apache Commons Log used to log messages from this initializer
- See Also:
-
LogFactory.getLog(Class)
Log
-
createApplicationContext
protected org.springframework.context.ConfigurableApplicationContext createApplicationContext(String[] basePackages, String[] configLocations) Creates (constructs and configures) an instance of the ConfigurableApplicationContext based on either the specified base packages containing @Configuration, @Component or JSR 330 annotated classes to scan, or the specified locations of context configuration meta-data files. The created ConfigurableApplicationContext is not automatically "refreshed" and therefore must be "refreshed" by the caller manually. When basePackages are specified, an instance of AnnotationConfigApplicationContext is constructed and a scan is performed; otherwise an instance of the ClassPathXmlApplicationContext is initialized with the configLocations. This method prefers the ClassPathXmlApplicationContext to the AnnotationConfigApplicationContext when both basePackages and configLocations are specified.- Parameters:
basePackages
- the base packages to scan for application @Components and @Configuration classes.configLocations
- a String array indicating the locations of the context configuration meta-data files used to configure the ClassPathXmlApplicationContext instance.- Returns:
- an instance of ConfigurableApplicationContext configured and initialized with either configLocations or the basePackages when configLocations is unspecified. Note, the "refresh" method must be called manually before using the context.
- Throws:
IllegalArgumentException
- if both the basePackages and configLocation parameter arguments are null or empty.- See Also:
-
newApplicationContext(String[])
AnnotationConfigApplicationContext
AnnotationConfigApplicationContext.scan(String...)
ClassPathXmlApplicationContext
-
initApplicationContext
protected org.springframework.context.ConfigurableApplicationContext initApplicationContext(org.springframework.context.ConfigurableApplicationContext applicationContext) Initializes the given ApplicationContext by registering this SpringContextBootstrappingInitializer as an ApplicationListener and registering a runtime shutdown hook.- Parameters:
applicationContext
- the ConfigurableApplicationContext to initialize.- Returns:
- the initialized ApplicationContext.
- Throws:
IllegalArgumentException
- if the ApplicationContext reference is null!- See Also:
-
ConfigurableApplicationContext
ConfigurableApplicationContext.addApplicationListener(ApplicationListener)
ConfigurableApplicationContext.registerShutdownHook()
-
refreshApplicationContext
protected org.springframework.context.ConfigurableApplicationContext refreshApplicationContext(org.springframework.context.ConfigurableApplicationContext applicationContext) Refreshes the given ApplicationContext making the context active.- Parameters:
applicationContext
- the ConfigurableApplicationContext to refresh.- Returns:
- the refreshed ApplicationContext.
- Throws:
IllegalArgumentException
- if the ApplicationContext reference is null!- See Also:
-
ConfigurableApplicationContext
ConfigurableApplicationContext.refresh()
-
init
- Specified by:
init
in interfaceorg.apache.geode.cache.Declarable
-
init
Initializes a SpringApplicationContext
with the given parameters specified with an Apache Geode or Pivotal GemFire <initializer> block in cache.xml.- Parameters:
parameters
-Properties
object containing the configuration parameters and settings defined in the Apache Geode/Pivotal GemFire cache.xml <initializer> block for the declaredSpringContextBootstrappingInitializer
Apache Geode/Pivotal GemFireDeclarable
object.cache
- reference to the peerCache
.- Throws:
org.springframework.context.ApplicationContextException
- if the SpringApplicationContext
could not be successfully constructed, configured and initialized.- See Also:
-
onApplicationEvent
public void onApplicationEvent(org.springframework.context.event.ApplicationContextEvent event) Gets notified when the Spring ApplicationContext gets created and refreshed by GemFire, once the <initializer> block is processed and the SpringContextBootstrappingInitializer Declarable component is initialized. This handler method proceeds in notifying any other GemFire components that need to be aware that the Spring ApplicationContext now exists and is ready for use, such as other Declarable GemFire objects requiring auto-wiring support, etc. In addition, this method handles the ContextClosedEvent by removing the ApplicationContext reference.- Specified by:
onApplicationEvent
in interfaceorg.springframework.context.ApplicationListener<org.springframework.context.event.ApplicationContextEvent>
- Parameters:
event
- the ApplicationContextEvent signaling that the Spring ApplicationContext has been created and refreshed by GemFire, or closed when the JVM process exits.- See Also:
-
ContextClosedEvent
ContextRefreshedEvent
#multicastEvent(org.springframework.context.ApplicationEvent)
-