1. Overview

In this article, we are going to learn a little about cloud foundry and we will also provide an example to deploy spring boot application in cloud foundry. We will see how spring boot will do auto-configuration for the MySQL database and the installation of the required tools for cloud foundry deployments.
Cloud Foundry is an open-source platform as service (Paas) which is backed by Google, IBM, Microsoft, pivotal, SAP, and other companies. Cloud Foundry makes it faster and easier to build, test, deploy and scale applications and it is providing the choice of clouds, developer frameworks, and application services.
In the cloud foundry account, Organizations can be created. Each Organization may again have spaces like dev, staging, production. Each space has services like database and application services. In cloud foundry, the marketplace is available for provisioning new services.

Prerequisites to deploy any spring boot application in cloud foundry are,

1.CF command-line interface (CLI) in your system
2.Cloud foundry account (Example: Pivotal or SAP cloud foundry…)
In this article, we will use the Pivotal cloud foundry trail account to deploy and test the Spring boot application with MySQL database connection.

2 Cloud Foundry CLI set up and Pivotal cloud foundry trail account creation

2.1 Cloud Foundry CLI installation steps

1. Download the CF CLI zip file.
Link for download: https://pivotal.io/platform/pcf-tutorials/getting-started-with-pivotal-cloud-foundry/install-the-cf-cli
2. Unzip the downloaded file and click on the cf_installer file and it will guide our next steps to finish the installation.
3. To test, CF CLI installed or not. just open the command prompt or terminal and run cf helpcommand.it will list down all cloud foundry related commands.

2.2 Pivotal cloud account creation steps

1. Go to the pivotal cloud foundry signup page.
2. After completing a successful pivotal account creation, just sign in, it will show different services.
3. Enter the organization name and finish. By default, it will create one space with name development.

3.MySQL database service creation in the pivotal cloud:

Cloud Foundry is providing different types of services and we can buy any service in the marketplace.
Below are steps to create a MySQL database service free plan

1. In in your Organization, go to the marketplace and search for MySQL
2. select Clear DB MySQL Database
3. choose the spark DB plan which is free.it looks as below image


Deploying spring boot application with MySQL database in cloud foundry

4.Project Structure

We have created a sample spring boot application to deploy in cloud foundry. Have look at the following image

Deploying spring boot application with MySQL database in cloud foundry


application.properties file:
These properties are configured to test rest API in the local system, not for a cloud system.

#Data source
#Hibernate properties

The important dependencies in pom.xmlare,
spring-boot-starter-web: Responsible for web application creation
spring-boot-starter-data-jpa: for database configuration
mysql-connector-java: for MySQL connection

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <relativePath /> <!-- lookup parent from repository -->
    <description>Demo project for Spring Boot</description>


package com.javadevloperzone.services;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
public class User {
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    private String name;
    private String email;
    public Integer getId() {
        return id;
    public void setId(Integer id) {
        this.id = id;
    public String getName() {
        return name;
    public void setName(String name) {
        this.name = name;
    public String getEmail() {
        return email;
    public void setEmail(String email) {
        this.email = email;


package com.javadevloperzone.services;
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository<User, Integer> {


package com.javadevloperzone.services;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
public class UserController {
    private UserRepository userRepository;
    public List<User> getAllUsers() {
        return (List<User>) userRepository.findAll();
    public User createUser(@RequestBody User user) {
        return userRepository.save(user);

5. Creating and using manifest.yml file.

Cloud Foundry uses manifest.ymlfile to identify application properties like memory limit, application name, service bindings, and routes and more
Create manifest.ymlfile in the root directory of your app.

- name: SpringBootMysqlApp
  memory: 1GB
  path: target/SpringBootMysqlApp.jar 
  - java_buildpack
  - MySqlDB

6.Deploying spring boot application in cloud foundry

1.open command prompt/terminal and change directory to project root directory,
Example: D:\Pivotel\SpringBootMysqlCF
Command: cd D:\Pivotel\SpringBootMysqlCF

2. Every cloud foundry will have an API endpoint. for pivotal cloud foundry
API endpoint: https://api.run.pivotal.io
Connect to API endpoint by entering following command
cf login -a https://api.run.pivotal.io

3. By providing credentials, it will be logged into your pivotal cloud foundry
4. Enter command cf push. it will read the manifest file and pick jar file from the path and deploy it to cloud foundry. By using the build pack, it will download the required dependencies for running an application.

5. Login into cloud foundry account and open development space and copy route URL.
By route URL, we can test our rest API
Route URL: https://springbootmysqlapp.cfapps.io
REST API endpoint: /users

Final URL: https://springbootmysqlapp.cfapps.io/users

we did not write code for MySQL database configuration. But still, it is working. In the next phase, we are going to discuss how spring boot did auto reconfiguration for the MySQL database.

7. How spring-boot is bind to MySQL service and configured with our zero lines of code?

Properties in application.properties are responsible for connecting MySQL local database, not cloud MySQL database.
Cloud foundry will do auto-reconfiguration for the MySQL database. Only when following are true for your application
1. Only one service instance of a given service type is bound to the application.
Example: if we bind MySQL and PostgreSQL to the application, then Auto-Reconfiguration does not work due to two databases are bind to application
2. Only one matching type bean is in the Spring application context. For example, you can have only one bean type of javax.sql.Datasource

If you are eager to know about manual configuration, how to connect to different databases and how pivotal cloud foundry will do auto-reconfiguration. just go through following URL
Pivotal cloud foundry doc.


We have explained the deployment process of spring boot with MySQL database in cloud foundry clearly with images.
Hope 😊 you like it. if you want more information you can through the following links,

About Buildpacks: https://docs.cloudfoundry.org/buildpacks/
manifest.yml file: https://docs.cloudfoundry.org/devguide/deploy-apps/manifest.html
Auto reconfiguration: https://docs.run.pivotal.io/buildpacks/java/configuring-service-connections/spring-service-bindings.html
Cloud Foundry CLI: https://docs.cloudfoundry.org/cf-cli/

9.Source Code

A complete example code is available on Spring Boot Examples Git repository.



Was this post helpful?

2 comments. Leave new

Leave a Reply

Your email address will not be published. Required fields are marked *