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
18
votes
4 answers

Swing with Guice

I'm already using Guice for the data model of my app, and so far I'm quite happy with it. However, the GUI part is about to become a Big Ball of Mud. I find it hard to use Guice here, because Swing components and models are tightly coupled, and…
Landei
  • 54,104
  • 13
  • 100
  • 195
18
votes
4 answers

Guice Performance on Android

As a Java developer I've become accustomed to having dependency injection available in applications. For Android though, I am especially wary of performance. What are the performance implications for using Guice in an Android app? I assume there…
user605331
  • 3,718
  • 4
  • 33
  • 60
18
votes
1 answer

How can I find all singletons on Guice which implement a specific type?

Imagine I have a type Disposable which some classes implement: class FactoryImpl implements Disposable {} I can bind this class as a singleton: bind(Factory.class) .to(FactoryImpl.class) .in(Singleton.class); or as an eager…
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
18
votes
1 answer

Guice assistedinject already configured

I have an issue with the AssistedInject. I followed the instructions on this link https://github.com/google/guice/wiki/AssistedInject but when I run my application I get an error: ERROR [2015-04-23 14:49:34,701]…
almy
  • 193
  • 1
  • 1
  • 7
18
votes
1 answer

In guice is there a difference between @provides and bind()?

I am wondering what the difference is between using @provides on a method and using bind() in my guice modules. I usually override AbstractModule.configure() and bind all my implementations to my interfaces like this: public class MyModule extends…
Ben Glasser
  • 3,216
  • 3
  • 24
  • 41
18
votes
5 answers

Implementing dynamic plugins in Java

I'd like to implement a dynamic plugin feature in a Java application. Ideally: The application would define an interface Plugin with a method like getCapabilities(). A plugin would be a JAR pluginX.jar containing a class PluginXImpl implementing…
Chris Conway
  • 55,321
  • 43
  • 129
  • 155
17
votes
5 answers

Is Guice's @ImplementedBy evil? Is it appropriate in some cases?

I've heard claims that "@ImplementedBy is evil", on grounds that it breaks DI concepts and makes the interface aware of its implementors. This might be true in some cases, but often I found that it just leads to cleaner code (no long Modules to…
ripper234
  • 222,824
  • 274
  • 634
  • 905
17
votes
6 answers

Is there ever a case for 'new' when using dependency injection?

Does dependency injection mean that you don't ever need the 'new' keyword? Or is it reasonable to directly create simple leaf classes such as collections? In the example below I inject the comparator, query and dao, but the SortedSet is directly…
parkr
  • 3,188
  • 5
  • 35
  • 39
17
votes
1 answer

What's new in Google Guice 3?

Guice 3 is just around the corner, but I cannot find a page telling me what's new in this version. So, what's new in Guice 3?
deamon
  • 89,107
  • 111
  • 320
  • 448
17
votes
1 answer

Guice configuration error: No implementation was bound

I'm trying to make DI with Guice work, doing (as it seems to me) exactly what's in the manual. I can't explain the problem because I don't really understand it - everything seems very logical and should work.. but it doesn't. So, I can only attach…
Roman
  • 64,384
  • 92
  • 238
  • 332
17
votes
1 answer

Binding @Provides method as eager singleton

I want to make a binding using a method annotated with @Provides into an eager singleton. I've found bug 216, which suggests this isn't possible, but doesn't mention the @Provides annotation explicitly. I currently have a class that requests the…
Jorn
  • 20,612
  • 18
  • 79
  • 126
17
votes
3 answers

Guice: Using @Named to create object

Using Guice, if I have @Inject @Named("light") Color light; I can use bind(Color.class).annotatedWith(Names.named("light")) .toInstance(new Color("white")); to bind it to white color if Color constructor only requires color name. But how can…
Ali Shakiba
  • 20,549
  • 18
  • 61
  • 88
16
votes
3 answers

Has anyone used ServiceLoader together with Guice?

I keep wanting to try this on a larger scale with our app + build system, but higher priorities keep pushing it to the back burner. It seems like a nice way to load Guice modules and avoids the common complaint about "hard coded configuration".…
Mark Renouf
  • 30,697
  • 19
  • 94
  • 123
16
votes
1 answer

What techniques do you use to debug complex guice bindings?

I have a complex set of bindings that include many private bindings to solve the robot legs problem. Because of Guice's limited ability to report intelligible biding errors, I'm wondering what effective tools or techniques, if any, besides reading…
Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
16
votes
2 answers

How to fix Guice error, "An illegal reflective access operation has occurred"

I've a Kotlin project which uses Guice for DI and has recently been updated from JDK 8 -> 11. It now emits the following error at runtime: WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by…
Matt R
  • 9,892
  • 10
  • 50
  • 83