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
21
votes
6 answers

Which Java Web Framework fits best with Google Guice?

I'm planning to start on a new project and am looking at the current state-of-the-art Java web frameworks. I decided to build my application around Guice, and am likely to use a very lightweight ORM like Squill/JEQUEL/JaQu or similar, but I can't…
Jevgeni Kabanov
  • 2,632
  • 24
  • 23
21
votes
5 answers

Guice injector.getInstance() - good practice?

Let's say I have two applications sharing the same library. This library contains common classes like DAOs, Utils, etc. Everything in the shared library is wired with Guice. My two apps depend on this library but do not have a direct dependency on…
albogdano
  • 2,710
  • 2
  • 33
  • 43
21
votes
2 answers

What is the Spring equivalent for CDI's Instance, or Guices Provider

In CDI you can define an object that will give you items of a certain type, using: @Inject Instance myObjectInstance; //... MyObject myObjectInstance.get(); Similarly in Guice you can do: @Inject Provider
bogdan.mustiata
  • 1,725
  • 17
  • 26
20
votes
1 answer

Jersey Guice JSP how to?

I'm using tomcat 6, jersey 1.8 with jersey guice and guice 3. I'm having problem using JSP with my setup. I have a "Status" servlet served as a simple servlet configured by web.xml, a jersey servlet configured by GuiceFilter which returns a jsp…
Eishay Smith
  • 1,137
  • 9
  • 13
20
votes
4 answers

Does Guice Persist provide transaction scoped or application managed EntityManager?

We use Guice Persist to inject EntityManager in our project. E.g. public class MyDao{ @Inject EntityManager em; public void someMethod(){ //uses em instance } } But it is unclear for us how injected instance of EntityManager is…
Piotr Sobczyk
  • 6,443
  • 7
  • 47
  • 70
19
votes
5 answers

Using Google Guice to inject java properties

I want to use google guice to make properties available in all classes of my application. I defined a Module which loads and binds the properties file Test.properties. Property1=TEST Property2=25 package com.test; import…
markus
  • 6,258
  • 13
  • 41
  • 68
19
votes
4 answers

Guice and interface that has multiple implementations

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…
newbie
  • 24,286
  • 80
  • 201
  • 301
19
votes
1 answer

Is it possible to automatically clean up resources at the end of scope in Guice?

Let's say I have a Closeable object injected through Guice using request scope: @Provides @RequestScoped public MyCloseableResource providesMyCloseableResource(){ return new MyCloseableResourceImpl(); } Is it possible to hook-up a clean-up method…
rodion
  • 14,729
  • 3
  • 53
  • 55
19
votes
2 answers

Why am I getting "classes must have either one (and only one) constructor" error?

I have been trying to get Guice working but end up with this: Classes must have either one (and only one) constructor My interface: public interface AddrBookStore { public Contact getContactByKey(String key); public void addContact(Contact…
Kartz Kartel
  • 193
  • 1
  • 1
  • 4
19
votes
1 answer

Is there an equivalent in CDI(WELD) to build definitions (as done in Guice modules) and then create an Injector?

I like the way Guice makes it fairly straight forward to manually create your own modules each with their own bindings done in code. CDI on the other hand seems to rely more on magic rather than programmatic access to sest bindings. Am i wrong or…
mP.
  • 18,002
  • 10
  • 71
  • 105
19
votes
2 answers

What is the best way to inject a list with google-guice?

I have three classes CircleBuilder, SquareBuilder, and TriangleBuilder implementing the ShapeBuilder interface. I need to initialize my FormBuilder and notably a BuilderList (extending List) with one instance of each class with…
Name is carl
  • 5,961
  • 3
  • 29
  • 44
19
votes
2 answers

Guice injection null pointer

We try to refactoring a project with Guice. The idea is to bind all the Language interface to a concreate object like French or Polish. We have a module for binding: public class StandardModule extends AbstractModule { @Override protected…
user1810567
  • 1,129
  • 3
  • 14
  • 22
18
votes
2 answers

Generics Hell: Can I construct a TypeLiteral> using generics?

The only way I was able to get the below generic method to work was to pass the seemingly redundant TypeLiteral> parameter. I believe it should be possible to construct this parameter programmatically given the other parameter, but can't…
Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
18
votes
2 answers

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

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…
Sandro Munda
  • 39,921
  • 24
  • 98
  • 123
18
votes
1 answer

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

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…
user655145