Skip to content
This repository has been archived by the owner on Feb 23, 2022. It is now read-only.

Spring MongoDB

markpollack edited this page Apr 12, 2011 · 3 revisions

Spring MongoDB

This section describes how you can configure your Spring application to interact with a MongoDB service in Cloud Foundry. For more information on Spring integration with MongoDB refer to the Spring Data MongoDB page.

The documentation for the Spring Hello MongoDB Sample Application is also a good place to look for information on getting started with Spring and MongoDB on Cloud Foundry.

Requirements

This example will use the new cloud XML namespace

To use this namespace add the following repository to your maven configuration

<repository>
    <id>spring-milestone</id>
    <name>Spring Maven MILESTONE Repository</name>
    <url>http://maven.springframework.org/milestone</url>
</repository>

and the following dependency

<dependency>
  <groupId>org.cloudfabric</groupId>
  <artifactId>cloudfoundry-runtime</artifactId>
  <version>0.6.0-BUILD-SNAPSHOT</version>
</dependency>

Environment variables

When the application is deployed in Cloud Foundy environment variables are used to describe the services and how to authenticate with them. The information for all services bound to your application is under the variable name VCAP_SERVICES and is shown below for the case of binding a Redis, MySQL and MongoDB service to the application (pretty printed a little)

VCAP_SERVICES = {
   "redis-2.2":[{"name":"redis-hello-mongo","label":"redis-2.2","plan":"free","credentials":{"node_id":"redis_node_3","hostname":"172.30.48.42","port":5005,"password":"fedb27a6-9968-4a18-a03a-c07a1f30d6ef","name":"redis-2ae54a28-0192-4f9b-941f-4845e44fad20"}}],
   "mysql-5.1":[{"name":"mysql-hello-mongo","label":"mysql-5.1","plan":"free","credentials":{"node_id":"mysql_node_3","hostname":"172.30.48.22","port":3306,"password":"pBhRT4A6FIKSe","name":"d5c4d2e84cf4a4104befdb9a6cff5eda0","user":"upP8A9d0nSmPe"}}],
   "mongodb-1.8":[{"name":"mongodb-hello-mongo","label":"mongodb-1.8","plan":"free","credentials":{"hostname":"172.30.48.65","port":25003,"username":"f920a767-eaa1-48e2-a4ca-a3f19b85b283","password":"2136e733-740c-4be8-950e-acde3307ee90","name":"mongodb-a0586078-c3d9-41a0-873f-341691d740b2","db":"db"}}]
}

Breaking appart the MongoDB specific information we have

"mongodb-1.8":[
  {"name":"mongodb-hello-mongo",
   "label":"mongodb-1.8",
   "plan":"free",
   "credentials":{"hostname":"172.30.48.65","port":25003,"username":"f920a767-eaa1-48e2-a4ca-a3f19b85b283","password":"2136e733-740c-4be8-950e-acde3307ee90","name":"mongodb-a0586078-c3d9-41a0-873f-341691d740b2","db":"db"}}

]}

You can read this information into your application using Java's environment variable API and/or existing Spring XML features but it is easer to consume this information using the new cloud namespace which parses it out into a convenient Properties object.

The key names in the java.util.Properties object createdx by the cloud namespace for the MongoDB service functionality are those in the credentials JSON string shown above. Nicely formatted these are:

<mongo-service-name>.hostname
<mongo-service-name>.port
<mongo-service-name>.username
<mongo-service-name>.password    
<mongo-service-name>.name
<mongo-service-name>.db

is the name you gave the MongoDB service that was boudn to the applcation. The name of the "db" is always "db" whereas the other variables will change for each service you create.

Cloud namespace

Here is an example namespace that uses Spring 3.1 new profile feature in addition to the cloud namespace. Note, you can use the cloud namespace without the profile feature in Spring 3.0.

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:cloud="http://schema.cloudfoundry.org/spring"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
            http://schema.cloudfoundry.org/spring http://schema.cloudfoundry.org/spring/cloudfoundry-spring-0.6.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">


<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
	    <constructor-arg ref="mongo"/>
	    <constructor-arg name="databaseName" value="#{serviceProperties['mongodb-hello-mongo.db']}"/>
	    <constructor-arg name="defaultCollectionName" value="person"/>
	    <property name="username" value="#{serviceProperties['mongodb-hello-mongo.username']}"/>    	
	    <property name="password" value="#{serviceProperties['mongodb-hello-mongo.password']}"/>     	
    </bean>

<beans profile="default">
      <bean id="mongo" class="com.mongodb.Mongo"/>
  <util:properties id="serviceProperties">		
        <prop key="mongodb-hello-mongo.db">pwdtest</prop>
        <prop key="mongodb-hello-mongo.username">test_user</prop>
        <prop key="mongodb-hello-mongo.password">efgh</prop>
      </util:properties>
</beans>
    
<beans profile="cloud">
      <cloud:mongo id="mongo"/>
      <cloud:service-properties id="serviceProperties"/>		
</beans>

</beans>

In the 'cloud' profile the cloud:service-properties/ element will populate a java.util.Properties object with they key values listed in the previous section. In this case the name of the application is 'mongodb-hello-mongo'. The Spring Expression Language to set the username and property values of MongoTemplate. We have also setup the 'default' profile - the local one - to also use authentiation.

Accessing MongoTemplate within your application

You can now access MongoTemplate in your class, for example in the controller, via annotation based dependency injection.

@Controller
public class HomeController {

@Autowired MongoTemplate template;

// controller methods here...

}

As with any Spring bean, you could have also configured the HomeController using XML or Java based bean metadata.

Clone this wiki locally