DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Deploying Spring Database Apps to CloudFoundry.com

Deploying Spring Database Apps to CloudFoundry.com

David Salter user avatar by
David Salter
·
Apr. 22, 11 · Java Zone · Interview
Like (0)
Save
Tweet
11.93K Views

Join the DZone community and get the full member experience.

Join For Free
Picture
Deploying Spring applications to CloudFoundry.com really is as easy as SpringSource say it is.

After being approved for a Cloud Foundry beta account, the first stage is to install Cloud Foundry support into STS or Eclipse. Christian Dupuis has an excellent blog post on how to achieve this, so I won't re-iterate what he has already said.

To deploy and run an application using a datasource, MySQL in my case, requires a bit more effort than deploying a standalone application, but literally very little.
To deploy an application with a datasource, you must first declare which datasource to use.  In Eclipse, open up the Cloud Foundry server and press the "Add" button on the services pane.
Picture
On the following screen, select a name and type for the datasource.
Press the "Finish" button and the datasource is registered.
Picture
Picture
After registering a datasource, you need to tell the application which datasource to use.  This is as straightforward as dragging the datasource onto the "Application  Services" panel for the Cloud Foundry server.
Picture
That's all the configuration that is needed for the server. Before deploying an application though, a couple of changes are needed to specify which datasource is required.
Because I'm deploying a Spring application, I need to change the application context file to point to the new Cloud Foundry database rather than a local database.  The nice thing about using a Cloud Foundry database is that database credentials are not needed, all that is needed is to change the datasource bean configuration in the servlet-context.xml file.
For a local deployment, a datasource configuration would look something like:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}" />
To configure this to use a Cloud Foundry MySQL database, the datasource configuration would look like:
?
1
<cloud:data-source id="dataSource" />
Spring 3.1 contains a new profiles feature to allow both of these configurations to be stored within the same context file. On Spring 3 however this feature is not available so the context file needs to either contain the regular bean dataSource definition or the new cloud data-source definition.
To access the new cloud tag, the servlet-context.xml needs changing to access the cloud namespace.
<beans xmlns="http://www.springframework.org/schema/beans"
...
xmlns:cloud="http://schema.cloudfoundry.org/spring"
...
http://schema.cloudfoundry.org/spring
http://schema.cloudfoundry.org/spring/cloudfoundry-spring-0.6.xsd">
To deploy the application, one final change is needed to add Cloud Foundry support. This is achieved by adding a dependency to Cloud Foundry in the applications pom.xml file.
<!-- CloudFoundry -->
<dependency>
<groupId>org.cloudfoundry</groupId>
<artifactId>cloudfoundry-runtime</artifactId>
<version>${org.cloudfoundry-version}</version>
</dependency>
<properties>
<org.cloudfoundry-version>0.6.0</org.cloudfoundry-version>
</properties>
After making these changes, the Cloud Foundry application can be deployed and started and stopped using the controls within STS.

From http://www.davidsalter.co.uk/1/post/2011/04/deploying-spring-database-apps-to-cloudfoundrycom.html

Spring Framework Cloud Foundry Database application Datasource Cloud app

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Optional in Java: When Better Code Is Not an Alternative
  • Developing a Cloud Adoption Strategy
  • Agile Micromanagement — Seriously?
  • How to Integrate a Distributed Database With Event Streaming

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo