问题
Lets say I have a component A and component B. In component A, html file I am writing
data-sly-use.head="com.Mymodel"
now in Component B also I need to use the same class, so in html file I have written
data-sly-use.head="com.Mymodel"
Component A and Component B can be available in a page or may not be available at a time. What I want is that if both Component A and B present in a page then I do not want to initialise Sling model (com.Mymodel) twice. I would like to initialise only one time. How to do it?
回答1:
Take a look at Sling Model Caching. This does exactly what you want, as long as it is adaptable from Resource.
https://sling.apache.org/documentation/bundles/models.html#caching
You only have to specify cache=true
in the Model-annotation.
@Model(adaptable = SlingHttpServletRequest.class, cache = true)
public class ModelClass {
...
}
来源:https://stackoverflow.com/questions/53204646/how-to-call-sling-model-only-once