javaaemcrx

ServiceReference not fetching the all class names for activation


I'm using the below code to fetch the all reference classes in-order to activate it :

String filter = "(component.factory=com.requestclasses.mypack)";
bundleContext = componentContext.getBundleContext();
ServiceReference[] references = bundleContext.getAllServiceReferences(ComponentFactory.class.getCanonicalName(), filter);

But in reference i'm not getting all the classes.I'm not sure whether the issue is with code or AEM 5.6 . Any suggestions ?


Solution

  • I generally wouldn't recommend querying for service references using low level APIs like these.

    You might be better off retrieving those references using the @Reference annotation like so:

    @Reference(
       name = "componentFactories",
       referenceInterface = ComponentFactory.class,
       cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE,
       policy = ReferencePolicy.DYNAMIC
    )
    protected List<ComponentFactory> componentFactories = new ArrayList<ComponentFactory>();