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
30
votes
1 answer

Injecting generics with Guice

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…
paradigmatic
  • 40,153
  • 18
  • 88
  • 147
29
votes
1 answer

What is the difference between javax.inject.Inject and com.google.inject.Inject?

I'm initiating myself to Google Guice. I have a simple question : What is the difference between the javax.inject's @Inject annotation and the com.google.inject's @Inject one ? Thanks.
Maoori
  • 293
  • 1
  • 3
  • 4
29
votes
5 answers

How to instantiate Spring managed beans at runtime?

I stuck with a simple refactoring from plain Java to Spring. Application has a "Container" object which instantiates its parts at runtime. Let me explain with the code: public class Container { private List runtimeBeans = new…
Vadim Kirilchuk
  • 3,532
  • 4
  • 32
  • 49
29
votes
1 answer

Guice: differences between Singleton.class and @Singleton

In Guice, what's the difference between: // Inside your AbstractModule subclass: @Override public void configure() { bind(Service.class).to(ServiceImpl.class).in(Singleton.class); } And: @Override public void configure() { …
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
28
votes
7 answers

Guice injector in JUnit tests

Using Guice, is it a good practice to get a new injector in each JUnit test class, as each test class should be independant?
Alexis Dufrenoy
  • 11,784
  • 12
  • 82
  • 124
28
votes
2 answers

How does guice's TypeLiteral work?

How does Guice's TypeLiteral overcome the Java generic types erasure procedure? It works wonders but how is this accomplished?
mgamer
  • 13,580
  • 25
  • 87
  • 145
28
votes
3 answers

Guice: how do you configure an @Provides and @Singleton in a module in this case?

I have a provider method in a module annotated with @Provides: @Provides public ChatServicePerformanceMonitor getChatServicePerfMon() { ... } and I have annotated my ChatServicePerformanceMonitor with @Singleton. In my code, where I use this…
Alper Akture
  • 2,445
  • 1
  • 30
  • 44
27
votes
2 answers

How to use two Guice modules that install a common dependency module

I'm working on a project that consists of four parts: The Main project that brings everything together. This contains the public static void main(String... args) entry point. Component A Component B A 3rd party Common component that both A and B…
derabbink
  • 2,419
  • 1
  • 22
  • 47
27
votes
3 answers

Hidden Features of Google Guice

Google Guice provides some great dependency injection features. I came across the @Nullable feature recently which allows you to mark constructor arguments as optional (permitting null) since Guice does not permit these by default: e.g. public…
Jonathan Holloway
  • 62,090
  • 32
  • 125
  • 150
27
votes
2 answers

Tomcat WebSocketServlet and Google Guice

I have a webapp which requires the usage of Tomcat 7 web sockets. In this webapp all standard Servlets (those extending javax.servlet.http.HttpServlet) work nicely (and correctly) with Google Guice. To make my Servlet work with Guice handlers I…
Joseph Victor Zammit
  • 14,760
  • 10
  • 76
  • 102
27
votes
2 answers

The purpose of Guice

I (think I) understand the purpose of dependency injection, but I'm just not getting why I need something like Guice to do it (well, obviously I don't need Guice, but I mean why it would be beneficial to use it). Let's say I have existing…
user1759936
  • 273
  • 2
  • 5
26
votes
1 answer

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

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…
Groostav
  • 3,170
  • 1
  • 23
  • 27
26
votes
7 answers

How to scan classes for annotations?

I have a plain jane servlets web application, and some of my classes have the following annotations: @Controller @RequestMapping(name = "/blog/") public class TestController { .. } Now when my servlet applications starts up, I would like to get a…
loyalflow
  • 14,275
  • 27
  • 107
  • 168
25
votes
4 answers

Guice: is it possible to inject modules?

I have a Module that requires some arbitrary Depedency. Is there a way Modules themselves can be injected? I realize this is a bit of a chicken and egg situation... Example: public class MyModule implements Module { private final Dependency…
sxc731
  • 2,418
  • 2
  • 28
  • 30
25
votes
1 answer

Guice injecting Generic type

I'am trying to Inject generic type with Guice. I have Repository< T > which is located in the Cursor class. public class Cursor { @Inject protected Repository repository; So when I create Cursor< User >, I also want the Guice to…
petomalina
  • 2,020
  • 2
  • 19
  • 25