Skip to content

Commit 15a4a23

Browse files
AbhinabKanrarmaibin
authored andcommitted
adding factory instance example (eugenp#1552)
* rest with spark java * 4 * Update Application.java * indentation changes * spring @RequestMapping shortcuts * removing spring requestmapping and pushing spring-mvc-java * Joining/Splitting Strings with Java and Stream API * adding more join/split functionality * changing package name * testcase change * adding webutils * adding testcase for WebUtils and ServletRequestUtils * adding testcase * spring-security-stormpath * adding ratpack module * adding pom.xml * adding following modules with updated testcase : DB, Filter, Json * adding spring-boot custom banner tutorial * changing banner format in plain text * Delete banner.txt~ * Delete b.txt~ * CORS in JAX-RS * ratpack with google guice * adding factory instance example
1 parent 87be4ed commit 15a4a23

3 files changed

Lines changed: 25 additions & 8 deletions

File tree

ratpack/src/main/java/com/baeldung/guice/Application.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.baeldung.guice.config.DependencyModule;
44
import com.baeldung.guice.service.DataPumpService;
5+
import com.baeldung.guice.service.ServiceFactory;
56
import com.baeldung.guice.service.impl.DataPumpServiceImpl;
67

78
import ratpack.guice.Guice;
@@ -15,17 +16,17 @@ public static void main(String[] args) throws Exception {
1516
.start(server -> server.registry(Guice.registry(bindings -> bindings.module(DependencyModule.class)))
1617
.handlers(chain -> chain.get("randomString", ctx -> {
1718
DataPumpService dataPumpService = ctx.get(DataPumpService.class);
18-
ctx.render(dataPumpService.generate().length());
19-
})));
19+
ctx.render(dataPumpService.generate());
20+
}).get("factory", ctx -> ctx.render(ServiceFactory.getInstance().generate()))));
2021

2122
// RatpackServer.start(server -> server
2223
// .registry(Guice
2324
// .registry(bindings -> bindings.bindInstance(DataPumpService.class, new DataPumpServiceImpl())))
2425
// .handlers(chain -> chain.get("randomString", ctx -> {
2526
// DataPumpService dataPumpService = ctx.get(DataPumpService.class);
2627
// ctx.render(dataPumpService.generate());
27-
// })));
28+
// }).get("factory", ctx -> ctx.render(ServiceFactory.getInstance().generate()))));
2829

2930
}
3031

31-
}
32+
}

ratpack/src/main/java/com/baeldung/guice/service/DataPumpService.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package com.baeldung.guice.service;
22

3-
import com.baeldung.guice.service.impl.DataPumpServiceImpl;
4-
import com.google.inject.ImplementedBy;
5-
6-
@ImplementedBy(DataPumpServiceImpl.class)
73
public interface DataPumpService {
84

95
String generate();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.guice.service;
2+
3+
import com.baeldung.guice.service.impl.DataPumpServiceImpl;
4+
5+
public class ServiceFactory {
6+
7+
private static DataPumpService instance;
8+
9+
public static void setInstance(DataPumpService dataPumpService) {
10+
instance = dataPumpService;
11+
}
12+
13+
public static DataPumpService getInstance() {
14+
if (instance == null) {
15+
return new DataPumpServiceImpl();
16+
}
17+
return instance;
18+
}
19+
20+
}

0 commit comments

Comments
 (0)