SpringBoot - GemFire - 启动时初始化定位器和端口

时间:2017-07-20 17:30:11

标签: gemfile pivotal-cloud-foundry spring-data-gemfire

我正在使用带有gemfire 8.2的springboot 1.5.2并配置xml中的主机和端口正常工作。相反,我们硬编码主机和端口的值需要从云服务器配置读取,并且它无法在xml中读取这些值。计划将主机和端口设置从xml移动到java代码。虽然开始低于错误。

现有XML配置

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

在春季启动时导入此xml。

XML到Java代码

@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;
    }


}

异常

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

1 个答案:

答案 0 :(得分:1)

@Vigneshwaran -

ClientCacheFactoryBean.setCacheXml(:Resource)用于设置对GemFire native cache.xml resource Spring XML配置的引用,因此异常......

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"

特别是...... nested exception is com.gemstone.gemfire.cache.CacheXmlException: Unknown XML element "beans",特别是......“未知元素'豆'”。

beans ”显然是来自Spring beans namespace Spring XML配置元素。当然,GemFire的本地cache.xml解析器对 Spring XML配置和命名空间(例如 beans )一无所知。

如果您想将 Spring XML配置与Spring的JavaConfig结合使用,请执行以下操作...

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

干杯, 约翰