I encountered (at least for me) unexpected behaviour in SCR concerning prototype services. I thought it was only possible to create instances of the prototype service by calling getService() on the respective ComponentServiceObjects-reference.
However look at the following example:
I defined two service interfaces:
public interface MyProtoService {}
public interface AnotherService {}
Then I defined a service prototype implementing both interfaces and providing them as OSGi services.
@Component(service = { MyProtoService.class, AnotherService.class }, scope = ServiceScope.PROTOTYPE)
public class MyPrototype implements MyProtoService, AnotherService {}
Then I have another component collecting AnotherService-instances via the whiteboard pattern:
@Component
public class AnotherServiceCollector {
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC,
policyOption = ReferencePolicyOption.GREEDY)
private volatile List<AnotherService> otherServices;
}
Though I don't have a single prototype_required reference in my application, a service instance instance of MyPrototype is still created and collected in AnotherServiceCollector when I start my application.
Is this expected behaviour?
Best Regards,
Mike