adding factory instance example (#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
This commit is contained in:
parent
87be4ede27
commit
15a4a23099
@ -2,6 +2,7 @@ package com.baeldung.guice;
|
|||||||
|
|
||||||
import com.baeldung.guice.config.DependencyModule;
|
import com.baeldung.guice.config.DependencyModule;
|
||||||
import com.baeldung.guice.service.DataPumpService;
|
import com.baeldung.guice.service.DataPumpService;
|
||||||
|
import com.baeldung.guice.service.ServiceFactory;
|
||||||
import com.baeldung.guice.service.impl.DataPumpServiceImpl;
|
import com.baeldung.guice.service.impl.DataPumpServiceImpl;
|
||||||
|
|
||||||
import ratpack.guice.Guice;
|
import ratpack.guice.Guice;
|
||||||
@ -15,8 +16,8 @@ public class Application {
|
|||||||
.start(server -> server.registry(Guice.registry(bindings -> bindings.module(DependencyModule.class)))
|
.start(server -> server.registry(Guice.registry(bindings -> bindings.module(DependencyModule.class)))
|
||||||
.handlers(chain -> chain.get("randomString", ctx -> {
|
.handlers(chain -> chain.get("randomString", ctx -> {
|
||||||
DataPumpService dataPumpService = ctx.get(DataPumpService.class);
|
DataPumpService dataPumpService = ctx.get(DataPumpService.class);
|
||||||
ctx.render(dataPumpService.generate().length());
|
ctx.render(dataPumpService.generate());
|
||||||
})));
|
}).get("factory", ctx -> ctx.render(ServiceFactory.getInstance().generate()))));
|
||||||
|
|
||||||
// RatpackServer.start(server -> server
|
// RatpackServer.start(server -> server
|
||||||
// .registry(Guice
|
// .registry(Guice
|
||||||
@ -24,7 +25,7 @@ public class Application {
|
|||||||
// .handlers(chain -> chain.get("randomString", ctx -> {
|
// .handlers(chain -> chain.get("randomString", ctx -> {
|
||||||
// DataPumpService dataPumpService = ctx.get(DataPumpService.class);
|
// DataPumpService dataPumpService = ctx.get(DataPumpService.class);
|
||||||
// ctx.render(dataPumpService.generate());
|
// ctx.render(dataPumpService.generate());
|
||||||
// })));
|
// }).get("factory", ctx -> ctx.render(ServiceFactory.getInstance().generate()))));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,9 +1,5 @@
|
|||||||
package com.baeldung.guice.service;
|
package com.baeldung.guice.service;
|
||||||
|
|
||||||
import com.baeldung.guice.service.impl.DataPumpServiceImpl;
|
|
||||||
import com.google.inject.ImplementedBy;
|
|
||||||
|
|
||||||
@ImplementedBy(DataPumpServiceImpl.class)
|
|
||||||
public interface DataPumpService {
|
public interface DataPumpService {
|
||||||
|
|
||||||
String generate();
|
String generate();
|
||||||
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.guice.service;
|
||||||
|
|
||||||
|
import com.baeldung.guice.service.impl.DataPumpServiceImpl;
|
||||||
|
|
||||||
|
public class ServiceFactory {
|
||||||
|
|
||||||
|
private static DataPumpService instance;
|
||||||
|
|
||||||
|
public static void setInstance(DataPumpService dataPumpService) {
|
||||||
|
instance = dataPumpService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DataPumpService getInstance() {
|
||||||
|
if (instance == null) {
|
||||||
|
return new DataPumpServiceImpl();
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user