Expand my Community achievements bar.

Join us in celebrating the outstanding achievement of our AEM Community Member of the Year!

Clarification Needed on When to Use @OsgiInjector in Sling Models

Avatar

Level 1

Hello Experience League Community,

I’m currently working with Sling Models and came across the @OsgiInjector annotation in the documentation. I am curious to understand its specific use and when it should be applied.

I am attaching the link to the documentation for reference:

https://sling.apache.org/documentation/bundles/models.html

3 Replies

Avatar

Community Advisor

@purushottam_kr 

 

I found this old article(2022) from @Jörg_Hoh   where there is a little explanation of OsgiInjector(I never used this one though)

https://cqdump.joerghoh.de/2022/11/28/sling-model-performance/

 

There is this cheat sheet from source code which is very useful AEM sling injector annotations which I usually use.

https://sourcedcode.com/blog/aem/aem-sling-model-injectors-annotations-cheat-sheet-reference-guide

Avatar

Level 6

Hi @purushottam_kr , to my understanding we avoid @inject annotation because of its unpredictable behaviour and performance overhead.
So, instead of @inject it is recommended to use the @OSGiService

@OSGiInjector is conceptually similar to @inject or @OSGiService but with additional metadata i.e. it provides more granular control over how OSGi services are injected.

 

@OSGiInjector(name="log")

 you can provide the name so that in case it can distinguish between the two services which implements the same interface.

 

@OsgiInjector(filter="paths=/bin/something")
private List<Servlet> servlets;

In this case, the filter paths=/bin/something likely matches services (e.g., Servlet implementations) registered in the OSGi registry with a property paths having the value /bin/something. List<Servlet> means that all matching services are injected as a list.

Avatar

Employee Advisor

It allows you to inject also OSGI services into your Sling Models.