Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

@Reference annotation not working

Tom_Fought
Level 4
Level 4

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;

1 Accepted Solution
Jörg_Hoh
Correct answer by
Employee
Employee

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

View solution in original post

0 Replies
dylanr46798176
Level 3
Level 3

Seems like I was right with my suggestion! Hopefully you can resolve this issue now by actually referencing the ReadService you referenced the DataSourceLookup!

Tom_Fought
Level 4
Level 4

Thanks for all your assistance. This has help significantly!