Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get an Guice Injector in a web application

I'm looking how I could get an Injector using Guice in a web application. I already found a solution using ServletContext, but I'm not really satisfied by this solution, because it breaks the layer architecture of the application. I'm not okay with using a ServletContext in the deeper layers of the app. Do you know another way?

An obvious solution would be to create my own singleton to host the Injector, but it seems to be that Guice should offer some out-of-the-box way to do this. I just can't find one yet...

like image 824
Alexis Dufrenoy Avatar asked Apr 04 '11 13:04

Alexis Dufrenoy


1 Answers

Assuming you're using Guice Servlet and assuming the class you want the Injector in is injected itself, just inject the Injector.

like image 67
ColinD Avatar answered Sep 28 '22 05:09

ColinD