users@hk2.java.net

Binding service instance reports PerLookup scope in its descriptor

From: Filip Krikava <filip.krikava_at_oracle.com>
Date: Wed, 25 Nov 2015 18:31:51 +0100

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