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
16
votes
3 answers

Play Framework: Dependency Inject Action Builder

since Play Framework 2.4 there is the possibility to use dependency injection (with Guice). Before I used objects (for example AuthenticationService) in my ActionBuilders: object AuthenticatedAction extends ActionBuilder[AuthenticatedRequest] { …
Tim Joseph
  • 847
  • 2
  • 14
  • 28
16
votes
2 answers

Guice @Provides Methods vs Provider Classes

I'm working on a fairly large project that has a lot of injections. We're currently using a class that implements Provider for each injection that needs one, and they mostly have one line get methods. It's starting to get annoying to create a new…
Eliezer
  • 7,209
  • 12
  • 56
  • 103
15
votes
4 answers

Java Generics: Accessing Generic Type at runtime

I'm looking to access the generic type of a declared field during runtime. I was previously under the impression that this was not possible due to the Java type erasure. However, this must not be the case because some well known frameworks…
John Ericksen
  • 10,995
  • 4
  • 45
  • 75
15
votes
4 answers

Scan the classpath for classes with custom annotation

For example if I have annotation @MyOwnAnnotation and have these classes in my classpath, so that I could scan classpath possibly with some kind of filter (example. scan only packages starting with my.own.app.*) and get list of all classes with…
newbie
  • 24,286
  • 80
  • 201
  • 301
15
votes
2 answers

Guice: Difference between Binder#bindConstant() and Binder#bind() ... toInstance

I would like to ask what's the difference between bindConstant().annotatedWith(Names.named("keepAliveInterval")).to(60); and bind(Integer.TYPE).annotatedWith(Names.named("keepAliveInterval")).toInstance(60); I would like to load all my…
zeratul021
  • 2,644
  • 3
  • 32
  • 45
15
votes
1 answer

Changing Guice bindings at runtime

I would like to be able to change the Guice injections at runtime to support multiple injections based on user input. This is what I would like to achieve: public interface IDao { public int someMethod(); } public class DaoEarth implements IDao…
Nepoxx
  • 4,849
  • 5
  • 42
  • 61
15
votes
1 answer

Mimicking Spring profiles in Guice

In Spring, if I want to have one set of objects for production, and another for local development/testing. I could use the @Profile annotation to designate the different classes, and switch between them by providing a system property when starting…
Thorn G
  • 12,620
  • 2
  • 44
  • 56
15
votes
3 answers

Guice proxying to support circular dependency

I'm getting the following error in my code at launch: Tried proxying com.bar.Foo to support a circular dependency, but it is not an interface. How exactly does this proxying work? If I just throw enough classes behind interfaces, will everything…
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
14
votes
1 answer

Guice: Avoid lazy injection

I have a class Cache which is quite expensive to create, but after that is set as a singleton and injected into my service layer. @Override protected void configure() { bind(Cache.class).in(Singleton.class); …
javaNoober
  • 1,338
  • 2
  • 17
  • 43
14
votes
1 answer

JAXB and Guice: How to integrate and visualize?

I find using JAXB together with Guice possible, but challenging: Both libraries "fight" for control over object creation, you have to be careful to avoid cyclic dependencies, and it can get messy with all the JAXB Adapters and Guice Providers and…
Landei
  • 54,104
  • 13
  • 100
  • 195
14
votes
1 answer

Google Guice: Provider with parameters

I have a constructor that depends on classes A and B. I defined it like this: @Inject TestClass(A a, B b) Is there a way in Guice to have one of the constructor parameters injected manually? Problem is, the object of class A cannot be built as it…
Abidi
  • 7,846
  • 14
  • 43
  • 65
14
votes
3 answers

How to avoid having injector.createInstance() all over the place when using guice?

There's something I just don't get about guice: According to what I've read so far, I'm supposed to use the Injector only in my bootstrapping class (in a standalone application this would typically be in the main() method), like in the example below…
sme
  • 5,673
  • 7
  • 32
  • 30
14
votes
1 answer

NOSONAR tag to ignore an invalid issue still shows as an issue

I have the below method which is showing a sonar issue saying the method is not used anywhere. @Provides @ObjectMapperAnnotation public ObjectMapper provideObjectMapper() { //NOSONAR ObjectMapper mapper = new ObjectMapper(); …
AnOldSoul
  • 4,017
  • 12
  • 57
  • 118
14
votes
1 answer

Guice Inject Field in class not created by Guice

I have a class like so, that I create myself somewhere in my code: class StarryEyes { @Inject MyValidator validator; public StarryEyes(String name) { //.. } public doSomething() { // validator is NULL } } I want Guice…
Verhogen
  • 27,221
  • 34
  • 90
  • 109
14
votes
2 answers

How to override guice modules in Playframework unit tests using ScalaTest

I want to write functional test for my controller in PlayFramework. To do that I want to mock implementation of some classes. I found nice example of how to do that using spec2 here:…
Zygimantas Gatelis
  • 1,923
  • 2
  • 18
  • 27