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

Adobe Summit 2023 [19th to 23rd March, Las Vegas and Virtual] | Complete AEM Session & Lab list
SOLVED

Sling Annotations

Avatar

Level 2

Which annotation is prefered to access the Service inside another service and why? @reference or @OSGiService.

 

Thanks

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @madhurmadan2296,

On OSGi service level you should use Reference annotation. So this will be option you should use in case you want to get reference of other OSGi service inside another.

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

@Component
public class YourService {
   
   @Reference
   private OtherService otherService;
}

 

OSGiService annotation is dedicated for Sling Model it will not work inside OSGi service/component. You can use it, in case you would like to inject OSGi Service into your Sling Model.

See also below links:

1 Reply

Avatar

Correct answer by
Community Advisor

Hi @madhurmadan2296,

On OSGi service level you should use Reference annotation. So this will be option you should use in case you want to get reference of other OSGi service inside another.

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

@Component
public class YourService {
   
   @Reference
   private OtherService otherService;
}

 

OSGiService annotation is dedicated for Sling Model it will not work inside OSGi service/component. You can use it, in case you would like to inject OSGi Service into your Sling Model.

See also below links: