Spring Data MongoDB on Cloud Foundry tutorial Part 1

Hi, in this tutorial I will show you how to develop a Spring powered MongoDB project, with Gradle as dependency management, and how to deploy it on Cloud Foundry. The app will be a web app deployed on Tomcat.

Cloud Foundry allows you to run many instances of your application, add and remove services, when needed, and so on. You may want to read some info on getting started.

Register on Cloud Foundry

The first step is to register on Cloud Foundry. You will either manage your cloud app using a command line VMC tool, or using Cloud Foundry extension for Spring Tool Suite/Eclipse. I chose to use the IDE independent VMC.

The Spring Data MongoDB app

The Spring app I am using is available to download from my GitHub (specifically, the version for this article is tagged as BlogPost1 and you can also get it from the tags view). Below I will show you some of the steps you need to make to get your Spring MongoDB app up and running - locally for now. More information is available from the Cloud Foundry Spring Application Development page.

Things to remember:

  • Cloud Foundry technology support is limited, so you have to figure out beforehand whether your stack will even work.
  • At this moment, Java 7 is not supported on Cloud Foundry, so you need to downgrade your project to Java 6.
  • Similarly, only certain versions of Spring and Spring Data MongoDB are supported.
  • Only Tomcat 6 is supported at the moment. Locally I'm developing against Tomcat 7, but due to Cloud Foundry limitations it has to work on Tomcat 6 as well (no problems with that).
  • Servlet Spec 3.0 is not supported - back to 2.5.
First, the build.gradle file with dependencies. Gradle dependency resolution strategy is different from that of Maven, but if you prefer Maven, the dependencies should be similar enough.
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'

group = 'me.m1key.audiolicious'
version = '0.0.1-SNAPSHOT'

sourceCompatibility = 1.6
targetCompatibility = 1.6

repositories {
   mavenRepo name: "spring-test-mvc", url: 'http://repo.springsource.org/libs-milestone/'
   mavenRepo url:'http://maven.springframework.org/milestone/'

project.ext.finalName = 'audiolicious-cloud.war'

war { archiveName = finalName }
task deployment(type: Copy) {
   from('build/libs/' + finalName)

project.ext.springVersion = '3.1.2.RELEASE'

dependencies {
   compile group: 'org.springframework', name: 'spring-beans', version: springVersion, force: true
   compile group: 'org.springframework', name: 'spring-context', version: springVersion, force: true
   compile group: 'org.springframework', name: 'spring-core', version: springVersion, force: true
   compile group: 'org.springframework', name: 'spring-webmvc', version: springVersion, force: true

   compile group: 'org.springframework.data', name: 'spring-data-mongodb', version: '1.0.4.RELEASE'
   compile group: 'org.mongodb', name: 'mongo-java-driver', version: '2.9.1'

   compile group: 'org.slf4j', name: 'slf4j-api', version: '1.6.6'
   compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.6.6'
   compile group: 'org.apache.velocity', name: 'velocity', version: '1.7'

   compile group: 'org.cloudfoundry', name: 'cloudfoundry-runtime', version: '0.8.2'

   testCompile group: 'org.springframework', name: 'spring-test', version: springVersion
   testCompile group: 'junit', name: 'junit', version: '4.+'
   testCompile 'org.hamcrest:hamcrest-all:1.3'
   testCompile 'javax.servlet:servlet-api:2.5'
   testCompile 'org.springframework:spring-test-mvc:1.0.0.M2'

configurations.all {
   resolutionStrategy {
       force group: 'org.springframework', name: 'spring-aop', version: springVersion
       force group: 'org.springframework', name: 'spring-expression', version: springVersion
       force group: 'org.springframework', name: 'spring-tx', version: springVersion
Meanwhile, in your src/main/webapp/WEB-INF/web.xml...
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   id="AudioliciousCloud" version="2.5" metadata-complete="true">
   <display-name>Spring MVC tutorial</display-name>


And the Spring servlet file (src/main/resources/audiolicious-servlet.xml).
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:mongo="http://www.springframework.org/schema/data/mongo"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
 http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">

   <!-- Enabling Spring beans auto-discovery -->
   <context:component-scan base-package="me.m1key.audiolicious.cloud" />

   <!-- Enabling Spring MVC configuration through annotations -->
   <mvc:annotation-driven />

   <bean id="velocityConfig"
       <property name="resourceLoaderPath">

   <!-- Defining which view resolver to use -->
   <bean id="viewResolver"
       <property name="prefix">
       <property name="suffix">

   <mongo:db-factory id="mongoDbFactory" dbname="adlcs_cloud"
       host="" port="27017" />

   <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
       <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />

   <mongo:repositories base-package="me.m1key.audiolicious.cloud.repositories" />
What's important here is MongoDB configuration. If you configure it this way, it will allow Cloud Foundry to inject its own values for host name and port, so that it also works on the cloud.

MongoDB repository

One of the nice features of Spring Data MongoDB is that you can (just like in Grails) ask it to create simple repositories for you - you just define interfaces, Spring will provide the implementation.
package me.m1key.audiolicious.cloud.repositories;
import me.m1key.audiolicious.cloud.entities.Song;
import org.springframework.data.repository.CrudRepository;

public interface SongRepository extends CrudRepository<Song, Long> {
This gives me a CRUD repo that I don't have to implement myself. You can add methods to this interface, such as findByLastName, and Spring will know how to implement them. For more information on this, see the repositories reference. You can extend this behaviour by implementing your own, more sophisticated methods.

Entity. Notice no annotations:

package me.m1key.audiolicious.cloud.entities;

public class Song {

    private String name;
    private String albumName;
    private String artistName;
    private String songKey;

    public Song(String name, String albumName, String artistName, String songKey) {
        this.name = name;
        this.albumName = albumName;
        this.artistName = artistName;
        this.songKey = songKey;

    public String getName() {
        return name;
    // Omitted for brevity.

In part 2 of this tutorial I will show you how to deploy this on Cloud Foundry.

PS. Tomcat debugging

This is the stuff I added in my catalina.sh (this is Linux syntax then) file to get remote debugging from my IDE to work.
JAVA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n $JAVA_OPTS"


  1. Very nice blog, Thanks for sharing grate article.
    You are providing wonderful information, it is very useful to us.
    Keep posting like this informative articles.
    Thank you.

    From: Field Engineer
    Cloud Architect Certification

  2. Great Article Cloud Computing Projects

    Networking Projects

    Final Year Projects for CSE

    JavaScript Training in Chennai

    JavaScript Training in Chennai

    The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

  3. This comment has been removed by the author.

  4. We provide 24/7 customer service to answer your questions and comments efficiently, polite to make sure you always receive vegus168 แทงบอลออนไลน์ from our friendly staff.

  5. Vegus168 คาสิโนออนไลน์ ทางเข้า แทงบอล แทงหวย สมัครสมาชิก โปรโมชั่น เครดิตฟรี 2,000 บาท สมัครง่าย


  6. Every weekend i used to pay a quick visit this web site,
    because i want enjoyment, for the reason that this this
    web page conations really nice funny data too.

    Feel free to visit my blog - 휴게텔

  7. Nice Blog. Thanks for sharing with us. Such amazing information.

    All you need to know about marriage laws and Divorce

  8. https://namnak.com/mobile-repairs.p80051part.
    Participate in theoretical and practical tests
    In order to be certified in the field of mobile repairs, you need to take theoretical and practical tests. In these tests, everything you have already learned will help you. You need to get the desired score from these exams in order to be known as a mobile repairman and be able to work in the market.

  9. https://www.titrebartar.com/fa/news/172685/meet-the-best-specialists-in-vision-problems-in-iran In general, an ophthalmologist, to treat problems such as; Obstructor of lacrimal ducts, corneal opacity, dry eye, Retinal decolan, Eye refractive disorder (two nose and nearest nose and camera), high eyelid loss, blur and visual loss, laziness, shingle, eye disease, eye inflammation, eye inflammation, eye inflammation, Rupture and cornea ulcers, increase in intraocular pressure, eye cancer, internal inflammation of the eyes, cataract water, eye infections, pearliness, eyelashes, black water, and ... with prescribing glasses, medicine or surgery.

  10. After the events that happened to Bitcoin in 2020, the digital currency market boomed among the people. This event starts the market bullish every four years. However, this year the situation was a little different. The uptrend that the digital currency https://www.golchinonline.ir/index.php/newposts/news/6344-%D8%A8%D8%B1%D8%B1%D8%B3%DB%8C-%D8%A7%D8%B1%D8%B2-%D8%AF%DB%8C%D8%AC%DB%8C%D8%AA%D8%A7%D9%84-%D8%A7%D9%84%D9%88%D9%86.html market experienced this time was different from the uptrend that everyone experienced.

  11. This comment has been removed by the author.

  12. Generally, programming assignments are charged differently from other assignments, but the charging procedure is as usual from the programming programming assignment help services. If you ask for the average price to get a programming assignment done, then it can vary between $100-180, which is still high enough for a student. But, to get the quality we are looking for, this is the amount that we will need.