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:
Abhinab Kanrar 2017-04-02 01:17:23 +05:30 committed by maibin
parent 87be4ede27
commit 15a4a23099
3 changed files with 25 additions and 8 deletions

View File

@ -2,6 +2,7 @@ package com.baeldung.guice;
import com.baeldung.guice.config.DependencyModule;
import com.baeldung.guice.service.DataPumpService;
import com.baeldung.guice.service.ServiceFactory;
import com.baeldung.guice.service.impl.DataPumpServiceImpl;
import ratpack.guice.Guice;
@ -15,8 +16,8 @@ public class Application {
.start(server -> server.registry(Guice.registry(bindings -> bindings.module(DependencyModule.class)))
.handlers(chain -> chain.get("randomString", ctx -> {
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
// .registry(Guice
@ -24,8 +25,8 @@ public class Application {
// .handlers(chain -> chain.get("randomString", ctx -> {
// DataPumpService dataPumpService = ctx.get(DataPumpService.class);
// ctx.render(dataPumpService.generate());
// })));
// }).get("factory", ctx -> ctx.render(ServiceFactory.getInstance().generate()))));
}
}
}

View File

@ -1,9 +1,5 @@
package com.baeldung.guice.service;
import com.baeldung.guice.service.impl.DataPumpServiceImpl;
import com.google.inject.ImplementedBy;
@ImplementedBy(DataPumpServiceImpl.class)
public interface DataPumpService {
String generate();

View File

@ -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;
}
}