Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

What is the difference between @inject and @Valuemapvalue.

Avatar

Level 3

I don't think both are equivalent. I was getting an exception with @ValuemapValue but it is gone when I used @inject 

Could you please explain the difference?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Lets explore with an example of @inject v/s @ValueMapValue

@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




Aanchal Sikka

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Lets explore with an example of @inject v/s @ValueMapValue

@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




Aanchal Sikka

Avatar

Community Advisor

Hi @vineetham123 

Kindly go through this https://cqdump.joerghoh.de/2022/11/28/sling-model-performance/ by @joerghoh 
This is best documentation I've come across this topic so far.

Regards,
Arpit Varshney