接口
package org.employee.employee.core.admin;
import org.apache.sling.api.resource.ResourceResolver;
public interface CustomerService {
public String getCustomerData();
}
服务类==>获取resolverFactory null
package org.employee.employee.core.admin;
import java.util.HashMap;
import java.util.Map;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype = true, immediate = true)
@Service
public class CustomerServiceImpl implements CustomerService {
protected final Logger log = LoggerFactory.getLogger(this.getClass());
@Reference
private ResourceResolverFactory resolverFactory;
public String getCustomerData() {
Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "CustomerService");
ResourceResolver resolver = null;
try {
resolver = resolverFactory.getServiceResourceResolver(param);
System.out.println(resolver);
} catch (LoginException loginExcp) {
log.error("Exception while getting resource resolver." + loginExcp);
}
return resolver.toString();
}
}
Service Component显示state => Active
UserMapper
我正在访问它
CustomerService customerService = new CustomerService();
String resolver = customerService.getCustomerData();
这是不正确的解决方案
@Reference
private CustomerService customerService;
String resolver = customerService.getCustomerData();
另外我相信您不应该使用admin用户(如屏幕截图所示)来访问服务,因为开发了Service映射以防止对存储库的无限制访问。
而是创建一个新的系统用户,并仅为用户提供该服务所需的权限。
要创建新系统用户,您可以按照以下步骤操作:
请参阅此helpx以获取完整步骤。 https://helpx.adobe.com/in/experience-manager/6-3/sites/administering/using/security-service-users.html
另一个SO答案证明了这一点:ResourceResolverFactory getServiceResourceResolver throws Exception in AEM 6.1
ACS Commons的这个工具对确保服务用户存在非常有帮助。 https://adobe-consulting-services.github.io/acs-aem-commons/features/ensure-service-users/index.html