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
159
votes
5 answers

Overriding Binding in Guice

I've just started playing with Guice, and a use-case I can think of is that in a test I just want to override a single binding. I think I'd like to use the rest of the production level bindings to ensure everything is setup correctly and to avoid…
tddmonkey
  • 20,798
  • 10
  • 58
  • 67
107
votes
6 answers

Google Guice vs. PicoContainer for Dependency Injection

My team is researching dependency injection frameworks and is trying to decide between using Google-Guice and PicoContainer. We are looking for several things in our framework: A small code footprint - What I mean by a small code footprint is we…
austen
  • 3,314
  • 3
  • 25
  • 26
103
votes
2 answers

How to retrieve annotated instance from Guice's injector?

Let's say I have a module: Module extends AbstractModule { @Override protected void configure() { bind(String.class). annotatedWith(Names.named("annotation")). toInstance("DELIRIOUS"); } } and I want to test the module and…
Boris Pavlović
  • 63,078
  • 28
  • 122
  • 148
96
votes
1 answer

How to use Guice's AssistedInject?

I've read https://github.com/google/guice/wiki/AssistedInject, but it doesn't say how to pass in the values of the AssistedInject arguments. What would the injector.getInstance() call look like?
Noel Yap
  • 18,822
  • 21
  • 92
  • 144
86
votes
1 answer

Why use/develop Guice, when You have Spring and Dagger?

To my knowledge, Dagger does generate code, while Guice and Spring rely on runtime processing, thus Dagger works faster, but requires more work on programmer side. Because of performance edge it's good for mobile (Android) development. However, when…
spam
  • 1,853
  • 2
  • 13
  • 33
71
votes
8 answers

Guice call init method after instantinating an object

Is it possible to tell Guice to call some method (i.e. init()) after instantinating an object of given type? I look for functionality similar to @PostConstruct annotation in EJB 3 (and Spring).
mgamer
  • 13,580
  • 25
  • 87
  • 145
67
votes
1 answer

How to specify a classifier in a gradle dependency's dependency?

Say I want to add guice-assistedinject as a dependency in my project. It specifies the guice artifact as a dependency itself. How do I tell it to use the no_aop version of guice? I know I can do the following, but can I do it in one step without…
jgrowl
  • 2,117
  • 2
  • 17
  • 23
64
votes
11 answers

Practical advice on using Jersey and Guice for RESTful service

From what I can find online, the state of the art for Guice + Jersey integration has stagnated since 2008 when it appears both teams reached an impasse. The crux of the issue is that JAX-RS annotations perform field and method injection and this…
user23987
56
votes
3 answers

Replacing com.google.inject with javax.inject

Is it true that javax.inject annotations can function as direct replacements for com.google.inject? So that, if I replaced all my current guice/gin annotations with those from javax.inject, my app would compile and run just fine? First, does…
Blessed Geek
  • 21,058
  • 23
  • 106
  • 176
56
votes
4 answers

Inject Generic Implementation using Guice

I would like to be able to inject a generic implementation of a generic interface using Guice. public interface Repository { void save(T item); T get(int id); } public MyRepository implements Repository { @Override public void…
Sean Carpenter
  • 7,681
  • 3
  • 37
  • 38
56
votes
1 answer

The guice AbstractModule install method

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…
MykelXIII
  • 1,085
  • 1
  • 8
  • 16
48
votes
3 answers

How to bind concrete classes?

I have this class: public class House { private final Door door; private final Window window; private final Roof roof; @Inject public House(Door door, Window window, Roof roof) { this.door = door; this.window =…
helpermethod
  • 59,493
  • 71
  • 188
  • 276
46
votes
5 answers

Java error: Found interface ... but class was expected

I am getting a strange runtime error from my code: "Found interface [SomeInterface] but class was expected" How can this happen? How can an interface get instantiated? Update: (In response to some answers) I am compiling and running against the…
levik
  • 114,835
  • 27
  • 73
  • 90
45
votes
2 answers

Lombok - retain field's annotation in constructor input params

Lombok misses field's annotation while auto generating constructor. Is there a way to retain field's annotation in constructor input params? Class to generate constructor, @RequiredArgsConstructor(onConstructor = @__(@Inject)) public class Test { …
sidss
  • 923
  • 1
  • 12
  • 20
43
votes
1 answer

Guice and properties files

Does anybody have an example of how to use Google Guice to inject properties from a .properties file. I was told Guice was able to validate that all needed properties exist when the injector starts up. At this time I cannot find anything on the…
benstpierre
  • 32,833
  • 51
  • 177
  • 288
1
2 3
99 100