What is the right stage of using Google Guice in production on an application server?

Sounds like a weird question (the obvious answer was Production, duh), but if you are reading java docs:

/** * We want fast startup times at the expense of runtime performance and some up front error * checking. */ DEVELOPMENT, /** * We want to catch errors as early as possible and take performance hits up front. */ PRODUCTION 

Assuming a scenario in which you have a stateless call on the application server, the initial method of getting (or abouts there) creates a new injector for each call. If in this call not all module bindings are needed, then it would be better to use the development phase (which is standard) and not take a surge in performance, because you can never accept it at all, and here is the difference between "upfront" and "runtime performance "is quite controversial, as this is one challenge.

Of course, the disadvantage of this may be that you lose error checking by causing potential code paths to cause an unexpected problem.

So, the question is that the assumptions in the rule are correct? Will you save performance on a large set of modules when a given injector life is one challenge?

+4
source share
1 answer

No need to create an injector for each request. This is not what Guice is supposed to be for use - you only need one injector for each application. The injector really represents the configuration or wiring of the application, not the short circuit condition.

I suspect you need to learn Guice Scopes .

GuiceServlet provides you with @RequestScoped , which allows you to limit the lifetime of an object with an HTTP request, which sounds like you want to do.

If you are not in the servlet, you can always define your own custom scope . It is not very difficult.

+4
source

Source: https://habr.com/ru/post/1304416/


All Articles