Expand my Community achievements bar.

SOLVED

Service returning null when calling from model.

Avatar

Level 2

Hi,
whenever I tried to call my service, from model class I am getting null. I tried annotating with @reference.

This is how I created the service @component (immediate = true, service = DashboardService.class), can anyone help me with this.

Thanks.

1 Accepted Solution

Avatar

Correct answer by
Level 3

Hi @HariniVR 

In your service class try 

@component (immediate = true, service = {DashboardService.class}, enabled=true)

After building and installing the code package at AEM, browse system/console/configMgr and confirm service is available at console.

In case service is not available probably the service is failing to initialize because of some other error. Check error logs to see if there is any failure related to your DashboardService

View solution in original post

4 Replies

Avatar

Level 9

try using @OSGIService in sling model

@OSGiService
MyServiceClass myService

Avatar

Correct answer by
Level 3

Hi @HariniVR 

In your service class try 

@component (immediate = true, service = {DashboardService.class}, enabled=true)

After building and installing the code package at AEM, browse system/console/configMgr and confirm service is available at console.

In case service is not available probably the service is failing to initialize because of some other error. Check error logs to see if there is any failure related to your DashboardService

Avatar

Community Advisor

How your sling model are adapted is it via Resource or slingHttpServletRequest ?

Try with @inject if you’re sling models are adaptable via Resource.class

Avatar

Community Advisor

@HariniVR ,

 

As other pointed out, @reference is only used in Sling Servlets, OSGI Services and Schedulers.

Use @OSGiService annotation to inject your custom OSGI Service into Sling Model.

Make sure your OSGI Service is up and running by visting /system/console/component in the OSGI console.