Guice

Guice eager/lazy singleton instantiations

好久不见. 提交于 2019-12-09 09:18:55
问题 I'm having some troubles understanding how Guice's singleton instantiations works. I've read the available documentation (here - http://code.google.com/p/google-guice/wiki/Scopes ), but I still can't figure out some things: 1) I've integrated Guice with Tomcat, and I've set up some bindings in a ServletModule: bind(MyServlet.class).asEagerSingleton(); serve("myUrl").with(MyServlet.class); serve("myOtherUrl").with(MyOtherServlet.class); (where MyOtherServlet class has a @Singleton annotation

Modern Akka DI with Guice

萝らか妹 提交于 2019-12-09 09:18:32
问题 Java 8, Guice 4.0 and Akka 2.3.9 here. I am trying to figure out how to annotate my actor classes with JSR330-style @Inject annotations, and then wire them all up via Guice. But literally every single article I have read (some examples below) either uses Scala code examples, a criminally-old version of Guice, or a criminally-old version of Akka: Let It Crash Scala-Guice So, given the following Guice module: public interface MyService { void doSomething(); } public class MyServiceImpl

Guice best practices and anti-patterns

本秂侑毒 提交于 2019-12-09 07:27:28
问题 I'm not sure if there is merit to this question or not, but are there any best practices and anti-patterns specific to Google Guice? Please direct any generic DI patterns to this question. 回答1: I have always felt that constructor injection to final fields is a best practice. It minimizes mutable state and makes the class easier to understand by making the class's formal dependencies explicit. public class MyClass { private final MyDependency dependency; @Inject public MyClass(MyDependency

Inject in scala object

最后都变了- 提交于 2019-12-09 03:08:20
问题 I'm using Play framework 2.5 and try to inject WSClient in a scala object used in my controllers. import play.api.libs.concurrent.Execution.Implicits.defaultContext object MyObject { @Inject var ws: WSClient = null def doSomething() = { // use wsclient } } I use MyObject in several controllers and when calling doSomething() wsclient is null. 回答1: You should define MyObject as class and inject wsclient to it: class MyObject @Inject()(ws: WSClient) { def doSomething() = { /* use wsclient */ } }

Integrate Guice component into Spring application

拜拜、爱过 提交于 2019-12-09 02:59:37
问题 We have a Spring framework based application and need to integrate a component that is built using Google Guice. Can anybody give us some advice on how this can be done? We came across the following links that show how integrate Spring into Guice, but we need it the other way around: http://google-guice.googlecode.com/git/javadoc/com/google/inject/spring/SpringIntegration.html http://www.jroller.com/mindcrime/entry/an_example_of_integrating_guice Any help is appreciated 回答1: No Special

Bind in Guice without to

China☆狼群 提交于 2019-12-08 17:40:20
问题 I have a question: Usually in Guice I use bind(classe).to(another_class_Implementation) ... However I found in a code source that they have used bind(class) only ( without the part ".to(another_class_Implementation)" ) ... What does this mean ( bind(class) without "to or as" ) ? Here is the part of code in question: public class RestModule extends AbstractModule { @Override protected void configure() { bind(RootResource.class); bind(DeveloperUtilsRedirector.class); bind(M34Repository.class)

(Play 2.4) Dependency injection in a trait?

为君一笑 提交于 2019-12-08 17:40:06
问题 In play 2.4, is it possible to use dependency injection in a trait ? Is there any example ? Thanks. 回答1: I talk about runtime DI with Guice here because it's the default method used by Play. Other DI methods or frameworks may differ here. It isn't possible to inject a dependency into a trait because a trait isn't instantiable. A trait doesn't have a constructor to define the dependencies. In Play you could use the injector directly as long as the Application trait is in scope. But this isn't

Using @Assisted inject with multiple params of same Type (@Named params)

自作多情 提交于 2019-12-08 15:48:52
问题 my problem boils down to using @Assisted with two string arguments to the factory. The problem is that because Guice treats type as the identification mechanism for parameters, both parameters are the same, and I get a configuration error. Some Code: public class FilePathSolicitingDialog { //... some fields public static interface Factory { public FilePathSolicitingDialog make(Path existingPath, String allowedFileExtension, String dialogTitle); } @Inject public FilePathSolicitingDialog

Best way to initialize JpaPersistModule

风格不统一 提交于 2019-12-08 11:20:28
问题 I use com.google.inject.persist.jpa.JpaPersistModule in my application. The configuration is located in persistence.xml file, but some of the properties are dynamic and I don't want store them in this file (for example javax.persistence.jdbc.url etc) but rather inject them from some other source. I know that there's a JpaPersistModule.properties(java.util.Properties p) method that allows to do exactly what I need. The problem is that I don't see a good way to pass that java.util.Properties

DI, Guice and Strategy Pattern

白昼怎懂夜的黑 提交于 2019-12-08 10:56:03
问题 Suppose I have the following base class, Queen and Knight as its derivatives. WeaponBehaviour is an interface. I can't figure out how to inject weapons using Guice depending on the concrete GameCharacter type. public abstract class GameCharacter { @Inject protected WeaponBehaviour weapon; public GameCharacter() { } public void fight() { weapon.useWeapon(); } public void setWeapon(WeaponBehaviour weapon) { this.weapon = weapon; } } 回答1: You could use Binding Annotations. A subclass: class