Menu

How to create session from resourceResolver object?

Sometimes we need a session to work with some special JCR API which we can,t use with the help of resourceResolver then we can create an instance of a session in the same class file using the object of resourceResolver. Below is the code snippet.

First, we need to inject the ResourceResolverFactory interface to create a new resourceResolver in AEM. You could inject using the SCR annotation @Reference or Sling Model annotation @Inject.
@Reference
private ResourceResolverFactory resolverFactory;
Now create the ResourceResolver object using resourceResolverFactory. This line of code will create a rsourceResolver with administrator login.
ResourceResolver resourceResolver=resolverFactory.getServiceResourceResolver(null);
Now will create the instance of Session using the object of resourceResolver.
Session session = resourceResolver.adaptTo(Session.class);

No comments:

Post a Comment