Checklist:
This article attempts to provide a working example and discuss various issues you may encounter when using a Spring XML configuration file for bootstrapping and configuring a GemFire CacheServer using the "--spring-xml-location" command-line option.
Working Scenario
1. CacheServer start script example:
......
### Gemfire lib jars ###
export CLASSPATH=$GEMFIRE/lib/geode-dependencies.jar:$GEMFIRE/lib/gfsh-dependencies.jar:/usr/java/jdk1.8.0_211/lib/tools.jar
### Spring & Spring data gemfire jars ###
# For example(Gemfire 9.9.1 + Spring data gemfire 2.2.6) :
# spring-data-gemfire-2.2.6.RELEASE.jar
# spring-core-5.2.5.RELEASE.jar
# spring-beans-5.2.5.RELEASE.jar
# spring-tx-5.2.5.RELEASE.jar
export CLASSPATH=/home/gpadmin/apps/springjars/*:$CLASSPATH
### directory of spring-gemfire-config.xml ###
# In my case, I put spring-gemfire-config.xml in path:
# /home/gpadmin/apps/springxmlDirectory/springxml/spring-gemfire-config.xml
export CLASSPATH=/home/gpadmin/apps/springxmlDirectory/:$CLASSPATH
gfsh start server --server-port=7901 --name=server1 --mcast-port=0 --locators=pivhdsne.localdomain[7900] --include-system-classpath=true --spring-xml-location="classpath:springxml/spring-gemfire-config.xml"
2. Example
spring-gemfire-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:gfe="http://www.springframework.org/schema/gemfire"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/gemfire
http://www.springframework.org/schema/gemfire/spring-gemfire.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<gfe:cache id="gemfireCache" pdx-read-serialized="true">
</gfe:cache>
<gfe:cache-server cache-ref="gemfireCache" port="0"></gfe:cache-server>
<gfe:partitioned-region id="examplePartionRegion1" copies="1" ></gfe:partitioned-region>
<gfe:replicated-region id="exampleReplicatedRegion1"></gfe:replicated-region>
<context:annotation-config/>
</beans>
Failure Scenarios
Scenario 1:SymptomThe GemFire CacheServer fails to create the regions as expected despite the Spring xml configuration file being correctly set in the "
--spring-xml-location" option. However, there are no exception/error messages in the CacheServer's log when CacheServer is starting.
Root CauseMost likely the CacheServer didn't find the Spring & Spring Data GemFire jars in the "
CLASSPATH" environment variable.
ResolutionYou need to add the below Spring Data GemFire and Spring dependency jars into the "
CLASSPATH" environment variable.
spring-data-gemfire-X.X.X.RELEASE.jar
spring-core-X.X.X.RELEASE.jar
spring-beans-X.X.X.RELEASE.jar
spring-tx-X.X.X.RELEASE.jar
Note:You can find the Spring Data GemFire(SDG) and Spring Framework dependency combinations from theĀ
compatibility matrix table.
Scenario 2:
Symptom
When starting a CacheServer with "
--spring-xml-location" option, it fails with an
InvalidDataAccessApiUsageException.
[pivhdsne:GemfireCluster1]$ ./startSpringXMLServer.sh
..The Cache Server process terminated unexpectedly with exit status 1. Please refer to the log file in /home/gpadmin/apps/GemfireCluster1/server1 for full details.
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/dao/InvalidDataAccessApiUsageException
at org.springframework.data.gemfire.support.SpringContextBootstrappingInitializer.init(SpringContextBootstrappingInitializer.java:400)
at org.springframework.data.gemfire.support.SpringServerLauncherCacheProvider.createCache(SpringServerLauncherCacheProvider.java:57)
at org.apache.geode.distributed.ServerLauncher.createCache(ServerLauncher.java:881)
at org.apache.geode.distributed.ServerLauncher.start(ServerLauncher.java:803)
at org.apache.geode.distributed.ServerLauncher.run(ServerLauncher.java:732)
at org.apache.geode.distributed.ServerLauncher.main(ServerLauncher.java:251)
Caused by: java.lang.ClassNotFoundException: org.springframework.dao.InvalidDataAccessApiUsageException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 6 more
Root CauseThe CacheServer failed to find all the necessary Spring Framework dependency jars from the "
CLASSPATH" environmental variable.
ResolutionYou need to add all the following Spring dependency jars into the "
CLASSPATH" environmental variable.
spring-core-X.X.X.RELEASE.jar
spring-beans-X.X.X.RELEASE.jar
spring-tx-X.X.X.RELEASE.jar
Scenario 3:
Symptom
When starting a CacheServer with "--spring-xml-location" option, it fails with ApplicationContextException.
[pivhdsne:GemfireCluster1]$ ./startSpringXMLServer.sh
..The Cache Server process terminated unexpectedly with exit status 1. Please refer to the log file in /home/gpadmin/apps/GemfireCluster1/server1 for full details.
Exception in thread "main" org.springframework.context.ApplicationContextException: Failed to bootstrap the Spring ApplicationContext; nested exception is org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 21 in XML document from class path resource [springxml/spring-gemfire-config_new.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 21; columnNumber: 30; The prefix "context" for element "context:annotation-config" is not bound.
at org.springframework.data.gemfire.support.SpringContextBootstrappingInitializer.init(SpringContextBootstrappingInitializer.java:448)
at org.springframework.data.gemfire.support.SpringContextBootstrappingInitializer.init(SpringContextBootstrappingInitializer.java:400)
at org.springframework.data.gemfire.support.SpringServerLauncherCacheProvider.createCache(SpringServerLauncherCacheProvider.java:57)
at org.apache.geode.distributed.ServerLauncher.createCache(ServerLauncher.java:881)
at org.apache.geode.distributed.ServerLauncher.start(ServerLauncher.java:803)
at org.apache.geode.distributed.ServerLauncher.run(ServerLauncher.java:732)
at org.apache.geode.distributed.ServerLauncher.main(ServerLauncher.java:251)
Caused by: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 21 in XML document from class path resource [springxml/spring-gemfire-config_new.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 21; columnNumber: 30; The prefix "context" for element "context:annotation-config" is not bound.
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:399)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:614)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:514)
at org.springframework.data.gemfire.support.SpringContextBootstrappingInitializer.refreshApplicationContext(SpringContextBootstrappingInitializer.java:321)
at org.springframework.data.gemfire.support.SpringContextBootstrappingInitializer.init(SpringContextBootstrappingInitializer.java:433)
... 6 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 21; columnNumber: 30; The prefix "context" for element "context:annotation-config" is not bound.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:441)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:325)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:289)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2786)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:348)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:76)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadDocument(XmlBeanDefinitionReader.java:429)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:391)
... 19 more
Exception in thread "Thread-1" java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:170)
at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1034)
at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1009)
at org.springframework.context.support.AbstractApplicationContext$2.run(AbstractApplicationContext.java:929)
Root CauseThe
spring-gemfire-config.xml which you are using is lacking the attribute "
xmlns:context="http://www.springframework.org/schema/context" when you are using "
<context:annotation-config/>" in
spring-gemfire-config.xml.
ResolutionAdding the attribute "
xmlns:context="http://www.springframework.org/schema/context" to the xml tag in your
spring-gemfire-config.xml should resolve the issue.