Guice

Spring and Guice together, or just Spring

纵然是瞬间 提交于 2019-11-30 08:25:34
I'm starting a new Java web app from scratch. I don't have much experience on Spring Framework, but I know I'd like to use some of its features, such as Transaccions Management. On the other hand, I really like Guice for dependency injection. I know that Guice and Spring can work together: http://www.jroller.com/mindcrime/entry/an_example_of_integrating_guice But before starting designing my application, I wanted to know if someone had experienced issues by following that approach. Also, what I really like from Guice is that you don't need an XML configuration file, but just java Modules,

Guice: How do I get an instance of a TypeLiteral-wrapped generic?

安稳与你 提交于 2019-11-30 08:16:56
I have a generic database access class, which i'm binding using the TypeLiteral construct. Now in a test i want to mock that class and i have therefor created a Provider, that creates a mock instance. In my test, i want to access that mock to define its behaviour. Now the question is, how can i retrieve the object from the injector? That's my binding definition: binder.bind(new TypeLiteral<GenericDbClass<Integer>>(){}).GenericDbClassProvider.class); Normally i would get an instance like this: injector.getInstance(GenericDbClass.class); But since i'm not binding the implementation of

The guice AbstractModule install method

核能气质少年 提交于 2019-11-30 07:51:48
What does the method install() from the AbstractModule class do? Can someone explain it to me? From the docs I read from the guice site all I could get was: Uses the given module to configure more bindings. Configure what bindings exactly? The bindings from the installed module or the bindings of the class that invoked the install method? install allows for composition: Within its configure method, FooModule may install FooServiceModule (for instance). This would mean that an Injector created based only on FooModule will include bindings and providers in both FooModule and FooServiceModule.

Injecting a generic factory in Guice

我是研究僧i 提交于 2019-11-30 07:27:21
问题 The following code is an example of a factory that produces a Bar<T> given a Foo<T> . The factory doesn't care what T is: for any type T , it can make a Bar<T> from a Foo<T> . import com.google.inject.*; import com.google.inject.assistedinject.*; class Foo<T> { public void flip(T x) { System.out.println("flip: " + x); } } interface Bar<T> { void flipflop(T x); } class BarImpl<T> implements Bar<T> { Foo<T> foo; @Inject BarImpl(Foo<T> foo) { this.foo = foo; } public void flipflop(T x) { foo

Guice 3.0 + Tomcat 7.0 = ClassLoader memory leak

安稳与你 提交于 2019-11-30 07:01:26
I know that this problem has been around for at least 3 yeears ( Issue 92 ), but I'm still not satisfied with the current state of it. I am also aware that this does not affect Tomcat if you do restart after redeploying (as suggested in Guice + Tomcat potential memory leak ). My problem is that I am experiencing OutOfMemoryError: PermGen errors after some redeployments. Notice that I am not using google-collections explicitly, I am only using Guice 3.0 (via maven). After analyzing heap dumps, I still see that the thread com.google.inject.internal.Finalizer is still active, keeps a reference to

Guice - How to share the same Singleton instance through multiple injectors/modules

假如想象 提交于 2019-11-30 06:50:55
In guice, the @Singleton scope does not refer to the Singleton pattern. According to the "Dependency Injection" book of "Dhanji" : Very simply, a singleton’s context is the injector itself. The life of a singleton is tied to the life of the injector (as in figure 5.8). Therefore, only one instance of a singleton is ever created per injector. It is important to emphasize this last point, since it is possible for multiple injectors to exist in the same application. In such a scenario, each injector will hold a different instance of the singleton-scoped object. Is it possible to share the same

Guice : How to bind classes that are dynamically obtained by an already binded object?

只愿长相守 提交于 2019-11-30 05:40:51
问题 I'm developing a small web framework using Guice. I have a Router object that, once initialized, expose a getControllerClasses() method. I have to loop over all those dynamically returned classes to bind() them using Guice. I bind the Router : bind(IRouter.class).to(Router.class); But then, how can I obtain the binded Router instance, in a Module, so I can also bind the classes returned by its getControllerClasses() method? The only way I've been able to get the Router instance in a Module,

Guice creates Swing components outside of UI thread problem?

ぐ巨炮叔叔 提交于 2019-11-30 05:27:14
问题 I'm working on Java Swing application with Google Guice as an IOC container. Things are working pretty well. There are some UI problems. When a standard L&F is replaced with Pushing pixels Substance L&F application is not running due to Guice's Swing components creation outside of UI thread. Is there a way to tell Guice to create Swing components in the UI thread? Maybe I should create custom providers which will return Swing components after SwingUtilities.invokeAndWait(Runnable) creates

Injecting generics with Guice

爷,独闯天下 提交于 2019-11-30 04:29:38
I am trying to migrate a small project, replacing some factories with Guice (it is my first Guice trial). However, I am stuck when trying to inject generics. I managed to extract a small toy example with two classes and a module: import com.google.inject.Inject; public class Console<T> { private final StringOutput<T> out; @Inject public Console(StringOutput<T> out) { this.out = out; } public void print(T t) { System.out.println(out.converter(t)); } } public class StringOutput<T> { public String converter(T t) { return t.toString(); } } import com.google.inject.AbstractModule; import com.google

Guice and interface that has multiple implementations

混江龙づ霸主 提交于 2019-11-30 03:25:41
If I have interface Validator and multiple implementations for this interface. How can I inject any of the multiple implementations with Guice? Now I know that I can use following code to inject one, but it allows only one implementation: public class MyModule extends AbstractModule { @Override protected void configure() { bind(Validator.class).to(OneOfMyValidators.class); } } What I would like to do is: Validator v1 = injector.getInstance(Validator1.class); Validator v2 = injector.getInstance(Validator2.class); Is it possible at all? Andrew McNamee Short answer: binding annotations. They're