Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

@Reference service always null in BundleListener class

Avatar

Level 5

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!

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

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

View solution in original post

6 Replies

Avatar

Level 5

Yes they are.

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

Screen Shot 2017-08-23 at 13.12.23.png

Avatar

Employee

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

There should be the reason displayed why the service is null

Avatar

Level 5
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.

Avatar

Correct answer by
Employee Advisor

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

Avatar

Level 5

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