Spring

The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform. Platform.sh is flexible, and allows you to use Spring Framework in several flavors such as Spring MVC and Spring Boot.

Services

The configuration reader library for Java is used in these examples, so be sure to check out the documentation for installation instructions and the latest version.

MongoDB

You can use Spring Data MongoDB to use MongoDB with your application by first determining the MongoDB client programmatically.

import com.mongodb.MongoClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import sh.platform.config.Config;
import sh.platform.config.MongoDB;

@Configuration
public class MongoConfig extends AbstractMongoConfiguration {

    private Config config = new Config();

    @Override
    @Bean
    public MongoClient mongoClient() {
        MongoDB mongoDB = config.getCredential("database", MongoDB::new);
        return mongoDB.get();
    }

    @Override
    protected String getDatabaseName() {
        return config.getCredential("database", MongoDB::new).getDatabase();
    }
}

Apache Solr

You can use Spring Data Solr to use Solr with your application by first determining the Solr client programmatically.

import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.solr.core.SolrTemplate;
import sh.platform.config.Config;
import sh.platform.config.Solr;

@Configuration
public class SolrConfig {

    @Bean
    public HttpSolrClient elasticsearchTemplate() {
        Config config = new Config();
        final Solr credential = config.getCredential("solr", Solr::new);
        final HttpSolrClient httpSolrClient = credential.get();
        String url = httpSolrClient.getBaseURL();
        httpSolrClient.setBaseURL(url.substring(0, url.lastIndexOf('/')));
        return httpSolrClient;
    }

    @Bean
    public SolrTemplate solrTemplate(HttpSolrClient client) {
        return new SolrTemplate(client);
    }
}

Redis

You can use Spring Data Redis to use Redis with your application by first determining the Redis client programmatically.

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;

@Configuration
public class RedisConfig {


    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        Config config = new Config();
        RedisSpring redis = config.getCredential("redis", RedisSpring::new);
        return redis.get();
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        final RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(jedisConnectionFactory());
        template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
        return template;
    }

}

MySQL

MySQL is an open-source relational database technology. Spring has robust integration with this technology: Spring Data JPA.

The first step is to choose the database that you would like to use in your project. Define the driver for MySQL and the Java dependencies, then determine the DataSource client programmatically:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import sh.platform.config.Config;
import sh.platform.config.MySQL;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

    @Bean(name = "dataSource")
    public DataSource getDataSource() {
        Config config = new Config();
        MySQL database = config.getCredential("database", MySQL::new);
        return database.get();
    }
}

Note: You can use the same MySQL driver for MariaDB as well if you wish to do so.

MariaDB

MariaDB is an open-source relational database technology. Spring has robust integration with this technology: Spring Data JPA.

The first step is to choose the database that you would like to use in your project. Define the driver for MariaDB and the Java dependencies, then determine the DataSource client programmatically:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import sh.platform.config.Config;
import sh.platform.config.MariaDB;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

    @Bean(name = "dataSource")
    public DataSource getDataSource() {
        Config config = new Config();
        MariaDB database = config.getCredential("database", MariaDB::new);
        return database.get();
    }
}

PostgreSQL

PostgreSQL is an open-source relational database technology. Spring has robust integration with this technology: Spring Data JPA.

The first step is to choose the database that you would like to use in your project. Define the driver for PostgreSQL and the Java dependencies, then determine the DataSource client programmatically:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import sh.platform.config.Config;
import sh.platform.config.PostgreSQL;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

    @Bean(name = "dataSource")
    public DataSource getDataSource() {
        Config config = new Config();
        PostgreSQL database = config.getCredential("database", PostgreSQL::new);
        return database.get();
    }
}

RabbitMQ

You can use Spring JMS to use RabbitMQ with your application by first determining the RabbitMQ client programmatically.

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.JmsListenerContainerFactory;

@Configuration
@EnableJms
public class JMSConfig {

    @Bean
    public JmsListenerContainerFactory jmsListenerContainerFactory() {
        Config config = new Config();
        final RabbitMQSpring rabbitMQ = config.getCredential("rabbitmq", RabbitMQSpring::new);
        final JmsListenerContainerFactory factory = rabbitMQ.get();
        return factory;
    }
}

Templates