Does scoping work on Guice providers? Suppose I have a FooProvider and bind like thus:
bind(Foo.class).toProvider(FooProvider.class).inScope(ServletScopes.REQUEST)
Will the FooProvider be instantiated once per request?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No,
FooProviderwill be instantiated by Guice only once.The scope applies to the binding, which means in your example that, if
Foois injected into another REQUEST-scoped object, Guice will callFooProvider.get()and will inject the returnedFoointo that original object.If you want the scope applied to FooProvider, then you would have to do something like that (NB: I haven’t checked it but it should work):