Expand my Community achievements bar.

SOLVED

AEM - Sling Model

Avatar

Level 2

Hi Everyone,
Can you please provide the difference between @valuemapvalue and @inject. 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!

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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:

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

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:

Avatar

Level 3

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

Avatar

Community Advisor

@inject 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, @inject 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 @inject. This is because value injection occurs at runtime, and using @inject 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