开发者

What is the correct stage to use for Google Guice in production in an application server?

It seems like a strange question (the obvious answer would Production, duh), but if you read the 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 where you have a stateless call to an application server, the initial receiving method (or there abouts) creates the injector new every call. If there all of the module bindings are not needed in a given call, then it would seem to have been better to use the Development stage (which is the default) and not take the performance hit up开发者_如何学Cfront, because you may never take it at all, and here the distinction between "upfront" and "runtime performance" is kind of moot, as it is one call.

Of course the downside of this would appear to be that you would lose the error checking, causing potential code paths to cause a problem by surprise.

So the question boils down to are the assumptions in the above correct? Will you save performance on a large set of modules when the given lifetime of an injector is one call?


There should be no need to create an Injector for every request. That's not really how Guice is intended to be used - you should only need one Injector per application. An injector is really represents the configuration or wiring of an application, not a short-lived state.

I suspect you need to look into using Guice Scopes.

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

If you're not in a Servlet, you can always define your own custom scope. It's not very complex.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜