users@hk2.java.net

Re: Binding service instance reports PerLookup scope in its descriptor

From: john wells <john.wells_at_oracle.com>
Date: Wed, 25 Nov 2015 13:04:34 -0500

This is because when you bind something with an instance of a Class like
in this case you are binding it as a "constant". Constant services
always return the same value (the value passed into the method) and will
always be considered PerLookup since that Context uses the least overhead.

On 11/25/2015 12:31 PM, Filip Krikava wrote:
> Hi,
>
> I’m wondering why a service that is added to a binder using the `AbstractBinder.bind(T service)` method does not report Singleton as its scope, but instead PerLookup. On the other hand, the service indeed acts as singleton.
>
> Here is the test:
>
> ```
> public class ScopeTest {
>
> @Service
> static class MyService {
> }
>
> @Test
> public void testScope() {
>
> final MyService service = new MyService();
>
> Binder binder = new AbstractBinder() {
> @Override
> protected void configure() {
> bind(service);
> }
> };
>
> final ServiceLocator sl = ServiceLocatorUtilities.bind(binder);
>
> final ServiceHandle<MyService> handle = sl.getServiceHandle(MyService.class);
>
> // EXPECTED
> assertEquals(handle.getActiveDescriptor().getScopeAnnotation(), Singleton.class);
> // INSTEAD
> assertEquals(handle.getActiveDescriptor().getScopeAnnotation(), PerLookup.class);
>
> final MyService s1 = sl.getService(MyService.class);
> assertEquals(s1, service);
> final MyService s2 = sl.getService(MyService.class);
> assertEquals(s2, service);
> }
> }
> ```
>
> Thanks a lot!
> Filip