@Reference service always null in BundleListener class | Community
Skip to main content
New Participant
August 23, 2017
Solved

@Reference service always null in BundleListener class

  • August 23, 2017
  • 6 replies
  • 2187 views

I am having issues trying to access a service of mine in a BundleListener. The service (LanguageService) works in different parts of my code outside of the BundleListener (for example my rest endpoints).

Activator:

public class Activator implements BundleActivator {

    @Override

    public void start(BundleContext bundleContext) throws Exception {

        bundleContext.addBundleListener(new PlatformCoreBundleListener());

    }

    ...

}

BundleListener:

@Component(immediate = true)

public class PlatformCoreBundleListener implements BundleListener {

    private static final Logger LOG = LoggerFactory.getLogger("AsAdventure");

    @Reference

    private LanguageService languageService;

    @Override

    public void bundleChanged(BundleEvent bundleEvent) {

        String symbolicName = bundleEvent.getBundle().getSymbolicName();

        if (symbolicName.equalsIgnoreCase(BundleConstants.SYMBOLIC_AEM_PLATFORM_CORE_BUNDLE)) {

            if (bundleEvent.getType() == BundleEvent.STARTED) {

                LOG.info("[PlatformCoreBundleListener]: " + BundleConstants.SYMBOLIC_AEM_PLATFORM_CORE_BUNDLE + " has started.");

            }

        }

    }

}

The languageService is always null even when I take a look at it in debug mode while the bundleEvent has a type of 'STARTED'.

What am I doing wrong? Thanks in advance!

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by joerghoh

Hi,

In the Activator class you create a PlatformCoreBundleListener through the use of the constructor ("new ..."). In that case the object is not constructed by the SCR runtime, which would handle all the injections.

You should get a reference to the PlatformCoreBundleListener instead.

But:I am not sure if this works either. IIRC the bundle activator is called by the OSGI framework itself and not by instantiated by the SCR framework. And then the @Reference injections are not handled as well.

That's the reason why I hardly use Bundle activators.

regards,
Jörg

6 replies

New Participant
August 24, 2017

Thanks! Explains a lot. Will need to find another method to provide the set of default permission for my system-user.

joerghoh
joerghohAccepted solution
Employee
August 23, 2017

Hi,

In the Activator class you create a PlatformCoreBundleListener through the use of the constructor ("new ..."). In that case the object is not constructed by the SCR runtime, which would handle all the injections.

You should get a reference to the PlatformCoreBundleListener instead.

But:I am not sure if this works either. IIRC the bundle activator is called by the OSGI framework itself and not by instantiated by the SCR framework. And then the @Reference injections are not handled as well.

That's the reason why I hardly use Bundle activators.

regards,
Jörg

New Participant
August 23, 2017
com.company.core.service.PlatformCoreBundleListener

Bundleaem-company-platform-core-bundle (520)
Implementation Classcom.company.core.service.PlatformCoreBundleListener
Default Stateenabled
Activationimmediate
Configuration Policyoptional
Services
PIDcom.company.core.service.PlatformCoreBundleListener
Reference languageServiceSatisfied
Service Name: com.company.core.service.LanguageService
Cardinality: 1..1
Policy: static
Policy Option: reluctant
Bound Service ID 26620 (com.company.core.service.LanguageService)
Propertiescomponent.id = 9272
component.name = com.company.core.service.PlatformCoreBundleListener
service.pid = com.company.core.service.PlatformCoreBundleListener

Everything seems to be ok here.

Btw my original use case was to provide a set of permission for the System-user.

Feike_Visser1
Employee
August 23, 2017

Can you go into "components" of the system/console and lookup your component?

There should be the reason displayed why the service is null

New Participant
August 23, 2017

Yes they are.

Example of me disabling the acs commons bundle while in debug mode:

Feike_Visser1
Employee
August 23, 2017

are all the bundles active?