Google Guice User Area

Just quickly ask if I understand the area correctly.

I understand that scope is the control of how / when an instance is created. Now I have a class in the application, and I want two instances of it; each of them will be introduced into some other instance.

Is it possible in Guice to create two areas for each instance? and then I can insert each instance of the scope into any other instance, how do I like it?

+4
source share
1 answer

Areas of use:

  • Areas allow you to control the life cycle of an object.

  • You can only bindScope() annotate the scope. The scope concept is the injector level: wiring configuration.

  • You can associate several things (keys) in scope.

  • You can define as many areas as you want, but each snap can only be in one area.

You did not provide detailed information about your problem, but, in my experience, in most cases, when the user area is seen as correct at first, it is rarely really!

Perhaps you want to annotate two instances of the same type? Sort of:

 bind(SomeService.class).in(First.class).to(FirstServiceImpl.class); bind(SomeService.class).in(Second.class).to(SecondServiceImpl.class); 

Then you can enter the one you want:

 @Inject SomeConstructor(@First SomeService service) { } 

or

 @Inject SomeConstructor(@Second SomeService service) { } 

If this does not help, you may need to provide more detailed information about your problem.

+5
source

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


All Articles