gpt4 book ai didi

java - ResourceResolverFactory 为 NULL (Adobe Experience Manager AEM)

转载 作者:搜寻专家 更新时间:2023-11-01 02:43:54 26 4
gpt4 key购买 nike

我正在尝试从 ResourceResolverFactory 获取对 ResourceResolver 的引用,如下所示:

@Reference
private ResourceResolverFactory resourceResolverFactory;

public void someMethod() {
Map<String, Object> authenticationMap = new HashMap<String, Object>();
authenticationMap.put(ResourceResolverFactory.USER, "user");
authenticationMap.put(ResourceResolverFactory.PASSWORD, "pwd");

//This line returns NullPointerException
ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(authenticationMap);
}

有人可以告诉我我做错了什么吗? AEM API 版本 v6.0。

最佳答案

所以我所做的是创建一个 Activator 类,在部署和启动包时调用它。 Activator 类然后获取 org.apache.sling.jcr.api.SlingRepository 的实例,我们可以使用它来连接到 JCR。这是激活码:

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(immediate = true, label = "Commons Activator")
public class Activator implements BundleActivator {

@Reference
private SlingRepository repository;

private static final Logger logger = LoggerFactory.getLogger(Activator.class);

@Activate
@Override
public void start(BundleContext context) throws Exception {
logger.info(context.getBundle().getSymbolicName() + " started");

//My own factory class instance
ResourceResolverDiscoveryService rrf = ResourceResolverDiscoveryService.getInstance();
//Set the 'repository' in your factory class instance
rrf.setSlingRepositoryFactory(repository);
}

@Deactivate
@Override
public void stop(BundleContext context) throws Exception {
logger.info(context.getBundle().getSymbolicName() + " stopped");
}

}

然后在我想使用 JCR 存储数据的类中,我执行了以下操作:

public class StoreInJCR {
public void store(Quote quote) throws LoginException, RepositoryException {
SlingRepository slingRepository = ResourceResolverDiscoveryService.getInstance().getSlingRepositoryFactory();

// GOT IT!!! Mission Accomplished
Session session = slingRepository.loginAdministrative(null);
Node root = session.getRootNode();
// Further code
.
.
}
}

希望有人觉得这有用。

关于java - ResourceResolverFactory 为 NULL (Adobe Experience Manager AEM),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27005919/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com