Skip to main content
Level 4
Feb 13, 2018 5:20 PM UTC
Solved

@Reference annotation not working

I am missing something here. I am creating a new class and waht to have the resource resolver factory loaded using the @Reference annotation.

But it is not working and I am always getting an NPE when I use it.

Would someone help me to understand why? What am I missing?

Thanks.

@Component(metatype = false, immediate = true)

@Service(value = TestComponent.class)

@Properties({ @org.apache.felix.scr.annotations.Property(name = Constants.SERVICE_DESCRIPTION, value = "TestComponent"),

        @org.apache.felix.scr.annotations.Property(name = Constants.SERVICE_VENDOR, value = GeneralConstants.VENDOR),

        @org.apache.felix.scr.annotations.Property(name = "process.label", value = "TestComponent") })

public class TestComponentImpl implements TestComponent {

    protected static final Logger LOG = LoggerFactory.getLogger( TestComponent.class);

    @Reference( policy = ReferencePolicy.STATIC )

    private ResourceResolverFactory resolverFactory;

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
joerghoh
joerghohAccepted solution
Adobe Employee
Feb 14, 2018 8:02 PM UTC

Hi,

resourceResolver = new ReadServiceImpl().getResourceResolver(); 

That's the problem. You are constructing the ReadServiceImpl via Constructor, and in that way SCR cannot inject any references. You should make the ReadServiceImpl a service and let SCR manage its lifecycle.

Jörg

22 replies

smacdonald2008
Level 10
Feb 13, 2018 5:29 PM UTC

@Reference works - we have a lot of HELPX articles that show its use. See this one where we use it with Declarative Services annotations:

Adobe Experience Manager Help | Creating an Experience Manager 6.3 Page using the Page Manager API

For info on DS annotations - see: Official OSGi Declarative Services Annotations in AEM - Adobe Experience Manager | AEM/CQ | Apache Sling

Translate
smacdonald2008
Level 10
Feb 13, 2018 5:30 PM UTC

I have personally never used the extra syntax you have:

( policy = ReferencePolicy.STATIC )

Translate
Level 4
Feb 13, 2018 6:18 PM UTC

I know it works. We have many instances where it is working. I just don't understand why it isn't in this case.

Translate
smacdonald2008
Level 10
Feb 13, 2018 6:21 PM UTC

Try same code on a fresh instance that will tell us if issue is with code or AEM instance.

Translate
joerghoh
Adobe Employee
Adobe Employee
Feb 13, 2018 8:49 PM UTC

Does the reference show up at the OSGI console (/system/console/components) for this component?

Jörg

Translate
Level 4
Feb 14, 2018 12:24 AM UTC

Yes. The component status is active and the resource resolver factory is satisfied.

Reference resolverFactory["Satisfied","Service Name: org.apache.sling.api.resource.ResourceResolverFactory","Cardinality: 1..1","Policy: static","Policy Option: reluctant","Bound Service ID 981 (Apache Sling Resource Resolver Factory)"]
Translate
joerghoh
Adobe Employee
Adobe Employee
Feb 14, 2018 12:58 PM UTC

Ok, how does the code look you are using?

Jörg

Translate
Hemant_arora
Level 8
Feb 14, 2018 12:59 PM UTC

Please share logs

Translate
Level 3
Feb 14, 2018 1:47 PM UTC

ReferencePolicy.STATIC is the default policy for any referenced service

Translate
Level 4
Feb 14, 2018 1:58 PM UTC

Can you give me an idea of what you are looking for in the logs?

Translate