Skip to content

Commit 74aaa48

Browse files
committed
Autowire datasource and add profile based configuration
1 parent ca73214 commit 74aaa48

5 files changed

Lines changed: 71 additions & 7 deletions

File tree

build.gradle

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ dependencies {
1616
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
1717
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.12'
1818

19+
// To use Spring Cloud Connectors for development
20+
compile 'org.springframework.cloud:spring-cloud-localconfig-connector'
21+
22+
// If you intend to deploy the app to Cloud Foundry
23+
compile 'org.springframework.cloud:spring-cloud-cloudfoundry-connector'
24+
compile 'org.springframework.cloud:spring-cloud-spring-service-connector'
25+
26+
compile "org.springframework.boot:spring-boot-configuration-processor"
27+
1928
testCompile("org.springframework.boot:spring-boot-starter-test")
2029
}
2130

@@ -40,4 +49,7 @@ flyway {
4049

4150
task testMigrate(type: org.flywaydb.gradle.task.FlywayMigrateTask) {
4251
url = testDbUrl
43-
}
52+
}
53+
54+
sourceCompatibility = 1.8
55+
targetCompatibility = 1.8

manifest.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ applications:
77
disk_quota: 256M
88
buildpacks:
99
- java_buildpack
10+
services:
11+
- tracker-database
1012
env:
11-
WELCOME_MESSAGE: Hello from the review environment
13+
MANAGEMENT_SECURITY_ENABLED: false
14+
WELCOME_MESSAGE: Hello from the review environment

src/main/java/io/pivotal/pal/tracker/PalTrackerApplication.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ public static void main(String[] args) {
2121
SpringApplication.run(PalTrackerApplication.class, args);
2222
}
2323

24+
@Autowired
25+
DataSource dataSource;
26+
2427
@Bean
25-
TimeEntryRepository timeEntryRepository(@Value("${SPRING_DATASOURCE_URL}") String dataSourceUrl) {
26-
DataSource dataSource = new MysqlDataSource();
27-
((MysqlDataSource) dataSource).setUrl(dataSourceUrl);
28+
TimeEntryRepository timeEntryRepository() {
2829
return new JdbcTimeEntryRepository(dataSource);
2930
}
3031

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.pivotal.pal.tracker;
2+
3+
import com.mysql.cj.jdbc.MysqlDataSource;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.cloud.config.java.*;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.Profile;
8+
9+
import javax.sql.DataSource;
10+
import java.util.Properties;
11+
12+
@ServiceScan
13+
public class PalTrackerConfig {
14+
@Configuration
15+
@Profile("cloud")
16+
static class CloudConfiguration extends AbstractCloudConfig {
17+
18+
@Bean
19+
public DataSource dataSource() {
20+
return connectionFactory().dataSource();
21+
}
22+
23+
@Bean
24+
public Properties cloudProperties() {
25+
return properties();
26+
}
27+
}
28+
29+
@Configuration
30+
@Profile("default")
31+
static class LocalConfiguration {
32+
@Bean
33+
public DataSource dataSource() {
34+
MysqlDataSource dataSource = new MysqlDataSource();
35+
dataSource.setUrl(System.getenv("SPRING_DATASOURCE_URL"));
36+
return dataSource;
37+
}
38+
@Bean
39+
public Properties cloudProperties() {
40+
return new Properties();
41+
}
42+
43+
}
44+
}

src/test/java/test/pivotal/pal/trackerapi/TimeEntryApiTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.springframework.jdbc.core.JdbcTemplate;
1818
import org.springframework.test.context.junit4.SpringRunner;
1919

20+
import javax.sql.DataSource;
2021
import java.time.LocalDate;
2122
import java.util.Collection;
2223
import java.util.TimeZone;
@@ -32,12 +33,15 @@ public class TimeEntryApiTest {
3233
@Autowired
3334
private TestRestTemplate restTemplate;
3435

36+
@Autowired
37+
DataSource dataSource;
38+
3539
private TimeEntry timeEntry = new TimeEntry(123L, 456L, LocalDate.parse("2017-01-08"), 8);
3640

3741
@Before
3842
public void setUp() throws Exception {
39-
MysqlDataSource dataSource = new MysqlDataSource();
40-
dataSource.setUrl(System.getenv("SPRING_DATASOURCE_URL"));
43+
//MysqlDataSource dataSource = new MysqlDataSource();
44+
//dataSource.setUrl(System.getenv("SPRING_DATASOURCE_URL"));
4145

4246
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
4347
jdbcTemplate.execute("TRUNCATE time_entries");

0 commit comments

Comments
 (0)