Get a Guice Injector in a Web App

I am looking at how I can get an Injector using Guice in a web application. I already found a solution using ServletContext, but I am not very satisfied with this solution because it destroys the application level architecture. I cannot use ServletContext in deeper layers of the application. Do you know another way?

The obvious solution would be to create my own singleton to house the Injector, but it looks like Guice should offer some of the ready-made ways to do this. I just can't find it yet ...

+5
source share
2 answers

Assuming you are using a Guice Servlet and assuming the class you want the injector to be injected into, just enter the injector.

+6
source

The application life cycle is completely controlled by the servlet container, so using a context listener to start the process of initializing your application is the logical result of this fact. Thus, I would not say that this fact alone does not violate the layered architecture, just as launching a web request at the model level does not destroy the layered architecture.

, , , . , Guice , , Guice factory, . , (: MyGuiceFactory.init() MyApp.init()).

+1

All Articles