Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
114 lines (83 sloc) 6.99 KB

Creating Cloud Foundry Spring application with MongoDB support


Cloud Foundry is a new open source platform as a service from VMWare. It is currently in beta, but you can request invitation to participate in testing. Cloud foundry allows deploying Spring, Rails, Scala and Node.js applications, while available application services are MongoDB, MySQL, PostgreSQL, RabbitMQ and Redis. There is also Cloud Foundry Micro, which is basically a Virtual machine image of Cloud Foundry, so that you can develop and test Cloud Foundry applications on your computer before you put them to the cloud. I haven't tested Cloud Foundry Micro yet, but I plan to do so, soon.

Recently I received an email from Cloud Foundry accepting my request to participate in beta testing of their service. They sent me credentials and basic instructions how to log in and start using Cloud Foundry, so I decided to build simple Spring application to try it out and share my experiences here. As a backing database I will use MongoDB, accessed through MongoTemplate from Spring data project.

Getting our hands dirty

So, let's first create a new web application from maven archetype:

mvn archetype:generate -DgroupId=eu.semberal.example -DartifactId=cloudfoundry-mangodb-app
    -DarchetypeArtifactId=webapp-jee5 -DarchetypeVersion=1.3

Now let's add a new repository containing Spring framework milestone artifacts to our pom.xml:

    <name>SpringSource milestone repository</name>

and dependencies we will need:

<dependency> <!-- cloud foundry runtime (necessary for the cloud namespace to work correctly) -->

In application context we will use component scan feature to discover our beans and, therefore, in our applicationContext.xml there will be just the definition of MongoTemplate, which will be autowired into our dao classes and used for communication with underlying MongoDB service. Spring application context configuration follows:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

    <context:component-scan base-package="eu.semberal.cloudfoundrysample.dao"/>
    <context:component-scan base-package="eu.semberal.cloudfoundrysample.service"/>

    <bean id="mongoTemplate" class="">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />

    <cloud:mongo-db-factory id="mongoDbFactory"/>

You can see the cloud namespace here. By default, parameters for configuring MongoDB are passed in Cloud Foundry via environment variables. Cloud namespace simplifies the configuration by autodetecting MongoDB service and autoconfiguring MongoDbFactory which is passed to the constuctor of MongoTemplate.

Now it's time to create service and dao beans, controllers, entities, configure Spring MVC and web.xml properly, create JSP views, etc. I won't put all the code here, but you can find everything in zipped sources attached to this post.

Putting it up there

So, when our application is ready, we can deploy it to Cloud Foundry. If you are a SpringSource Tool Suite user, you can deploy applications right from your IDE. Those who are not STS users (including me) must settle for command-line interface called vmc. If you have ruby in your classpath, you can install vmc with command:

gem install vmc

After successful installation of vmc gem, we can connect to Cloud Foundry and authenticate with e-mail and password from the e-mail:

vmc target
vmc login

After successful login we can start deploying the application. So, let's move to the directory with the project and type:

vmc push

During the deployment it will ask for couple of things, such as name of the application, URL, memory allocation, requested services etc. It is a pretty straightforward configuration process and you shouldn't run into any problems. At the end you should see "Starting application: OK". If the application didn't start correctly, you will have to check logs to find out what happened and why the deployment failed. To get access to application logs, use command (vmc logs <appname>).

When I first deployed my application to Cloud Foundry and tried to access it, I was getting an error: "VCAP ROUTER: 404 - DESTINATION NOT FOUND", even though vmc confirmed successful startup with: "Starting Application: OK". After checking crash logs (vmc crashlogs <appname>), I found out that it ran out of memory (since I was greedy and allocated only 128M out of 2GB quota). After raising the amount of memory to 256MB (mem <appname> 256M), it started working.


You can see the application running at where you might add yourself to the list. Don't get scared of the look of the application. You can guess I'm neither talented web designer nor was it important to show you how Cloud Foundry works.

I hope this article was helpful to everyone beginning with Cloud Foundry. If you couldn't get my sample application working or had any other questions or comments, don't hesitate to ask in the discussion.

Source code: GitHub