Questions tagged [guice]

Guice is a lightweight dependency injection framework for Java.

Google Guice

Guice is Google's lightweight dependency injection framework. It is an open source software framework for the Java platform, compatible with Java 5 and above, released by Google under the Apache License.

It provides support for dependency injection using annotations to configure Java objects. Guice allows implementation classes to be programmatically bound to an interface, then injected into constructors, methods or fields using an @Inject annotation. When more than one implementation of the same interface is needed, the user can create custom annotations that identify an implementation, then use that annotation when injecting it.

Starting with version 3.0, Guice implements the JSR-330 standard of dependency injection for the Java platform.

References

Related Tags:

3211 questions
42
votes
5 answers

Pass parameter to constructor with Guice

I have a factory as below, public final class Application { private static IFoo foo; public static IFoo getFoo(String bar) { // i need to inject bar to the constructor of Foo // obvious i have to do something, not sure…
DarthVader
  • 52,984
  • 76
  • 209
  • 300
42
votes
3 answers

Can anyone provide a clear explanation of why Google Guice is useful?

I've read about Google Guice, and understand the general issues with other approaches to dependency injection, however I haven't yet seen an example of someone using Guice "in practice" where its value becomes clear. I'm wondering if anyone is aware…
sanity
  • 35,347
  • 40
  • 135
  • 226
40
votes
2 answers

Accessing Guice injector in its Module?

I am extending Guice's AbstractModule and inside of the extending class I need access to Guice's injector. It that possible, if yes, how?
stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270
39
votes
3 answers

How to have a Guice module use another Guice module?

Let's say I have a Guice module ProdModule that I would like to depend on other GuiceModules, ProdDbModule and ProdPubSubModule. How would I implement ProdModule's configure()?
Noel Yap
  • 18,822
  • 21
  • 92
  • 144
38
votes
2 answers

Injecting Collection of Classes with Guice

I'm trying to inject things with Google Guice 2.0 and I have the following structure: FooAction implements Action BarAction implements Action I then have an ActionLibrary with the following constructor: ActionLibrary (List theActions) When…
Scruffers
  • 4,984
  • 8
  • 35
  • 41
38
votes
6 answers

Dependency Injection in OSGI environments

First some background: I'm working on some webapp prototype code based on Apache Sling which is OSGI based and runs on Apache Felix. I'm still relatively new to OSGI even though I think I've grasped most concepts by now. However, what puzzles me is…
ilikeorangutans
  • 1,753
  • 1
  • 11
  • 14
37
votes
3 answers

How to bind one implementation to a few interfaces with Google Guice?

I need to bind one class as implementation of two interfaces. And it should be binded in a singleton scope. What I've done: bind(FirstSettings.class). to(DefaultSettings.class). in(Singleton.class); bind(SecondSettings.class). …
Pavel
  • 1,019
  • 2
  • 10
  • 19
36
votes
4 answers

Guice, JDBC and managing database connections

I'm looking to create a sample project while learning Guice which uses JDBC to read/write to a SQL database. However, after years of using Spring and letting it abstract away connection handling and transactions I'm struggling to work it our…
pledge
  • 1,054
  • 3
  • 12
  • 17
35
votes
6 answers

In what maven2 repository can Google Guice be found?

As far as I understand Google Guice 2.0 is out not so long ago. But I see that central repo still has outdated 1.0 version. Please, tell where can I find maven2 repository with Google Guice 2.0.
Artyom Sokolov
  • 2,385
  • 3
  • 23
  • 34
35
votes
10 answers

Java Dependency injection: XML or annotations

Annotations becoming popular. Spring-3 supports them. CDI depends on them heavily (I can not use CDI with out of annotations, right?) My question is why? I heard several issues: "It helps get rid of XML". But what is bad about xml? Dependencies are…
Ilya K
  • 413
  • 1
  • 5
  • 6
35
votes
2 answers

Guice best practices and anti-patterns

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.
ripper234
  • 222,824
  • 274
  • 634
  • 905
34
votes
4 answers

How do I test Guice injections?

I gave to Google Guice the responsibility of wiring my objects. But, how can I test if the bindings are working well? For example, suppose we have a class A which has a dependence B. How can I test that B is injected correctly? class A { private…
yeraycaballero
  • 1,603
  • 3
  • 19
  • 29
32
votes
4 answers

How to inject Injector?

Situation: i need lazy dependency instantiation in some FooClass, so i pass Injector to class as a constructor parameter. private final Injector m_injector; public FooClass(@Named("FooInjector") Injector injector) { m_injector =…
Alex M
  • 395
  • 1
  • 4
  • 6
31
votes
1 answer

What's aopalliance all about? And why is guice using it?

I'm using guice for dependency injection with aop from aopalliance. I can't quite figure out what's aopalliance all about and who implemented the version (dated from 2004) that's on their sourceforge page. Why is guice using this version instead of…
ivo
  • 4,101
  • 5
  • 33
  • 42
30
votes
3 answers

Why use @Singleton over Scala's object in Play Framework?

I have been using Play! Framework for Scala for nearly a year now. I am currently using version 2.5.x. I am aware of the evolution of controllers in Play and how developers have been forced away from static object routes. I am also aware of the…
Rhys Bradbury
  • 1,699
  • 13
  • 24