Skip to content

Commit ad3cc63

Browse files
committed
Starter: Guice starter
1 parent b7a5cb9 commit ad3cc63

11 files changed

Lines changed: 315 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
db = mem
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration scan="true" scanPeriod="15 seconds" debug="false">
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>[%d{ISO8601}]-[%thread] %-5level %logger - %msg%n</pattern>
6+
</encoder>
7+
</appender>
8+
9+
<root level="INFO">
10+
<appender-ref ref="STDOUT" />
11+
</root>
12+
</configuration>

‎starters/guice-starter/pom.xml‎

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
5+
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<artifactId>guice-starter</artifactId>
9+
<groupId>starter</groupId>
10+
<version>1.0.0</version>
11+
12+
<properties>
13+
<!-- Startup class -->
14+
<application.class>starter.App</application.class>
15+
16+
<jooby.version>2.4.0</jooby.version>
17+
18+
<maven.compiler.source>1.8</maven.compiler.source>
19+
<maven.compiler.target>1.8</maven.compiler.target>
20+
<maven.compiler.parameters>true</maven.compiler.parameters>
21+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>io.jooby</groupId>
27+
<artifactId>jooby-netty</artifactId>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>io.jooby</groupId>
32+
<artifactId>jooby-jackson</artifactId>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>io.jooby</groupId>
37+
<artifactId>jooby-guice</artifactId>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>io.jooby</groupId>
42+
<artifactId>jooby-hikari</artifactId>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>io.jooby</groupId>
47+
<artifactId>jooby-jdbi</artifactId>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>ch.qos.logback</groupId>
52+
<artifactId>logback-classic</artifactId>
53+
</dependency>
54+
55+
<dependency>
56+
<groupId>com.h2database</groupId>
57+
<artifactId>h2</artifactId>
58+
</dependency>
59+
60+
<!-- Tests -->
61+
<dependency>
62+
<groupId>org.junit.jupiter</groupId>
63+
<artifactId>junit-jupiter-api</artifactId>
64+
<scope>test</scope>
65+
</dependency>
66+
67+
<dependency>
68+
<groupId>org.junit.jupiter</groupId>
69+
<artifactId>junit-jupiter-engine</artifactId>
70+
<scope>test</scope>
71+
</dependency>
72+
73+
<dependency>
74+
<groupId>io.jooby</groupId>
75+
<artifactId>jooby-test</artifactId>
76+
<scope>test</scope>
77+
</dependency>
78+
79+
<dependency>
80+
<groupId>com.squareup.okhttp3</groupId>
81+
<artifactId>okhttp</artifactId>
82+
<scope>test</scope>
83+
</dependency>
84+
85+
</dependencies>
86+
87+
<build>
88+
<plugins>
89+
<plugin>
90+
<artifactId>maven-compiler-plugin</artifactId>
91+
<version>3.6.2</version>
92+
</plugin>
93+
<plugin>
94+
<artifactId>maven-surefire-plugin</artifactId>
95+
<version>2.22.2</version>
96+
</plugin>
97+
<!-- jooby:run -->
98+
<plugin>
99+
<groupId>io.jooby</groupId>
100+
<artifactId>jooby-maven-plugin</artifactId>
101+
<version>${jooby.version}</version>
102+
</plugin>
103+
<!-- Build uber jar -->
104+
<plugin>
105+
<artifactId>maven-shade-plugin</artifactId>
106+
<version>3.2.1</version>
107+
<executions>
108+
<execution>
109+
<id>uber-jar</id>
110+
<phase>package</phase>
111+
<goals>
112+
<goal>shade</goal>
113+
</goals>
114+
<configuration>
115+
<createDependencyReducedPom>false</createDependencyReducedPom>
116+
<transformers>
117+
<transformer
118+
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
119+
<transformer
120+
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
121+
<mainClass>${application.class}</mainClass>
122+
</transformer>
123+
</transformers>
124+
</configuration>
125+
</execution>
126+
</executions>
127+
</plugin>
128+
</plugins>
129+
</build>
130+
131+
<dependencyManagement>
132+
<dependencies>
133+
<dependency>
134+
<groupId>io.jooby</groupId>
135+
<artifactId>jooby-bom</artifactId>
136+
<version>${jooby.version}</version>
137+
<type>pom</type>
138+
<scope>import</scope>
139+
</dependency>
140+
</dependencies>
141+
</dependencyManagement>
142+
</project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package starter;
2+
3+
import io.jooby.Jooby;
4+
import io.jooby.di.GuiceModule;
5+
import io.jooby.hikari.HikariModule;
6+
import io.jooby.jdbi.JdbiModule;
7+
import io.jooby.json.JacksonModule;
8+
import org.jdbi.v3.core.Jdbi;
9+
import starter.billing.BillingModule;
10+
import starter.billing.BillingService;
11+
import starter.billing.PizzaOrder;
12+
13+
public class App extends Jooby {
14+
{
15+
install(new JacksonModule());
16+
17+
install(new HikariModule());
18+
19+
install(new JdbiModule());
20+
21+
install(new GuiceModule(new BillingModule()));
22+
23+
onStarted(() -> {
24+
require(Jdbi.class).useHandle(handle -> {
25+
handle.createUpdate(
26+
"create table pizza (id bigint auto_increment, type varchar(255), count int, credit_card varchar(255))")
27+
.execute();
28+
});
29+
});
30+
31+
get("/order", ctx -> {
32+
BillingService billingService = require(BillingService.class);
33+
PizzaOrder order = ctx.query(PizzaOrder.class);
34+
return billingService.chargeOrder(order);
35+
});
36+
}
37+
38+
public static void main(String[] args) {
39+
runApp(args, App::new);
40+
}
41+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package starter.billing;
2+
3+
import com.google.inject.AbstractModule;
4+
5+
public class BillingModule extends AbstractModule {
6+
@Override
7+
protected void configure() {
8+
/*
9+
* This tells Guice that whenever it sees a dependency on a TransactionLog,
10+
* it should satisfy the dependency using a DatabaseTransactionLog.
11+
*/
12+
bind(TransactionLog.class).to(DatabaseTransactionLog.class);
13+
}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package starter.billing;
2+
3+
import javax.inject.Inject;
4+
5+
public class BillingService {
6+
private final TransactionLog transactionLog;
7+
8+
@Inject BillingService(TransactionLog transactionLog) {
9+
this.transactionLog = transactionLog;
10+
}
11+
12+
public Receipt chargeOrder(PizzaOrder order) {
13+
return new Receipt(transactionLog.save(order));
14+
}
15+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package starter.billing;
2+
3+
import org.jdbi.v3.core.Jdbi;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import javax.inject.Inject;
8+
9+
public class DatabaseTransactionLog implements TransactionLog {
10+
private Jdbi jdbi;
11+
private Logger log = LoggerFactory.getLogger(getClass());
12+
13+
/**
14+
* Jdbi instance is provisioned by Guice using the Jooby module architecture.
15+
*
16+
* @param jdbi
17+
*/
18+
@Inject
19+
public DatabaseTransactionLog(Jdbi jdbi) {
20+
this.jdbi = jdbi;
21+
}
22+
23+
@Override public long save(PizzaOrder order) {
24+
log.info("saving order: {}", order);
25+
26+
return jdbi.withHandle(h -> {
27+
return h.createUpdate("insert into pizza (type, count, credit_card) "
28+
+ "values (:type, :count, :creditCard)")
29+
.bind("type", order.getType())
30+
.bind("count", order.getCount())
31+
.bind("creditCard", order.getCreditCard())
32+
.executeAndReturnGeneratedKeys()
33+
.mapTo(Long.class)
34+
.one();
35+
});
36+
}
37+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package starter.billing;
2+
3+
public class PizzaOrder {
4+
private String type;
5+
6+
private int count;
7+
8+
private String creditCard;
9+
10+
public PizzaOrder(String type, int count, String creditCard) {
11+
this.type = type;
12+
this.count = count;
13+
this.creditCard = creditCard;
14+
}
15+
16+
public String getType() {
17+
return type;
18+
}
19+
20+
public int getCount() {
21+
return count;
22+
}
23+
24+
public String getCreditCard() {
25+
return creditCard;
26+
}
27+
28+
@Override public String toString() {
29+
return "PizzaOrder{" +
30+
"type='" + type + '\'' +
31+
", count=" + count +
32+
'}';
33+
}
34+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package starter.billing;
2+
3+
public class Receipt {
4+
private long number;
5+
6+
public Receipt(long number) {
7+
this.number = number;
8+
}
9+
10+
public long getNumber() {
11+
return number;
12+
}
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package starter.billing;
2+
3+
public interface TransactionLog {
4+
long save(PizzaOrder order);
5+
}

0 commit comments

Comments
 (0)