Skip to content

Commit b07a19a

Browse files
committed
Modification to run the example as a Spring Boot web application
1 parent 92cef2a commit b07a19a

File tree

5 files changed

+111
-35
lines changed

5 files changed

+111
-35
lines changed

pom.xml

+24-14
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,39 @@
3030

3131
<dependencies>
3232

33+
<!--<dependency>-->
34+
<!--<groupId>org.springframework.boot</groupId>-->
35+
<!--<artifactId>spring-boot-starter</artifactId>-->
36+
<!--</dependency>-->
37+
3338
<dependency>
3439
<groupId>org.springframework.boot</groupId>
35-
<artifactId>spring-boot-starter</artifactId>
40+
<artifactId>spring-boot-starter-web</artifactId>
3641
</dependency>
3742

3843
<!-- Spring data JPA, default tomcat pool, exclude it -->
3944
<dependency>
4045
<groupId>org.springframework.boot</groupId>
4146
<artifactId>spring-boot-starter-data-jpa</artifactId>
42-
<!--<exclusions>-->
43-
<!--<exclusion>-->
44-
<!--<groupId>org.apache.tomcat</groupId>-->
45-
<!--<artifactId>tomcat-jdbc</artifactId>-->
46-
<!--</exclusion>-->
47-
<!--</exclusions>-->
47+
<exclusions>
48+
<exclusion>
49+
<groupId>org.apache.tomcat</groupId>
50+
<artifactId>tomcat-jdbc</artifactId>
51+
</exclusion>
52+
</exclusions>
4853
</dependency>
4954

5055

5156
<!-- PostgreSQL JDBC driver -->
57+
<!--<dependency>-->
58+
<!--<groupId>postgresql</groupId>-->
59+
<!--<artifactId>postgresql</artifactId>-->
60+
<!--<version>9.1-901-1.jdbc4</version>-->
61+
<!--</dependency>-->
5262
<dependency>
53-
<groupId>postgresql</groupId>
63+
<groupId>org.postgresql</groupId>
5464
<artifactId>postgresql</artifactId>
55-
<version>9.1-901-1.jdbc4</version>
65+
<scope>runtime</scope>
5666
</dependency>
5767

5868
<!-- Oracle JDBC driver -->
@@ -64,11 +74,11 @@
6474

6575

6676
<!-- HikariCP connection pool -->
67-
<!--<dependency>-->
68-
<!--<groupId>com.zaxxer</groupId>-->
69-
<!--<artifactId>HikariCP</artifactId>-->
70-
<!--<version>2.6.0</version>-->
71-
<!--</dependency>-->
77+
<dependency>
78+
<groupId>com.zaxxer</groupId>
79+
<artifactId>HikariCP</artifactId>
80+
<version>2.6.0</version>
81+
</dependency>
7282

7383
<!-- Common DBCP2 connection pool -->
7484
<!--<dependency>-->
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,61 @@
11
package com.ucuenca.spring;
22

3-
import org.springframework.boot.*;
4-
import org.springframework.boot.autoconfigure.*;
3+
import com.ucuenca.spring.dao.CustomerRepository;
4+
import com.ucuenca.spring.model.Customer;
5+
import com.zaxxer.hikari.HikariDataSource;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.http.HttpStatus;
10+
import org.springframework.http.ResponseEntity;
511
import org.springframework.stereotype.*;
6-
//import org.springframework.web.bind.annotation.*;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.RequestMethod;
14+
import org.springframework.web.bind.annotation.RequestParam;
15+
import org.springframework.web.bind.annotation.ResponseBody;
16+
17+
import javax.sql.DataSource;
18+
import java.util.List;
719

820
/**
921
* Created by santteegt
1022
*/
1123
@Controller
12-
@EnableAutoConfiguration
24+
@RequestMapping(path = "/customer")
1325
public class SimpleController {
1426

15-
// @RequestMapping("/")
16-
// @ResponseBody
17-
// String home() {
18-
// return "Hello World!";
19-
// }
27+
@Autowired
28+
DataSource dataSource;
29+
30+
@Autowired
31+
private CustomerRepository customerRepository;
32+
33+
private static final Logger logger = LoggerFactory.getLogger(SimpleController.class);
34+
35+
@RequestMapping(path = "/create", method = RequestMethod.POST)
36+
public ResponseEntity<?> createCustomer(@RequestParam(required = true) String name,
37+
@RequestParam(required = true) String email) {
38+
customerRepository.addCustomer(name, email);
39+
return ResponseEntity.status(HttpStatus.CREATED).build();
40+
41+
}
2042

21-
public static void main(String[] args) throws Exception {
22-
SpringApplication.run(SimpleController.class, args);
43+
@RequestMapping(path = "/list", method = RequestMethod.GET)
44+
public ResponseEntity<List<Customer>> getCustomers() {
45+
return new ResponseEntity<>(customerRepository.findAll(), HttpStatus.OK);
2346
}
47+
48+
@RequestMapping("/")
49+
@ResponseBody
50+
String home() {
51+
logger.info("DATASOURCE = " + dataSource);
52+
53+
// If you want to check the HikariDataSource settings
54+
if(dataSource instanceof HikariDataSource) {
55+
HikariDataSource newds = (HikariDataSource)dataSource;
56+
logger.info("USING DATASOURCE WITH HIKARICP -> Max pool size = " + newds.getMaximumPoolSize());
57+
}
58+
return "Hello World!";
59+
}
60+
2461
}

src/main/java/com/ucuenca/spring/SpringBootConsoleApplication.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.ucuenca.spring.dao.CustomerRepository;
55
import com.ucuenca.spring.model.Customer;
66

7+
import com.zaxxer.hikari.HikariDataSource;
78
import org.springframework.beans.factory.annotation.Autowired;
89
import org.springframework.boot.CommandLineRunner;
910
import org.springframework.boot.SpringApplication;
@@ -14,7 +15,7 @@
1415

1516
import static java.lang.System.exit;
1617

17-
@SpringBootApplication
18+
//@SpringBootApplication
1819
public class SpringBootConsoleApplication implements CommandLineRunner {
1920

2021
@Autowired
@@ -29,8 +30,11 @@ public void run(String... args) throws Exception {
2930
System.out.println("DATASOURCE = " + dataSource);
3031

3132
// If you want to check the HikariDataSource settings
32-
//HikariDataSource newds = (HikariDataSource)dataSource;
33-
//System.out.println("DATASOURCE = " + newds.getMaximumPoolSize());
33+
if(dataSource instanceof HikariDataSource) {
34+
HikariDataSource newds = (HikariDataSource)dataSource;
35+
System.out.println("USING DATASOURCE WITH HIKARICP -> Max pool size = " + newds.getMaximumPoolSize());
36+
}
37+
3438

3539

3640
if (args.length <= 0) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.ucuenca.spring;
2+
3+
import com.zaxxer.hikari.HikariDataSource;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.SpringApplication;
8+
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
10+
import javax.annotation.PostConstruct;
11+
import javax.sql.DataSource;
12+
13+
/**
14+
* Created by santteegt on 13/03/2018.
15+
*/
16+
17+
@SpringBootApplication
18+
public class SpringBootServiceApplication {
19+
20+
public static void main(String[] args) throws Exception {
21+
SpringApplication.run(SpringBootServiceApplication.class, args);
22+
23+
}
24+
}

src/main/resources/application.properties

+8-7
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ spring.datasource.password=postgres
1010
spring.datasource.driver-class-name=org.postgresql.Driver
1111

1212
# HikariCP settings
13-
# spring.datasource.hikari.*
14-
#spring.datasource.hikari.connection-timeout=60000
15-
#spring.datasource.hikari.maximum-pool-size=5
13+
#spring.datasource.hikari.*
14+
spring.datasource.hikari.connection-timeout=60000
15+
spring.datasource.hikari.maximum-pool-size=5
1616

1717
# Tomcat settings
1818
# spring.datasource.tomcat.*
19-
spring.datasource.tomcat.maxWait=60000
20-
spring.datasource.tomcat.maxActive=10
21-
spring.datasource.tomcat.minIdle=5
19+
#spring.datasource.tomcat.maxWait=60000
20+
#spring.datasource.tomcat.maxActive=10
21+
#spring.datasource.tomcat.minIdle=5
2222

2323
# dbcp2 settings
2424
# spring.datasource.dbcp2.*
@@ -30,4 +30,5 @@ spring.datasource.tomcat.minIdle=5
3030
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
3131
logging.level.org.hibernate.SQL=debug
3232
#logging.level.org.hibernate.type.descriptor.sql=trace
33-
logging.level.=error
33+
logging.level.org.springframework=debug
34+
logging.level.com.ucuenca.spring=info

0 commit comments

Comments
 (0)