Search Tutorials

Pivotal Cloud Foundry Tutorial - Deploying Spring Boot + MySQL Application to PCF | JavaInUse

Pivotal Cloud Foundry Tutorial - Deploying Spring Boot + MySQL Application to PCF

In previous tutorial we had deployed a Spring Boot Application to PCF.
In this tutorial we will develop a Spring Boot + MySQL Application and deploy it to PCF. We will be creating a Spring Boot + MySQL Application using JDBC and testing it locally. We have already seen Spring Boot MYSQL JDBC basics in a previous tutorial. For deploying on PCF, we dont need to install MySQL on the PCF cloud. On PCF there is a concept of services where on demand resources like MySQL and RabbitMQ can ordered and directly bound to the application. We will see how this is done.
Spring Boot SQL PCF Push

Pivotal Cloud Foundry - Table Of Contents

Pivotal Cloud Foundry Tutorial - Quick Introduction Pivotal Cloud Foundry Tutorial - Setting up the Development Environment Pivotal Cloud Foundry Tutorial - Deploy Spring Boot Application Hello World Example Pivotal Cloud Foundry Tutorial - Understanding PCF Deployment Architecture Pivotal Cloud Foundry Tutorial - Deploying Spring Boot + MySQL Application to PCF Pivotal Cloud Foundry Tutorial - Deploying Spring Boot + RabbitMQ Application to PCF


This tutorial is explained in the below Youtube Video.

Lets Begin-

Creating and testing Spring Boot + MySQL application locally

The project will be as follows-
Spring Boot JDBC Maven PCF
Add the spring-jdbc-starter dependency.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""


	<description>Demo project for Spring Boot</description>

		<relativePath /> <!-- lookup parent from repository -->






In the file specify the datasource properties
Create the schema-mysql.sql file and specify the initialization scripts-

CREATE TABLE employee (
  empName VARCHAR(100) NOT NULL

INSERT INTO employee(empId,empName)values("emp001","emp1");
INSERT INTO employee(empId,empName)values("emp002","emp2");
INSERT INTO employee(empId,empName)values("emp003","emp3");

Create the Employee Domain class
package com.javainuse.model;

public class Employee {

	private String empId;
	private String empName;

	public String getEmpId() {
		return empId;

	public void setEmpId(String empId) {
		this.empId = empId;

	public String getEmpName() {
		return empName;

	public void setEmpName(String empName) {
		this.empName = empName;

	public String toString() {
		return "Employee [empId=" + empId + ", empName=" + empName + "]";


Create the DAO interface.
package com.javainuse.dao;

import java.util.List;

import com.javainuse.model.Employee;

public interface EmployeeDao {
	List<Employee> getAllEmployees();
The DAO implementation class.
package com.javainuse.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.javainuse.dao.EmployeeDao;
import com.javainuse.model.Employee;

public class EmployeeDaoImpl extends JdbcDaoSupport implements EmployeeDao {

	DataSource dataSource;

	private void initialize() {

	public List<Employee> getAllEmployees() {
		String sql = "SELECT * FROM employee";
		List<Map<String, Object>> rows = getJdbcTemplate().queryForList(sql);

		List<Employee> result = new ArrayList<Employee>();
		for (Map<String, Object> row : rows) {
			Employee emp = new Employee();
			emp.setEmpId((String) row.get("empId"));
			emp.setEmpName((String) row.get("empName"));

		return result;
Create Service interface to specify employee operations to be performed.
package com.javainuse.service;

import java.util.List;

import com.javainuse.model.Employee;

public interface EmployeeService {
	List<Employee> getAllEmployees();

The Service class implementation.
package com.javainuse.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.javainuse.dao.EmployeeDao;
import com.javainuse.model.Employee;
import com.javainuse.service.EmployeeService;

public class EmployeeServiceImpl implements EmployeeService {

	EmployeeDao employeeDao;

	public List<Employee> getAllEmployees() {
		List<Employee> employees = employeeDao.getAllEmployees();
		return employees;


Create the Controller class to expose a GET API which fetches the employee records
package com.javainuse.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.javainuse.model.Employee;
import com.javainuse.service.EmployeeService;

public class EmployeeController {

	EmployeeService empService;

	@RequestMapping(value = "/employees", method = RequestMethod.GET)
	public List<Employee> firstPage() {

		return empService.getAllEmployees();


Finally create the class with @SpringBootApplication annotation.
package com.javainuse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringBootJdbcApplication {

	public static void main(String[] args) {, args);
Start the application. And got to localhost:8080/employees

Spring Boot PCF JDBCTemplate Example

PCF Deployment-

In Cloud Foundry, services are on demand resources that users can provision and use for their deployed application.
Examples of resources services provide include databases on a shared or dedicated server, or accounts on a SaaS application. These resources are known as service instances and the systems that deliver and operate these resources are known as Services. Think of a service as a factory that delivers service instances.
Services can either be
  • User Defined Service
  • MarketPlace Service
For this example we will be needing the MySQL MarketPlace service.
PCF market services

Provisioning MySql Service using PCF Web Console

  • Login to Pivotal Cloud Foundry with your credentials
    Pivotal Web Services Home
  • Go to Services tab for our development space
    Cloud Foundry Services
  • Click on Add New Service and type MySQL.
    Cloud Foundry MySQL Service
  • Select ClearDB MySql
    Cloud Foundry MySQL ClearDB
  • Select the free plan free Spark DB.
    Cloud Foundry MySQL ClearDB Free

Creating Manifest Configuration file to Bind our Maven Project to PCF SQL Service

Previously when deploying the application to PCF using the cf push command, we had given the required configuration like location if the jar file as command line arguments.
     cf push test-environment -p target\employee-producer-0.0.1-SNAPSHOT.jar
There are other configurations like memory, application name,PCF services to be bound which can be specified as command line arguments.
A much better way to specify this is as a configuration file named manifest.yml. Manifest is the blueprint of the application to be deployed in PCF. Manifests provide consistency and reproducibility, and can help us automate deploying apps.
PCF manifest file
In our eclipse jdbc project create a manifest configuration file as follows-
name: SpringBootMySql
path: target\boot-jdbc-0.0.1-SNAPSHOT.jar
memory: 1G

Push the application to PCF

  • Open the command terminal and use the following command
         cf login

    Cloud Foundry Login
  • It will ask for Cloud Foundry API. Enter The following API value-

    Cloud Foundry Login API
  • Next it will ask you for the Cloud Foundry credentials
    Cloud Foundry Credentials
  • Use the command cf push-
         cf push

    Spring Boot MySQL Push
The application is deployed on PCF. Go the PCF Web Console. Our new application will be up and running.
Spring Boot MySQL Running App
Select the application and go to the specified route. Append /employees to the route url. Our application is up and running.
Spring Boot MySQL Output

Download Source Code

Download it -
PCF- Spring Boot+MySQL