Quantcast
Viewing all articles
Browse latest Browse all 128

Using Mongo namespace in Spring Profiles

I would like to use the same bean id while using the Mongo namespace for a Mongo instance in two separate Spring profiles. The only difference between the mongo:mongo configurations in the separate profiles is to support a stand-alone host or a replica set. The reason I want to do this is so I don't have to duplicate the configurations for the MongoDbFactory and the MongoTemplate configurations in each profile to reference a separate mongo:mongo ID (I want to define them one time outside of the Spring Profile blocks).

Spring profiles allow you to use the same id (as long as they are in separate profiles) when using the normal Spring bean "<bean>" but I have problems trying to do that with the mongo namespace. Here is my example, note the id attribute for "mongo:mongo" is identical in both profiles.

Code:

   
    <beans profile="mongoSingle">
        <mongo:mongo id="ReferenceCache" host="${ds-edge-cache-host}" port="${ds-edge-cache-port}" replica-set="">
            <mongo:options connections-per-host="${ds-edge-cache-connections-per-host}"
                          threads-allowed-to-block-for-connection-multiplier="${ds-edge-cache-blocking-multiplier}"
                          connect-timeout="${ds-edge-cache-connect-timeout}"
                          max-wait-time="${ds-edge-cache-max-wait-time}"
                          auto-connect-retry="${ds-edge-cache-auto-connect-retry}"
                          socket-keep-alive="${ds-edge-cache-socket-keep-alive}"
                          socket-timeout="${ds-edge-cache-socket-timeout}"
                          write-number="${ds-edge-cache-write-number}"
                          write-timeout="${ds-edge-cache-write-timeout}"
                    />
        </mongo:mongo>
       
    </beans>

    <beans profile="mongoReplica">
        <mongo:mongo id="ReferenceCache" replica-set="${ds-edge-cache-host}" port="${ds-edge-cache-port}">
            <mongo:options connections-per-host="${ds-edge-cache-connections-per-host}"
                          threads-allowed-to-block-for-connection-multiplier="${ds-edge-cache-blocking-multiplier}"
                          connect-timeout="${ds-edge-cache-connect-timeout}"
                          max-wait-time="${ds-edge-cache-max-wait-time}"
                          auto-connect-retry="${ds-edge-cache-auto-connect-retry}"
                          socket-keep-alive="${ds-edge-cache-socket-keep-alive}"
                          socket-timeout="${ds-edge-cache-socket-timeout}"
                          write-number="${ds-edge-cache-write-number}"
                          write-timeout="${ds-edge-cache-write-timeout}"
                          write-fsync="${ds-edge-cache-write-fsync}"
                    />
        </mongo:mongo>
    </beans>

When I try to deploy my example (above), I get a SAX parsing error:

Offending resource: class path resource [ds-reference.xml]; nested exception is org.springframework.beans.factory.xml.XmlBeanDefin itionStoreException: Line 43 in XML document from class path resource [ds-rest-cache-context.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-id.2: There are multiple occurrences of ID value 'ReferenceCache'.

Any help would be greatly appreciated.
Andy

Viewing all articles
Browse latest Browse all 128

Trending Articles