Use Cloudfoundry in Spring without the dedicated XML namespace

Spring allows you to configure a dead simple connection on a provisioned service, like mysql, redis, rabbitmq and many others. It’s really simple, since you don’t even need to configure explicit credentials and connection strings. Instead, you can retrieve a reference to this service from the cloud itself using the CloudFoundry “cloud” namespace.

Problem is, you have to create an XML file, and we have more and more projects without XML anymore. So, how to use the Cloudfoundry integration without XML?

Like the XML version, you need the Maven dep:


The package org.cloudfoundry.runtime.service offers 4 implementations of AbstractCloudServiceFactory:
– CloudDataSourceFactory
– CloudMongoDbFactoryBean
– CloudRabbitConnectionFactoryBean
– CloudRedisConnectionFactoryBean

So, depending of the type of your service, choose the right implementation and create a configuration class. Here is an example using Redis:

import javax.inject.Inject;

import org.cloudfoundry.runtime.service.CloudPoolConfiguration;
import org.cloudfoundry.runtime.service.keyvalue.CloudRedisConnectionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;

@ComponentScan(basePackages = { "..." })
public class KeyValueConfig {

 private RedisConnectionFactory redisConnectionFactory;

 public StringRedisTemplate redisTemplate() {
  StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(
  return stringRedisTemplate;

  * Properties to support the local and test mode of operation.
 @Profile({ Profiles.LOCAL, Profiles.TEST, Profiles.PROD })
 static class Default {

  private Environment environment;

  public RedisConnectionFactory redisConnectionFactory() {
   JedisConnectionFactory redis = new JedisConnectionFactory();
   redis.setPort(environment.getProperty("redis.port", Integer.class));
   return redis;

  * Properties to support the cloud mode of operation.
 static class Cloud {

  public RedisConnectionFactory redisConnectionFactory() throws Exception {
   CloudPoolConfiguration cloudPoolConfiguration = new CloudPoolConfiguration();

   CloudRedisConnectionFactoryBean factory = new CloudRedisConnectionFactoryBean();

   return factory.getObject();

And that’s all!

Related Posts

Leave a comment

About privacy:

This site uses Akismet to reduce spam. Learn how your comment data is processed.