I have recently come across a piece of code which is something like:
for(int i=0;i<10000;i++) {
....
PageManager manager = resourceResolver.adaptTo(PageManager.class);
....
}
Does it mean 10000 PageManager objects will be created or do we get the same object each time?
Thanks
Have a look at the Adaptable documentation hope it answer your question
it is explicitly left as an implementation detail whether each call to this method with the same type yields the same object or a new object on each call.
similar question purpose-of-resourceresolver-adapttosession
Just to explain in simple
SlingHttpServletRequest.getResourceResolver()
method. A resource resolver can also be created through the ResourceResolverFactory
.So when you get the resource by ResourceResolver and adaptTo other types or some representations of the object, the object will be same. Remember that the operations which you are performing on a resource (after adaptTO()
) should be taken care as the resource is generally not thread safe. Example the resolver is updated to reflect the latest state by using refresh()
method, etc.