AEM - Sling Model | Community
Skip to main content
Level 2
January 3, 2024
Solved

AEM - Sling Model

  • January 3, 2024
  • 3 replies
  • 1856 views

Hi Everyone,
Can you please provide the difference between @valuemapvalue and @586265. I have gone through some document, I don't think both are equivalent but not able to articulate exact difference. Can anyone explain same with use cases ?

Thank You!

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 lukasz-m

Hi @nayangarade,

The main difference between Inject and ValueMapValue is that Inject can be used to inject OSGi services and other dependencies into the Sling Model, while ValueMapValue is used to map JCR properties directly to Java fields in the Sling Model.

In other words ValueMapValue is specialized injector, when Inject is more common. They are not equivalent.

Examples:

The @Inject annotation is typically used to inject services and other dependencies that are not directly related to the content being mapped, but are needed for the Sling Model to function properly. For example, you might use @Inject to inject a service that generates a PDF from data in the Sling Model.

 

On the other hand, the @ValueMapValue annotation is used specifically to map content properties to Java fields in the Sling Model. For example, you might use @ValueMapValue to map a title property in the content tree to a Java String field in the Sling Model.


Additionally according to best practices you should avoid using Inject in any use case. You should always use specialized/dedicated injector. Result of using Inject in Sling Model is unpredictable and can cause performance issue.

For example if you need OSGi service instance in your Sling Model use OSGiService annotation not Inject, if you need vale from valueMap use ValueMapValue annotation, etc.

Please check below links for more information:

3 replies

lukasz-m
Community Advisor
lukasz-mCommunity AdvisorAccepted solution
Community Advisor
January 3, 2024

Hi @nayangarade,

The main difference between Inject and ValueMapValue is that Inject can be used to inject OSGi services and other dependencies into the Sling Model, while ValueMapValue is used to map JCR properties directly to Java fields in the Sling Model.

In other words ValueMapValue is specialized injector, when Inject is more common. They are not equivalent.

Examples:

The @Inject annotation is typically used to inject services and other dependencies that are not directly related to the content being mapped, but are needed for the Sling Model to function properly. For example, you might use @Inject to inject a service that generates a PDF from data in the Sling Model.

 

On the other hand, the @ValueMapValue annotation is used specifically to map content properties to Java fields in the Sling Model. For example, you might use @ValueMapValue to map a title property in the content tree to a Java String field in the Sling Model.


Additionally according to best practices you should avoid using Inject in any use case. You should always use specialized/dedicated injector. Result of using Inject in Sling Model is unpredictable and can cause performance issue.

For example if you need OSGi service instance in your Sling Model use OSGiService annotation not Inject, if you need vale from valueMap use ValueMapValue annotation, etc.

Please check below links for more information:

Santhosh_Talepalle
Level 2
January 9, 2024

Thanks for the detailed explanation. Even I had a little confusion till now, but its clarified.

aanchal-sikka
Community Advisor
Community Advisor
January 3, 2024

@586265 is a general-purpose annotation that retrieves values from various injectors, while @ValueMapValue is an injector-specific annotation designed to fetch values from the ValueMap injector.

 

When an injected value is exclusively available from a single injector, @586265 and @ValueMapValue will behave the same. However, if a property can be provided by multiple injectors (e.g., script-binding and ValueMap), they may inject different values.

 

It’s advisable to prefer injector-specific annotations like @ValueMapValue or @ChildResource over @586265. This is because value injection occurs at runtime, and using @586265 can introduce ambiguity, requiring the framework to make educated guesses. When using @ValueMapValue, especially in the context of retrieving properties from the ValueMap, the process is automatic, resulting in less code to write and increased clarity.

 

For more details on how annotations affect performance, refer to Sling Model Performance by Jörg Hoh

 

Reference: https://techrevelhub.wordpress.com/2023/10/11/sling-model-annotations-best-practices/

Aanchal Sikka