Hi,
I was going through this article https://sling.apache.org/documentation/bundles/models.html#field-injection to understand models in aem.
How does the injection of values really work? The documentation says that : In this case, a property named propertyName will be looked up from the Resource (after first adapting it to a ValueMap) and it is injected. Fields can use any visibility modifier:
So does the resource first adapted to ValueMap and then injected?
if anyone has any detailed explanation or understanding of what happens behind the scene, could you please explain?
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @sindhusr
The resource is first adapted to a `ValueMap` and then injected into the model class. Here's a more detailed explanation of what happens behind the scenes:
When a model class is instantiated, Sling creates a new instance of the class and injects the values of its fields using the `@Inject` annotation. The injection process works as follows:
1. Sling looks up the resource that corresponds to the model instance. This is typically done using the `@Self` annotation, which tells Sling to inject the resource that corresponds to the model instance.
2. Sling adapts the resource to a `ValueMap`. A `ValueMap` is a map-like interface that provides access to the properties of a resource.
3. Sling looks for fields in the model class that are annotated with `@Inject` and have a name that matches a property in the `ValueMap`. For example, if a field is named `propertyName` and there is a property in the `ValueMap` with the same name, Sling will inject the value of that property into the field.
4. If the field is of a different type than the property in the `ValueMap`, Sling will attempt to convert the property to the appropriate type. For example, if the field is of type `int` and the property in the `ValueMap` is a string, Sling will attempt to convert the string to an integer.
5. If there is no property in the `ValueMap` that matches the name of the field, Sling will not inject a value into the field.
6. If the field is not annotated with `@Inject`, Sling will not inject a value into the field.
So, to answer your question, yes, the resource is first adapted to a `ValueMap` and then injected into the model class. This allows the model class to access the properties of the resource using a simple field-based syntax.
Thanks.
Hello @sindhusr
Apache Sling Models allow you to map resource properties, OSGi services, Sling objects and more onto Java POJOs, leveraging annotation-driven injection.
In the process of field injection in Sling Models, yes, the Resource is indeed first adapted to a ValueMap and then the designated property is injected.
Sling Models Works like this:
Thanks,
Venkat
The entire sequence unfolds as follows:
new MyModel()
, is generated by Sling through the adaptation of the current resource.@Inject
, @ValueMapValue
, @OSGiService
, among others.
@PostConstruct
.Reference: https://techrevelhub.wordpress.com/2017/03/18/sling-model-annotations/#PostConstruct
To understand Adapter Pattern in detail, please refer to https://experienceleague.adobe.com/docs/experience-manager-cloud-service/content/implementing/develo...
Hi @sindhusr
The resource is first adapted to a `ValueMap` and then injected into the model class. Here's a more detailed explanation of what happens behind the scenes:
When a model class is instantiated, Sling creates a new instance of the class and injects the values of its fields using the `@Inject` annotation. The injection process works as follows:
1. Sling looks up the resource that corresponds to the model instance. This is typically done using the `@Self` annotation, which tells Sling to inject the resource that corresponds to the model instance.
2. Sling adapts the resource to a `ValueMap`. A `ValueMap` is a map-like interface that provides access to the properties of a resource.
3. Sling looks for fields in the model class that are annotated with `@Inject` and have a name that matches a property in the `ValueMap`. For example, if a field is named `propertyName` and there is a property in the `ValueMap` with the same name, Sling will inject the value of that property into the field.
4. If the field is of a different type than the property in the `ValueMap`, Sling will attempt to convert the property to the appropriate type. For example, if the field is of type `int` and the property in the `ValueMap` is a string, Sling will attempt to convert the string to an integer.
5. If there is no property in the `ValueMap` that matches the name of the field, Sling will not inject a value into the field.
6. If the field is not annotated with `@Inject`, Sling will not inject a value into the field.
So, to answer your question, yes, the resource is first adapted to a `ValueMap` and then injected into the model class. This allows the model class to access the properties of the resource using a simple field-based syntax.
Thanks.
@sindhusr Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.
Views
Replies
Total Likes
Views
Likes
Replies