You can use the cloud-connectors as well and get the DataSource.
###pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-spring-service-connector</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-cloudfoundry-connector</artifactId>
</dependency>
package com.apress.spring.config;
import javax.sql.DataSource;
import org.springframework.cloud.config.java.AbstractCloudConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
@Profile("cloud")
public class CloudDataSourceConfig extends AbstractCloudConfig {
@Bean
public DataSource dataSource() {
return connectionFactory().dataSource();
}
}
VCAP_APPLICATION
{
"application_name": "journal",
"application_uris": [""],
"application_version": "1",
"host": "localhost",
"port": 8080,
"instance_id": "abcd",
"instance_index": 0,
"name": "journal",
"version": "1.0"
}
VCAP_SERVICES
{
"p-mysql": [
{
"credentials": {
"uri": "jdbc:mysql://localhost/journal?user=spring&password=springboot"
},
"label": "MySQL Service",
"name": "p-mysql",
"plan": "100mb-dev",
"tags": [
"mysql"
]
}
]
}