guice
Guice (pronounced 'juice') is a lightweight dependency injection framework for Java 6 and above, brought to you by Google.
google/guice · GitHub guice - guice (pronounced 'juice') is a lightweight dependency injection framework for java 6 and above, brought to you by google.
I'm trying to inject things with Google Guice 2.0 and I have the following structure:
FooAction implements Action
BarAction implements Action
I then have an ActionLibrary with the following constructor:
ActionLibrary (List<Action> theActions)
When I request an instance of ActionLibrary from Guice, I would like Guice to identify both of the registered Action classes (FooAction, BarAction) and pass them into the constructor. The motivation here being that when I add a third action BazAction, it would be as simple as registering it in the Module and it would automatically be added to the list in the constructor.
Is this possible?
Source: (StackOverflow)
Maybe I am just blind, but I do not see how to use Guice (just starting with it) to replace the new
call in this method:
public boolean myMethod(String anInputValue) {
Processor proc = new ProcessorImpl(anInputValue);
return proc.isEnabled();
}
For testing there might be a different implementation of the Processor, so I'd like to avoid the new
call and in the course of that get rid of the dependency on the implementation.
If my class could just remember an instance of Processor I could inject it via the constructor, but as the Processors are designed to be immutable I need a new one every time.
How would I go about and achieve that with Guice (2.0) ?
Source: (StackOverflow)
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 (non-Guice) code that's something like this:
public SomeBarFooerImplementation(Foo foo, Bar bar) {
this.foo = foo;
this.bar = bar;
}
public void fooThatBar() {
foo.fooify(bar);
}
And somewhere higher level, maybe in my main(), I have:
public static void main(String[] args) {
Foo foo = new SomeFooImplementation();
Bar bar = new SomeBarImplementation();
BarFooer barFooer = new SomeBarFooerImplementation(foo, bar);
barFooer.fooThatBar();
}
Now I've basically got the benefits of dependency injection, right? Easier testability and so forth? Of course if you want you could easily change the main() to get implementation classnames from configuration instead of hardcoding, too.
As I understand it, to do the same in Guice, I'd do something like:
public SomeModule extends AbstractModule {
@Override
protected void configure() {
bind(Foo.class).to(SomeFooImplementation.class);
bind(Bar.class).to(SomeBarImplementation.class);
bind(BarFooer.class).to(SomeBarFooerImplementation.class);
}
}
@Inject
public SomeBarFooerImplementation(Foo foo, Bar, bar) {
this.foo = foo;
this.bar = bar;
}
public static void main(String[] args) {
Injector injector = Guice.createInjector(new SomeModule());
Foo foo = injector.getInstance(Foo.class);
barFooer.fooThatBar();
}
Is that it? It seems to me like just syntactic sugar, and not particularly helpful syntactic sugar. And if there's some advantage to breaking the "new XxxImplementation()" stuff out into a separate module rather than doing it directly in main(), that's easily done without Guice anyway.
So I get the feeling that I'm missing something very basic. Could you please explain to me how the Guice way is advantageous?
Thanks in advance.
Source: (StackOverflow)
I am planning to use dependency injection framework.
what should I choose? Guice or Spring?
I will use java configuration both ways. also my application is pure java j2se, not web/j2ee application and I don't plan to use spring's rich capabilities. my main concern about guice is that it is much less popular and not in common use. I want a mature, documented and easy to use DI framework.
Can you tell me what is the preferred option from your experience?
Source: (StackOverflow)
Situation: i need lazy dependency instantiation in some FooClass
, so i pass Injector
to class as a constructor parameter.
private final Injector m_injector;
public FooClass(@Named("FooInjector") Injector injector) {
m_injector = injector;
}
But guice doesn't permit to bind core classes (injectors, modules and etc). What is the solution?
Source: (StackOverflow)
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 guice wiki about this.
Source: (StackOverflow)
As far as I understand Google Guice 2.0 is out not so long ago. But I see that central repo still has outdated 1.0 version. Please, tell where can I find maven2 repository with Google Guice 2.0.
Source: (StackOverflow)
I gave to Google Guice the responsability of wiring my objects. But, How can I test if the bindings are working well.
For example, suppose we have a class A which has a dependence B. How can I test than B is injected correctly.
class A {
private B b;
public A() {}
@Inject
public void setB(B b) {
this.b = b
}
}
Notice A hasn't got a getB() method and I want to assert that A.b isn't null.
Source: (StackOverflow)
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 decide on the web framework. Which one would fit best in such a lightweight environment? And which one integrates best with Guice?
Source: (StackOverflow)
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 don't want to have dependency injection code litter everywhere in our code base. If we need to refactor down the road, we want it to be as easy as possible.
- Performance - How much overhead does each framework have when creating and injecting objects?
- Ease of use - Is there a large learning curve? Do we have to write mounds of code to get something simple working? We want to have as little configuration as possible.
- Community size - Larger communities usually means that a project will continue to be maintained. We don't want to use a framework and have to fix our own bugs ;) Also any questions we have along the way can (hopefully) be answered by the framework's developer/user community .
Comparisons of the two frameworks against the listed criteria would be greatly appreciated. Any personal experiences that help to compare the two would also be extremely helpful.
Disclaimer: I'm fairly new to dependency injection so excuse my noob-ness if I asked a question that isn't pertinent to this discussion.
Source: (StackOverflow)
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 duplication.
So imagine I have the following Module
public class ProductionModule implements Module {
public void configure(Binder binder) {
binder.bind(InterfaceA.class).to(ConcreteA.class);
binder.bind(InterfaceB.class).to(ConcreteB.class);
binder.bind(InterfaceC.class).to(ConcreteC.class);
}
}
And in my test I only want to override InterfaceC, while keeping InterfaceA and InterfaceB in tact, so I'd want something like:
Module testModule = new Module() {
public void configure(Binder binder) {
binder.bind(InterfaceC.class).to(MockC.class);
}
};
Guice.createInjector(new ProductionModule(), testModule);
I've also tried the following, with no luck:
Module testModule = new ProductionModule() {
public void configure(Binder binder) {
super.configure(binder);
binder.bind(InterfaceC.class).to(MockC.class);
}
};
Guice.createInjector(testModule);
Does anyone know if it's possible to do what I want or am I completely barking up the wrong tree??
--- Follow up:
It would seem I can achieve what I want if I make use of the @ImplementedBy tag on the interface and then just provide a binding in the test case, which works nicely when there is a 1-1 mapping between the interface and implementation.
Also, after discussing this with a colleague it would seem we'd head down the road of overriding an entire module and ensuring we have our modules defined correctly. This seems like it might cause a problem though where a binding is misplaced in a module and needs to be moved, thus possibly breaking a load of tests as bindings may no longer be available to be overriden.
Source: (StackOverflow)
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 doesn't play nicely with Guice's own dependency injection.
A few examples which I've found don't go far enough to elucidate:
Iqbalyusuf's post on Jersey + Guice on Google App Engine Java suffers from a lot of boilerplate (manually getting and calling the injector). I want binding and injection should happen behind the scenes via Guice annotations.
Jonathan Curran's article Creating a RESTful service with Jersey, Guice, and JSR-250 gave me hope because it's much more current (2010), but went no further than showing how to start up a Jersey service inside of a Guice ServletModule. However, there are no examples of doing any real dependency injection. I suppose that was left as an exercise for the reader. Curran's post may in fact be the correct first step towards wiring up Guice and Jersey and so I plan on starting with that.
tantalizingly James Strachan writes:
JAX-RS works well with dependency
injection frameworks such as Spring,
Guice, GuiceyFruit or JBossMC - you
can basically pick whichever one you
prefer.
But I see no evidence that is true from a practitioner's point of view.
What I find lacking are practical examples and explanations on how to combine JAX-RS and Guice annotations. For instance:
- I believe I cannot use constructor injection with any resource as Jersey wants to control this
- I'm uncertain whether I can combine @Inject with @PathParam, @QueryParam, et al.
- How to use injection in a MessageBodyWriter implementation
Does anyone have examples, preferably with source, of non-trivial application which combines Jersey and Guice without sacrificing one or the other in the process? I'm keeping on this road regardless, but the bits and pieces on the Jersey and Guice lists makes me think I'm repeating the work of others who came before me.
Source: (StackOverflow)