Skip to content
This repository has been archived by the owner on Feb 23, 2022. It is now read-only.
markfisher edited this page May 25, 2011 · 24 revisions

Spring

There are several Spring components that make application development on Cloud Foundry simple.

  • STS - Eclipse tooling support
  • Cloud namespace - configure cloud based services
  • Profiles - group together sets of related bean definitions (local/cloud/dev/prod) in XML or Java
  • Spring Data - Spring programming model support for new data access technologies such as Hadoop, NoSQL databases and updates to Spring's RDBMS integration. Relevant projects for the current services offered by Cloud Foundry are Spring Data JPA, Spring Data Key Value - Redis and Spring Data Document MongoDB services.

Mark Fisher's blog entitled Cloud Foundry for Spring Developers provides an excellent introduction to using Spring and Cloud Foundry together.

STS

Please refer to the Getting Started Guide for STS users

Spring Cloud Foundry namespace support

To reference cloud based services within Spring use the new cloud namespace. The cloud namespace provides support for defining beans backed by services.

    <cloud:data-source/>

    <cloud:redis-connection-factory/>

    <cloud:mongo/>

This style works as long as you have only one service of each type bound to the application.

If you want to bind to specific services (perhaps because you have more than one of a kind of service, for example two MySql services), you need to specify the service-name attribute as follows:

    <cloud:data-source service-name="inventory-db"/>

    <cloud:data-source service-name="pricing-db"/>

In your Java code, simply add @Autowired dependencies for each bounded service:

    @Autowired DataSource dataSource;
    @Autowired RedisConnectionFactory redisConnectionFactory;
    @Autowired Mongo mongo;

You have access to services without breaking a sweat.

There is also a properties factory bean:

<cloud-service-properties id="serviceProperties"/>

and the properties exposed are in the form <service-name>.<option>. The options are from the credentials section of the service info (so username, password are common, and you might see things like virtualhost for rabbit).

Read the page on Cloud Foundry Environment Variables for additional information.

The service name will be used as the bean id by default, but if you would prefer to use a separate value for that, you can specify the 'id' attribute. Regardless of how the id is determined, you can specify the @Qualifier annotation to pick the right bean or even use the XML based configuration. Please check the Spring Framework documentation for more details.

To access the namespace, add the following to your maven pom.xml

<dependency>
  <groupId>org.cloudfoundry</groupId>
  <artifactId>cloudfoundry-runtime</artifactId>
  <version>0.6.1</version>
</dependency>

and the following maven repository location

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

Command Line and Development

Please refer to the Getting Started Guide for Command Line (VMC) users

Services

The initial set of services offered on Cloud Foundry are

Spring provides strong integration with all of these services. The features that Spring offers for each service is discussed in the following sections.

RDBMS

Spring has always provided great support for relational databases. Spring's JDBC and ORM support can be used within Cloud Foundry.

A new Spring project, Spring JPA, provides great infrastructure support to further kickstart your development. It will automatically provide implementations of of Repository interfaces including support for custom finder methods. There is also integration wit QueryDSL so that you can write type-safe queries.

The bootstrapping of a datasource is the only thing you need to be concerned with.

If you are using Spring's cloud namespace, then in your Spring XML configuration file declare the following element

    <cloud:data-source/>

The service name will be used as the bean id by default, but if you would prefer to use a separate value for that, you can specify the 'id' attribute. You can then reference within other Spring XML definitions or within your classes using @Autowired or @Inject annotations.

Redis

To quote the Redis project home page: "Redis is an advanced key-value store. It is similar to memcached but the dataset is not volatile, and values can be strings, exactly like in memcached, but also lists, sets, and ordered sets. All this data types can be manipulated with atomic operations to push/pop elements, add/remove elements, perform server side union, intersection, difference between sets, and so forth. Redis supports different kind of sorting abilities."

Part of the umbrella Spring Data project, the Spring Data Key Value provides integration with Redis.

Spring Redis integration provides easy configuration and access to Redis from Spring application. Offers both low-level and high-level abstraction for interacting with the store, freeing the user from infrastructural concerns.

Sample application

The sample application RetwisJ is running on Cloud Foundry and showcases various Spring Redis features. Please read the RetwisJ documentation and look at the source code.

MongoDB

MongoDB provides a document oriented data model which rich query abilities that can scale to handle high performance read and writes as provide automatic sharing for scalability.

Spring MongoDB integration provides object to document mapping functionality, Java based Query, Criteria, and Update DSLs, QueryDSL integration to support type-safe queries, and Cross-store persistence (support for JPA Entities with fields transparently persisted/retrieved using MongoDB) to name a few of it more predominate features.

Sample application

Spring Hello MongoDB sample is described in detail here

The WGRUS sample uses MongoDB for the Spring Integration MessageStore (claim check pattern). Look at the source code. Spring Mongo at present does not enforce authentication so the <cloud:mongo/> feature gives you an unautheticated instance of Mongo. If you are using Spring Mongo, then the MongoTemplate has settings for username and password (see Spring MongoDB docs).