@Reference annotation not working | Community
Skip to main content
New Participant
February 13, 2018
Solved

@Reference annotation not working

  • February 13, 2018
  • 22 replies
  • 11318 views

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.
Best answer by joerghoh

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

New Participant
February 14, 2018

I started to, but there are dependencies that are not satisfied for our primary bundle. So at the moment I have suspended that approach.

smacdonald2008
New Participant
February 14, 2018

Did you try running this code on a Fresh AEM DEV instance so we can rule out if its your code or the instance. Please do so and let us know the results.

New Participant
February 14, 2018

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

New Participant
February 14, 2018

ReferencePolicy.STATIC is the default policy for any referenced service

Hemant_arora
New Participant
February 14, 2018

Please share logs

joerghoh
Employee
February 14, 2018

Ok, how does the code look you are using?

Jörg

New Participant
February 14, 2018

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)"]
joerghoh
Employee
February 13, 2018

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

Jörg

smacdonald2008
New Participant
February 13, 2018

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

New Participant
February 13, 2018

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