Class SpringContextBootstrappingInitializer
java.lang.Object
org.springframework.data.gemfire.support.SpringContextBootstrappingInitializer
- All Implemented Interfaces:
EventListener,Declarable,ApplicationListener<ApplicationContextEvent>
public class SpringContextBootstrappingInitializer
extends Object
implements ApplicationListener<ApplicationContextEvent>, 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.-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected ConfigurableApplicationContextcreateApplicationContext(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 voiddestroy()Destroy the state of theSpringContextBootstrappingInitializer.Gets a reference to the Spring ApplicationContext constructed, configured and initialized inside the GemFire Server-based JVM process.voidinit(Properties parameters) voidinit(Cache cache, Properties parameters) Initializes a SpringApplicationContextwith the given parameters specified with an Apache Geode or Pivotal GemFire <initializer> block in cache.xml.protected ConfigurableApplicationContextinitApplicationContext(ConfigurableApplicationContext applicationContext) Initializes the given ApplicationContext by registering this SpringContextBootstrappingInitializer as an ApplicationListener and registering a runtime shutdown hook.protected org.slf4j.LoggerInitialization method for the logger used to log important messages from this initializer.protected static voidNotifies 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.voidGets 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 ConfigurableApplicationContextrefreshApplicationContext(ConfigurableApplicationContext applicationContext) Refreshes the given ApplicationContext making the context active.static booleanRegisters the specified Spring annotated POJO class, which will be used to configure and initialize the Spring ApplicationContext.static <T extends ApplicationListener<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 voidsetBeanClassLoader(ClassLoader beanClassLoader) Sets the ClassLoader used by the Spring ApplicationContext, created by this GemFire Initializer, when creating bean definition classes.static booleanunregister(Class<?> annotatedClass) Un-registers the specified Spring annotated POJO class used to configure and initialize the Spring ApplicationContext.static <T extends ApplicationListener<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, waitMethods 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
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:
-
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(ApplicationListener<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:
-
register
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:
-
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
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:
-
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:
-
createApplicationContext
protected 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[])AnnotationConfigApplicationContextAnnotationConfigApplicationContext.scan(String...)ClassPathXmlApplicationContext
-
initApplicationContext
protected ConfigurableApplicationContext initApplicationContext(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:
-
refreshApplicationContext
protected ConfigurableApplicationContext refreshApplicationContext(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:
-
init
- Specified by:
initin interfaceDeclarable
-
init
Initializes a SpringApplicationContextwith the given parameters specified with an Apache Geode or Pivotal GemFire <initializer> block in cache.xml.- Parameters:
parameters-Propertiesobject containing the configuration parameters and settings defined in the Apache Geode/Pivotal GemFire cache.xml <initializer> block for the declaredSpringContextBootstrappingInitializerApache Geode/Pivotal GemFireDeclarableobject.cache- reference to the peerCache.- Throws:
ApplicationContextException- if the SpringApplicationContextcould not be successfully constructed, configured and initialized.- See Also:
-
onApplicationEvent
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:
onApplicationEventin interfaceApplicationListener<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:
-