0
@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 处传递任何参数。请举一些例子来访问存储库。

4

2 回答 2

0

查看 ResourceResolverFactory 的文档。

https://sling.apache.org/apidocs/sling7/org/apache/sling/api/resource/ResourceResolverFactory.html

于 2019-01-17T16:30:23.003 回答
0

通过工厂访问时,我们应该使用系统用户访问存储库。你需要

  1. 创建系统用户,提供适当的权限
  2. 将捆绑符号名称映射到系统用户
  3. 使用系统用户通过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);
    

您也可以参考此 helpx链接文章以了解有关使用服务用户创建会话的信息

于 2019-01-17T10:54:45.037 回答