The Spring Service Connector is part of the Spring Cloud Connectors project.

This library provides ServiceConnectorCreator implementations for javax.sql.DataSource and various Spring Data connector factories. It also provides Java configuration and XML namespace support for connecting to cloud services, accessing cloud services, and accessing application properties.

The Java Configuration

Typical use of the Java configuration involves extending the AbstractCloudConfig class and creating beans for services by annotating methods with the @Bean annotation.

Tip

If you are migrating an application that uses auto-reconfiguration, you might first try the service-scanning approach until you need more explicit control.

The Spring Service Connector Java configuration also offers a way to expose application and service properties in case you want lower-level access when creating your own service connectors (or for debugging purposes, etc.).

Creating Service Beans

If you do not wish to extend AbstractCloudConfig, you can create your own Cloud object as an alternative.

@Bean
public Cloud cloud() {
  return new CloudFactory().getCloud();
}

The following example creates a DataSource bean (without configuration) using the getSingletonServiceConnector() method on Cloud.

@Bean
@ConfigurationProperties(DataSourceProperties.PREFIX)
public DataSource dataSource() {
  return cloud().getSingletonServiceConnector(DataSource.class, null);
}
Note

Following examples presume a configuration class which extends AbstractCloudConfig.

The configuration shown in the following example creates a DataSource bean that connects to the only relational database service bound to the application (it will fail if there is no such unique service). It also creates a MongoDbFactory bean, which again connects to the only MongoDB service bound to the application. (For ways to connect to other services, see the Javadoc for AbstractCloudConfig.ServiceConnectionFactory.)

class CloudConfig extends AbstractCloudConfig {
    @Bean
        public DataSource inventoryDataSource() {
            return connectionFactory().dataSource();
        }

    @Bean
    public MongoDbFactory documentMongoDbFactory() {
        return connectionFactory().mongoDbFactory();
    }

    // (More beans to obtain service connectors)
}

You can specify a bean name by providing a value in the @Bean annotation.

@Bean("inventory-service")

Otherwise, bean names will match the method names. (This works in the same way as does Spring’s Java configuration.)

If you have more than one service of a type bound to the application or want explicit control over the services to which a bean is bound, you can pass the service names to methods such as dataSource() and mongoDbFactory().

class CloudConfig extends AbstractCloudConfig {
    @Bean
    public DataSource inventoryDataSource() {
        return connectionFactory().dataSource("inventory-db-service");
    }

    @Bean
    public MongoDbFactory documentMongoDbFactory() {
        return connectionFactory().mongoDbFactory("document-service");
    }

    // (More beans to obtain service connectors)
}

Out of the box, the Spring Service Connector provides methods for connecting to a variety of services. For information on using the Java configuration to create connections to supported services, see below.

RabbitMQ

To connect to a unique RabbitMQ service, you can create a service bean using rabbitConnectionFactory(). The following example connects to the only RabbitMQ service bound to the application.

//Connect to the only available RabbitMQ service
@Bean
public RabbitConnectionFactory rabbitFactory() {
    return connectionFactory().rabbitConnectionFactory();
}

To connect to a specific RabbitMQ service, you can use an overloaded variant of rabbitConnectionFactory(). The following example connects specifically to the bunnymq RabbitMQ service.

//Connect to the 'bunnymq' RabbitMQ service
@Bean
public RabbitConnectionFactory rabbitFactory() {
    return connectionFactory().rabbitConnectionFactory("bunnymq");
}

To provide configuration for a RabbitMQ service, you can use an overloaded rabbitConnectionFactory() variant. The following example connects to the bunnymq RabbitMQ service and supplies configuration using a RabbitConnectionFactoryConfig, which is initialized with a channelCacheSize of 10.

//Connect to the 'bunnymq' RabbitMQ service, supplying configuration
@Bean
public RabbitConnectionFactory rabbitFactory() {
    RabbitConnectionFactoryConfig rabbitConfig = new RabbitConnectionFactoryConfig(10);
    return connectionFactory().rabbitConnectionFactory("bunnymq", rabbitConfig);
}

To set properties on a RabbitMQ service, you can use an overloaded variant of rabbitConnectionFactory(). The following example connects to the bunnymq RabbitMQ service and supplies configuration using a RabbitConnectionFactoryConfig, which is initialized with a HashMap of property keys and values.

//Connect to the 'bunnymq' RabbitMQ service, setting properties
@Bean
public RabbitConnectionFactory rabbitFactory() {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("requestedHeartbeat", 5);
    properties.put("connectionTimeout", 10);

    RabbitConnectionFactoryConfig rabbitConfig = new RabbitConnectionFactoryConfig(properties);
    return connectionFactory().rabbitConnectionFactory("bunnymq", rabbitConfig);
}

Relational database (DB2, MySQL, Oracle, PostgreSQL, SQL Server)

Note

Spring Cloud Spring Service Connector selects from a number of connection-pooling DataSource implementations based on availability and following a set priority. For details on the priority, see Detection and Prioritization of DataSource Implementations. If you need to reorder the prioritization of supported implementations, see Reordering Implementation Priority.

To connect to a unique relational database service, you can create a service bean using dataSource(). The following example connects to the only relational database service bound to the application.

//Connect to the only available relational database service
@Bean
public DataSource dataSource() {
    return connectionFactory().dataSource();
}

To connect to a specific relational database service, you can use an overloaded variant of dataSource(). The following example connects specifically to the my-own-personal-sql MySQL service.

//Connect to the 'my-own-personal-sql' relational database service
@Bean
public DataSource dataSource() {
    return connectionFactory().dataSource("my-own-personal-sql");
}

To provide configuration for a relational database service, you can use an overloaded dataSource() variant. The following example connects to the my-own-personal-sql MySQL service and supplies configuration using a DataSourceConfig, which is initialized with a PoolConfig that sets a minPoolSize of 5, a maxPoolSize of 30, and a maxWaitTime of 3000.

//Connect to the 'my-own-personal-sql' relational database service, supplying configuration
@Bean
public DataSource dataSource() {
    PoolConfig poolConfig = new PoolConfig(5, 30, 3000);
    DataSourceConfig dbConfig = new DataSourceConfig(poolConfig, null);
    return connectionFactory().dataSource("my-own-personal-sql", dbConfig);
}

To set properties on a relational database service, you can use an overloaded variant of dataSource(). The following example connects to the my-own-personal-sql MySQL service and supplies configuration using a DataSourceConfig. The DataSourceConfig is initialized with a PoolConfig (which sets a minPoolSize of 5, a maxPoolSize of 30, and a maxWaitTime of 3000) and a ConnectionConfig (which sets the useUnicode and characterEncoding properties).

//Connect to the 'my-own-personal-sql' relational database service, setting properties
@Bean
public DataSource dataSource() {
    PoolConfig poolConfig = new PoolConfig(5, 30, 3000);
    ConnectionConfig connConfig = new ConnectionConfig("useUnicode=yes;characterEncoding=UTF-8");
    DataSourceConfig dbConfig = new DataSourceConfig(poolConfig, connConfig);
    return connectionFactory().dataSource("my-own-personal-sql", dbConfig);
}
Detection and Prioritization of DataSource Implementations

When creating a DataSource, the connector prefers any of the following four connection-pooling DataSource implementations, in the order shown.

The connector uses the first of these that is found on the classpath. If none of these are on the classpath, the connector falls back to a SimpleDriverDataSource, which does not reuse connections.

Reordering Implementation Priority
Note

This feature is available only in Spring Cloud Connectors 1.2.1 and later.

If you would like to reorder the priorities given to supported DataSource implementations, you can provide a DataSourceConfig containing a List which uses your own ordering. You can name an implementation either by its full class name or by a string included in its class name.

The following example connects to the my-own-personal-sql MySQL service and supplies configuration using a DataSourceConfig, which is initialized with a List of DataSource implementation class names.

//Set order of DataSource implementations, using a List of full class names
@Bean
public DataSource dataSource() {
    List<String> dataSourceNames = Arrays.asList("TomcatJdbcPooledDataSourceCreator", "HikariCpPooledDataSourceCreator", "BasicDbcpPooledDataSourceCreator");
    DataSourceConfig dbConfig = new DataSourceConfig(dataSourceNames);
    return connectionFactory().dataSource("my-own-personal-sql", dbConfig);
}

The following example connects to the my-own-personal-sql MySQL service and supplies configuration using a DataSourceConfig, which is initialized with a List of strings contained in DataSource implementations’ class names.

//Set order of DataSource implementations, using a List of strings contained in class names
@Bean
public DataSource dataSource() {
    List<String> dataSourceNames = Arrays.asList("TomcatJdbc", "HikariCp", "BasicDbcp");
    DataSourceConfig dbConfig = new DataSourceConfig(dataSourceNames);
    return connectionFactory().dataSource("my-own-personal-sql", dbConfig);
}

MongoDB

To connect to a unique MongoDB service, you can create a service bean using mongoDbFactory(). The following example connects to the only MongoDB service bound to the application.

//Connect to the only available MongoDB service
@Bean
public MongoDbFactory mongoFactory() {
    return connectionFactory().mongoDbFactory();
}

To provide configuration for a unique MongoDB service, you can use an overloaded mongoDbFactory() variant. The following example connects to the only MongoDB service bound to the application and supplies configuration using a MongoDbFactoryConfig that sets writeConcern to NONE, connectionsPerHost to 50, and maxWaitTime to 200.

//Connect to the only available MongoDB service, supplying configuration
@Bean
public MongoDbFactory mongoFactory() {
    MongoDbFactoryConfig mongoConfig = new MongoDbFactoryConfig("NONE", 50, 200);
    return connectionFactory().mongoDbFactory(mongoConfig);
}

To connect to a specific MongoDB service, you can use an overloaded variant of mongoDbFactory(). The following example connects specifically to the mongo-service MongoDB service.

//Connect to the 'mongo-service' MongoDB service
@Bean
public MongoDbFactory mongoFactory() {
    return connectionFactory().mongoDbFactory("mongo-service");
}

To connect to a specific MongoDB service and provide configuration, you can use an overloaded mongoDbFactory() variant. The following example connects to the mongo-service MongoDB service and supplies configuration using a MongoDbFactoryConfig that sets writeConcern to NONE, connectionsPerHost to 50, and maxWaitTime to 200.

//Connect to the 'mongo-service' MongoDB service, supplying configuration
@Bean
public MongoDbFactory mongoFactory() {
    MongoDbFactoryConfig mongoConfig = new MongoDbFactoryConfig("NONE", 50, 200);
    return connectionFactory().mongoDbFactory("mongo-service", mongoConfig);
}

Redis

To connect to a unique Redis service, you can create a service bean using redisConnectionFactory(). The following example connects to the only Redis service bound to the application.

//Connect to the only available Redis service
@Bean
public RedisConnectionFactory redisFactory() {
    return connectionFactory().redisConnectionFactory();
}

To provide configuration for a unique Redis service, you can use an overloaded redisConnectionFactory() variant. The following example connects to the only Redis service bound to the application and supplies configuration using a PooledServiceConnectorConfig, which is initialized with a PoolConfig that sets a minPoolSize of 5, a maxPoolSize of 30, and a maxWaitTime of 3000.

//Connect to the only available Redis service, supplying configuration
@Bean
public RedisConnectionFactory redisFactory() {
    PoolConfig poolConfig = new PoolConfig(5, 30, 3000);
    PooledServiceConnectorConfig redisConfig = new PooledServiceConnectorConfig(poolConfig);
    return connectionFactory().redisConnectionFactory(redisConfig);
}

To connect to a specific Redis service, you can use an overloaded variant of redisConnectionFactory(). The following example connects specifically to the redis-service Redis service.

//Connect to the 'redis-service' Redis service
@Bean
public RedisConnectionFactory redisFactory() {
    return connectionFactory().redisConnectionFactory("redis-service");
}

To connect to a specific Redis service and provide configuration, you can use an overloaded redisConnectionFactory() variant. The following example connects to the redis-service Redis service and supplies configuration using a RedisConnectionFactoryConfig, which is initialized with a PoolConfig that sets writeConcern to NONE, connectionsPerHost to 50, and maxWaitTime to 200.

//Connect to the 'redis-service' Redis service, supplying configuration
@Bean
public RedisConnectionFactory redisFactory() {
  PoolConfig poolConfig = new PoolConfig(5, 30, 3000);
  PooledServiceConnectorConfig redisConfig = new RedisConnectionFactoryConfig(poolConfig);
  return connectionFactory().redisConnectionFactory("redis-service", redisConfig);
}

To connect to a specific Redis service and set properties on the service, you can use an overloaded variant of redisConnectionFactory(). The following example connects to the redis-service Redis service and sets the timeout property using a RedisConnectionFactoryConfig initialized with a HashMap that contains the property key and value.

//Connect to the 'redis-service' Redis service, setting a property
@Bean
public RedisConnectionFactory redisFactory() {
  Map<String, Object> properties = new HashMap<String, Object>();
  properties.put("timeout", 10);
  RedisConnectionFactoryConfig redisConfig = new RedisConnectionFactoryConfig(properties);
  return connectionFactory().redisConnectionFactory("redis-service", redisConfig);
}

To connect to a specific Redis service and provide configuration and property values for the service, you can use an overloaded variant of redisConnectionFactory(). The following example connects to the redis-service Redis service and uses a RedisConnectionFactoryConfig initialized with a PoolConfig (which sets writeConcern to NONE, connectionsPerHost to 50, and maxWaitTime to 200) and a HashMap (which contains a property key and value) to configure the service and set its timeout property.

//Connect to the 'redis-service' Redis service, providing configuration and setting a property
@Bean
public RedisConnectionFactory redisFactory() {
  Map<String, Object> properties = new HashMap<String, Object>();
  properties.put("timeout", 10);
  PoolConfig poolConfig = new PoolConfig(5, 30, 3000);
  RedisConnectionFactoryConfig redisConfig = new RedisConnectionFactoryConfig(poolConfig, properties);
  return connectionFactory().redisConnectionFactory("redis-service", redisConfig);
}

Cassandra

To connect to a unique Cassandra service, you can create a service bean using cluster(). The following example connects to the only Cassandra service bound to the application.

//Connect to the only available Cassandra service
@Bean
public Cluster cluster() {
    return connectionFactory().cluster();
}

To provide configuration for a unique Cassandra service, you can use an overloaded cluster() variant. The following example connects to the only Cassandra service bound to the application and supplies configuration using a CassandraClusterConfig with metrics and JMX reporting disabled.

//Connect to the only available Cassandra service, supplying configuration
@Bean
public Cluster cluster() {
    CassandraClusterConfig config = new CassandraClusterConfig();
    config.setMetricsEnabled(false);
    config.setJmxReportingEnabled(false);
    return connectionFactory().cluster(config);
}

To connect to a specific Cassandra service, you can use an overloaded variant of cluster(). The following example connects specifically to the cassandra-service Cassandra service.

//Connect to the 'cassandra-service' Cassandra service
@Bean
public Cluster cluster() {
    return connectionFactory().cluster("cassandra-service");
}

To connect to a specific Cassandra service and provide configuration, you can use an overloaded cluster() variant. The following example connects to the cassandra-service Cassandra service and supplies configuration using a CassandraClusterConfig with metrics and JMX reporting disabled.

//Connect to the 'cassandra-service' Cassandra service, supplying configuration
@Bean
public Cluster cluster() {
    CassandraClusterConfig config = new CassandraClusterConfig();
    config.setMetricsEnabled(false);
    config.setJmxReportingEnabled(false);
    return connectionFactory().cluster("cassandra-service", config);
}

SMTP

To connect to a unique SMTP service, you can create a service bean using service(), providing the service type MailSender. The following example connects to the only SMTP service bound to the application.

//Connect to the only available SMTP service
@Bean
public MailSender mailSender() {
    return connectionFactory().service(MailSender.class);
}

To connect to a specific SMTP service, you can use an overloaded variant of service(), providing the service type MailSender. The following example connects specifically to the mail-service SMTP service.

//Connect to the 'mail-service' SMTP service
@Bean
public MailSender mailSender() {
    return connectionFactory().service("mail-service", MailSender.class);
}

Connecting to Generic Services

The Java configuration supports access to generic services (services which don’t have a directly mapped method; this is typical for a newly-introduced service or when connecting to a private service in a private PaaS) through the service() method. It follows the same pattern as dataSource() etc., except that it allows you to supply the connector type as an additional parameter. The following example connects to a hypothetical service of type Search, called search-service.

@Bean
public Search search() {
  return connectionFactory().service("search-service", Search.class);
}

Scanning for Services

You can scan for each bound service using the @ServiceScan annotation. (This is conceptually similar to Spring’s @ComponentScan annotation.)

@Configuration
@ServiceScan
class CloudConfig {
}

In the above example, the configuration will create one bean of the appropriate type (such as a DataSource in the case of a relational database service). Each bean will have an id matching the corresponding service name.

You can inject such beans using autowiring.

@Autowired DataSource inventoryDb;

If the application is bound to more than one service of a given type, you can specify one by using the @Qualifier annotation and providing it with the name of the appropriate service.

@Autowired @Qualifier("inventory-db") DataSource inventoryDb;
@Autowired @Qualifier("shipping-db") DataSource shippingDb;

Accessing Service Properties

You can expose raw properties for all services and for the application through a bean.

class CloudPropertiesConfig extends AbstractCloudConfig {
    @Bean
    public Properties cloudProperties() {
        return properties();
    }
}

The <cloud> Namespace

Setting Up

The <cloud> namespace offers a simple way for a Spring application to connect to cloud services.

To use this namespace, add a declaration for it.

<?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:cloud="http://www.springframework.org/schema/cloud"
       xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/cloud https://www.springframework.org/schema/cloud/spring-cloud.xsd">

<!-- <cloud> namespace usage here -->

Creating Service Beans

A namespace element which creates a service bean conforms to the following pattern (in this example, the bean is for a relational database service).

<cloud:data-source id="inventory-db" service-name="inventory-db-service">
    <cloud:connection properties="sessionVariables=sql_mode='ANSI';characterEncoding=UTF-8"/>
    <cloud:pool pool-size="20" max-wait-time="200"/>
</cloud>

The above example creates a javax.sql.DataSource bean with the id inventory-db. The bean is bound to the inventory-db-service and is configured with the connection and pool properties specified in the nested <cloud:connection> and <cloud:pool> elements.

If no id attribute is specified, the id is set to the service name. If no service-name is specified, the bean is bound to the only service in the corresponding category (in this case, a relational database). If no unique service is found, a runtime exception will be thrown.

Other namespace elements which create service connectors include:

<cloud:mongo-db-factory/>
<cloud:redis-connection-factory/>
<cloud:rabbit-connection-factory/>

For information on using the <cloud> namespace to create connections to services with built-in support in the Spring Service Connector, see below.

RabbitMQ

To connect to a RabbitMQ service, you can use the <cloud:rabbit-connection-factory> element. The following example connects to the only RabbitMQ service bound to the application.

<!-- Connect to the only available RabbitMQ service -->
<cloud:rabbit-connection-factory />

To connect to a specific RabbitMQ service, you can use the service-name attribute. The following example connects specifically to the bunnymq RabbitMQ service.

<!-- Connect to the 'bunnymq' RabbitMQ service -->
<cloud:rabbit-connection-factory service-name="bunnymq" />

To specify an id for the RabbitMQ connection bean, you can use the id attribute. The following example connects specifically to the bunnymq RabbitMQ service with a bean given the id rabbitmq.

<!-- Connect to the 'bunnymq' RabbitMQ service with a bean of id 'rabbitmq' -->
<cloud:rabbit-connection-factory id="rabbitmq" service-name="bunnymq" />

To set properties on a RabbitMQ service, you can use the <cloud:rabbit-options> nested element. The following example connects specifically to the bunnymq RabbitMQ service with a bean given the id rabbitmq and uses the <cloud:rabbit-options> element to set the size of the channel cache to 200.

<!-- Connect to the 'bunnymq' RabbitMQ service with a bean of id 'rabbitmq', setting channel cache size -->
<cloud:rabbit-connection-factory id="rabbitmq" service-name="bunnymq">
  <cloud:rabbit-options channel-cache-size="200"/>
</cloud:rabbit-connection-factory>

To set connection properties on a RabbitMQ service, you can use the <cloud:connection-properties> nested element. The following example connects specifically to the bunnymq RabbitMQ service with a bean given the id rabbitmq. It uses the <cloud:rabbit-options> element to set the size of the channel cache to 200, and it uses the <cloud:connection-properties> element to set a heartbeat timeout of 5 seconds and a connection timeout of 10 milliseconds.

<!-- Connect to the 'bunnymq' RabbitMQ service with a bean of id 'rabbitmq', setting channel cache size and connection properties -->
<cloud:rabbit-connection-factory id="rabbitmq" service-name="bunnymq">
  <cloud:rabbit-options channel-cache-size="200"/>
  <cloud:connection-properties>
    <entry key="requestedHeartbeat" value="5"/>
    <entry key="connectionTimeout" value="10"/>
  </cloud:connection-properties>
</cloud:rabbit-connection-factory>

Relational database (DB2, MySQL, Oracle, PostgreSQL, SQL Server)

Note

Spring Cloud Spring Service Connector selects from a number of connection-pooling DataSource implementations based on availability and following a set priority. For details on the priority, see Detection and Prioritization of DataSource Implementations. If you need to reorder the prioritization of supported implementations, see Reordering Implementation Priority.

To connect to a relational database service, you can use the <cloud:data-source> element. The following example connects to the only relational database service bound to the application.

<!-- Connect to the only available relational database service -->
<cloud:data-source/>

To connect to a specific relational database service, you can use the service-name attribute. The following example connects specifically to the my-own-personal-sql MySQL service.

<!-- Connect to the 'my-own-personal-sql' relational database service -->
<cloud:data-source service-name="my-own-personal-sql"/>

To specify an id for the relational database connection bean, you can use the id attribute. The following example connects specifically to the my-own-personal-sql MySQL service with a bean given the id mysql.

<!-- Connect to the 'my-own-personal-sql' relational database service, with a bean of id 'mysql' -->
<cloud:data-source id="mysql" service-name="my-own-personal-sql" />

To set connection properties on a relational database service, you can use the <cloud:connection> nested element. The following example connects specifically to the my-own-personal-sql MySQL service with a bean given the id mysql and uses the <cloud:connection> element to set the useUnicode and characterEncoding properties.

<!-- Connect to the 'my-own-personal-sql' relational database service with a bean of id 'mysql', setting connection properties -->
<cloud:data-source id="mysql" service-name="my-own-personal-sql">
    <cloud:connection properties="useUnicode=yes;characterEncoding=UTF-8"/>
</cloud:data-source>

To configure pool settings on a relational database service, you can use the <cloud:pool> nested element. The following example connects specifically to the my-own-personal-sql MySQL service with a bean given the id mysql. It uses the <cloud:pool> element to set a pool-size of 5–30 and a max-wait-time of 3000 milliseconds.

<!-- Connect to the 'my-own-personal-sql' relational database service with a bean of id 'mysql', configuring pool settings -->
<cloud:data-source id="mysql" service-name="my-own-personal-sql">
    <cloud:pool pool-size="5-30" max-wait-time="3000"/>
</cloud:data-source>
Detection and Prioritization of DataSource Implementations

When creating a DataSource, the connector prefers any of the following four connection-pooling DataSource implementations, in the order shown.

The connector uses the first of these that is found on the classpath. If none of these are on the classpath, the connector falls back to a SimpleDriverDataSource, which does not reuse connections.

Reordering Implementation Priority
Note

This feature is available only in Spring Cloud Connectors 1.2.1 and later.

If you would like to reorder the priorities given to supported DataSource implementations, you can use the <cloud:pool-data-sources> nested element. For each implementation, include a <value> element for a string contained in the class name. List these in the order that you wish to have the connector follow.

The following example connects to the my-own-personal-sql MySQL service and uses the <cloud:pool-data-sources> element to specify the priority of supported DataSource implementations.

<!-- Set order of DataSource implementations, using strings contained in class names -->
<cloud:data-source id="mysql" service-name="my-own-personal-sql" />
  <cloud:pool-data-sources>
    <value>TomcatJdbc</value>
    <value>TomcatDbcp</value>
    <value>BasicDbcp</value>
  </cloud:pool-data-sources>
</cloud:data-source>

MongoDB

To connect to a MongoDB service, you can use the <cloud:mongo-db-factory/> element. The following example connects to the only MongoDB service bound to the application.

<!-- Connect to the only available MongoDB service -->
<cloud:mongo-db-factory/>

To connect to a specific MongoDB service, you can use the service-name attribute. The following example connects specifically to the mongo-service MongoDB service.

<!-- Connect to the 'mongo-service' MongoDB service -->
<cloud:mongo-db-factory service-name="mongo-service"/>

To specify an id for the MongoDB connection bean, you can use the id attribute. The following example connects specifically to the mongo-service MongoDB service with a bean given the id mongo.

<!-- Connect to the 'mongo-service' MongoDB service with a bean of id 'mongo' -->
<cloud:mongo-db-factory id="mongo" service-name="mongo-service"/>

To set properties on a MongoDB service, you can use the <cloud:mongo-options> nested element. The following example connects specifically to the mongo-service MongoDB service with a bean given the id mongo and uses the <cloud:mongo-options> element to allow 50 connections per host.

<!-- Connect to the 'mongo-service' MongoDB service with a bean of id 'mongo', setting connections per host -->
<cloud:mongo-db-factory id="mongo" service-name="mongo-service">
    <cloud:mongo-options connections-per-host="50"/>
</cloud:mongo-db-factory>

Redis

To connect to a Redis service, you can use the <cloud:redis-connection-factory/> element. The following example connects to the only Redis service bound to the application.

<!-- Connect to the only available Redis service -->
<cloud:redis-connection-factory/>

To connect to a specific Redis service, you can use the service-name attribute. The following example connects specifically to the redis-service Redis service.

<!-- Connect to the 'redis-service' Redis service -->
<cloud:redis-connection-factory service-name="redis-service"/>

To specify an id for the Redis connection bean, you can use the id attribute. The following example connects specifically to the redis-service Redis service with a bean given the id redis.

<!-- Connect to the 'redis-service' Redis service with a bean of id 'redis' -->
<cloud:redis-connection-factory id="redis" service-name="redis-service"/>

To set connection properties on a Redis service, you can use the <cloud:connection-properties> nested element. The following example connects specifically to the redis-service Redis service with a bean given the id redis and uses the <cloud:connection-properties> element to set a timeout of 10.

<!-- Connect to the 'redis-service' Redis service with a bean of id 'redis', setting a connection property -->
<cloud:redis-connection-factory id="redis" service-name="redis-service">
  <cloud:connection-properties>
    <entry key="timeout" value="10"/>
  </cloud:connection-properties>
</cloud:redis-connection-factory>

To configure pool settings on a Redis service, you can use the <cloud:pool> nested element. The following example connects specifically to the redis-service Redis service with a bean given the id redis. It uses the <cloud:pool> element to set a pool-size of 5–30 and a max-wait-time of 3000 milliseconds.

<!-- Connect to the 'redis-service' Redis service with a bean of id 'redis', configuring pool settings -->
<cloud:redis-connection-factory id="redis" service-name="redis-service">
  <cloud:pool pool-size="5-30" max-wait-time="3000"/>
</cloud:redis-connection-factory>

Cassandra

To connect to a Cassandra service, you can use the <cloud:cassandra-session-factory/> element. The following example connects to the only Cassandra service bound to the application.

<!-- Connect to the only available Cassandra service -->
<cloud:cassandra-session-factory/>

To connect to a specific Cassandra service, you can use the service-name attribute. The following example connects specifically to the cassandra-service Cassandra service.

<!-- Connect to the 'cassandra-service' Cassandra service -->
<cloud:cassandra-session-factory service-name="cassandra-service"/>

To specify an id for the Cassandra connection bean, you can use the id attribute. The following example connects specifically to the cassandra-service Cassandra service with a bean given the id cassandra.

<!-- Connect to the 'cassandra-service' Cassandra service with a bean of id 'cassandra' -->
<cloud:cassandra-session-factory id="cassandra" service-name="cassandra-service"/>

To set properties on a Cassandra service, you can use the <cloud:cassandra-options> nested element. The following example connects specifically to the cassandra-service Cassandra service with a bean given the id cassandra and uses the <cloud:cassandra-options> element.

<!-- Connect to the 'cassandra-service' Cassandra service with a bean of id 'cassandra', setting connections options -->
<bean id="retryPolicy" class="com.datastax.driver.core.policies.DefaultRetryPolicy"/>

<cloud:cassandra-session-factory id="cassandra" service-name="cassandra-service" >
  <cloud:cassandra-options compression="NONE" retry-policy="retryPolicy"/>
</cloud:cassandra-session-factory>

Connecting to Generic Services

Spring Service Connector also supports a generic <cloud:service> namespace for connecting to a service with no directly-mapped element (this is typical for a newly-introduced service or when connecting to a private service in a private PaaS). You must specify either the connector-type attribute (for locating a unique service by type) or the service-name attribute.

 <cloud:service id="email" service-name="email-service" connector-type="com.something.EmailConnectory" />

Scanning for Services

Besides these elements (which create only one bean per element), Spring Service Connector provides a <cloud:service-scan> element, in the same spirit as the <context:component-scan> element. It scans for all services bound to the application and creates a bean for each service. Each bean has an id matching the service name; this means that you can use the @Qualifier annotation along with @Autowired when there is more than one bean of the same type.

Accessing Service Properties

Spring Service Connector also provides a <cloud:properties> element, which exposes properties for the application and for services.

<cloud:properties id="cloudProperties"/>