@Component
public class AddNode {
@Reference
static ResourceResolverFactory resolverFactory;
static Session session;
public static void main(String[] args) throws Exception {
ResourceResolver resourceResolver = resolverFactory.getServiceResourceResolver(null);
session = resourceResolver.adaptTo(Session.class);
Node root = session.getRootNode();
Node adobe = root.addNode("ProgramNode");
Node day = adobe.addNode("subnode");
day.setProperty("jcr:title", "programNode");
Node node = root.getNode("ProgramNode/subnode");
System.out.println(node.getPath());
System.out.println(node.getProperty("jcr:title").getString());
session.save();
session.logout();
}
}
此处resolverFactory.getServiceResourceResolver(null);我需要在null处传递任何参数。请举一些示例以访问存储库。
答案 0 :(得分:0)
通过Factory访问时,我们应使用System Users访问存储库。您需要
使用系统用户通过ResourceResolverFactory
Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "<sub-service-name>");
ResourceResolver resolver = null;
resolver = resolverFactory.getServiceResourceResolver(param);
session = resolver.adaptTo(Session.class);
您也可以参考this helpx链接文章,以获取有关使用服务用户创建会话的想法
答案 1 :(得分:0)
看看ResourceResolverFactory的文档。
https://sling.apache.org/apidocs/sling7/org/apache/sling/api/resource/ResourceResolverFactory.html