Spring PropertyPlaceholderConfigurer和MongoDB副本集

时间:2011-11-16 22:42:14

标签: spring mongodb

在我的spring环境中,我使用PropertyPlaceholderConfigurer从env.properties加载属性:

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="locations">
            <list>
                <value>classpath:env.properties</value>
            </list>
        </property>
</bean>

在我的env.properties中,我有以下内容:

db.mongo.replicationset=server1:1111, server2:2222, server3:3333

最后,在春天的背景下:

<mongo:mongo id="mongo" replica-set="${db.mongo.replicationset}">
    <mongo:options slave-ok="true"/>
</mongo:mongo>

我看到java.lang.ArrayIndexOutOfBoundsException:

ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [root-context.xml]; nested exception is java.lang.ArrayIndexOutOfBoundsException: 1
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:412)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:124)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:93)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:467)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:397)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:282)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:204)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4205)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4704)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
    at org.apache.catalina.core.StandardService.start(StandardService.java:525)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)

所有内容都适用于硬代码复制 - 如下所示:

<mongo:mongo id="mongo" replica-set="server1:1111, server2:2222, server3:3333">
    <mongo:options slave-ok="true"/>
</mongo:mongo>

我的猜测是冒号或逗号可能是PropertyPlaceholderConfigurer的特殊字符,因此无法使用。

如何使用PropertyPlaceholderConfigurer和属性文件进行此配置。

修改 spring-data-mongodb修复此问题:1.0.0.RELEASE和spring-data-mongodb:1.1.0.M1

1 个答案:

答案 0 :(得分:3)

这实际上是最新的Spring-data-mongo M5里程碑中的一个错误 http://forum.springsource.org/showthread.php?114672-Replica-set-configuration-problem

以下作品:

<mongo:mongo
replica-set="${mongo.replicaset.1}:${mongo.replicaset.1.port},${mongo.replicaset.2}:${mongo.replicaset.2.port},${mongo.replicaset.3}:${mongo.replicaset.3.port}">