0

I am using springboot 1.5.2 with gemfire 8.2 and configure the host and port in the xml is working good. Instead we hard code the value of host and port need to read from cloud server config and its was not able to read those value in xml. Planned to move the host and port setting from xml to java code. While start up getting below error.

Existing XML configuration

<gfe:pool id="clientPool" subscription-enabled="true">
        <gfe:locator host="x.x.x.x" port="x" />
    </gfe:pool> 

Imported this xml in the spring start up.

XML to Java code

@Configuration
public class GeodeConfig {

    @Resource
    GemFireCache gemfireCache;

    @Bean
    ClientCacheFactoryBean gemfireCache() {
        ClientCacheFactoryBean gemfireCache = new ClientCacheFactoryBean();

        gemfireCache.setClose(true);
        gemfireCache.setCacheXml(new ClassPathResource("gemfirexml.xml"));
        return gemfireCache;
    }

    @Bean
    PoolFactoryBean gemfirePool(
          @Value("${host}") String host,
          @Value("${port}") int port) {
        PoolFactoryBean gemfirePool = new PoolFactoryBean();
        gemfirePool.setName("clientPool");
        gemfirePool.setSubscriptionEnabled(true);
        gemfirePool.setThreadLocalConnections(false);
        gemfirePool.setServers(Collections.singletonList(new ConnectionEndpoint(host, port)));
        return gemfirePool;
    }


}

Exception

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-07-20 12:17:51.486 ERROR [magenta-enterprise-event-testing,,,] 22640 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'geodeConfig': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gemfireCache': FactoryBean threw exception on object creation; nested exception is com.gemstone.gemfire.cache.CacheXmlException: Unknown XML element "beans"
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:321) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[sprin
John Blum
  • 7,381
  • 1
  • 20
  • 30
Vigneshwaran
  • 229
  • 1
  • 3
  • 14

1 Answers1

1

@Vigneshwaran-

The ClientCacheFactoryBean.setCacheXml(:Resource) is for setting a reference to a GemFire native cache.xml resource, not Spring XML config, hence the Exception...

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gemfireCache': FactoryBean threw exception on object creation; nested exception is com.gemstone.gemfire.cache.CacheXmlException: Unknown XML element "beans"

In particular... nested exception is com.gemstone.gemfire.cache.CacheXmlException: Unknown XML element "beans" and specifically... "Unknown element 'beans'".

"beans" is clearly a Spring XML config element, from the Spring beans namespace. Of course GemFire's native cache.xml parser does not know anything about Spring XML config and namespaces (e.g. beans).

If you want to use Spring XML config in conjunction with Spring's JavaConfig, then do the following...

@Configuration
@ImportResource("class/path/to/spring/config.xml")
class GeodeConfig {
 ...
}

Cheers, John

John Blum
  • 7,381
  • 1
  • 20
  • 30
  • Thanks John. How to move the host and port configuration from springxml config to GeodeConfig java code. – Vigneshwaran Jul 20 '17 at 20:28
  • [warn 2017/07/20 15:23:35.683 CDT tid=0x22] Could not connect to: localhost:40404 java.net.ConnectException: Connection refused: connect – Vigneshwaran Jul 20 '17 at 20:29
  • You need to register a static `propertySourcesPlaceholderConfigurer` (e.g. https://github.com/jxblum/contacts-application/blob/master/configuration-example/src/main/java/example/app/spring/java/geode/server/JavaConfiguredGeodeServerApplication.java#L63-L66) bean definition to resolve the property placeholders in the `@Value` annotations of your *Spring* JavaConfig (above). – John Blum Jul 20 '17 at 20:59
  • Additionally, you can also set default values for your property placeholders, as in... `@Value("${host:localhost}") String host` and `@Value("${port:10334}") int port`. – John Blum Jul 20 '17 at 21:01
  • After adding bean for propertySourcesPlaceholderConfigurer getting same error. Also hard coded the value for locators and port getting same error.FYI I have removed tag from xml configuration – Vigneshwaran Jul 20 '17 at 23:47
  • Could you please clarify which error you are getting now? The original error you posted concerning, "nested exception is com.gemstone.gemfire.cache.CacheXmlException: Unknown XML element "beans"" has nothing to do with resolving property placeholders using the propertySourcePlaceholderConfigurer. – John Blum Jul 22 '17 at 06:05