【问题标题】:SpringBoot - GemFire - Initialize Locators and port while start upSpringBoot - GemFire - 启动时初始化定位器和端口
【发布时间】:2017-07-20 17:30:11
【问题描述】:

我正在使用带有 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> 

在spring启动时导入了这个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

【问题讨论】:

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


    【解决方案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'”。

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

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

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

    干杯, 约翰

    【讨论】:

    • 谢谢约翰。如何将主机和端口配置从 springxml config 移动到 GeodeConfig java 代码。
    • [警告 2017/07/20 15:23:35.683 CDT tid=0x22] 无法连接到:localhost:40404 java.net.ConnectException:连接被拒绝:连接
    • 你需要注册一个静态的propertySourcesPlaceholderConfigurer(例如github.com/jxblum/contacts-application/blob/master/…)bean定义来解析你的Spring JavaConfig(上图)@Value注解中的属性占位符.
    • 此外,您还可以为属性占位符设置默认值,如... @Value("${host:localhost}") String host@Value("${port:10334}") int port
    • 在为 propertySourcesPlaceholderConfigurer 添加 bean 后得到相同的错误。还硬编码定位器和端口的值得到相同的错误。仅供参考,我已从 xml 配置中删除标签
    猜你喜欢
    • 2018-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多