接口
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();
}
}
服务组件显示状态=>活动
UserMapper
答案 0 :(得分:0)
我的访问方式就像
CustomerService customerService = new CustomerService();
String resolver = customerService.getCustomerData();
哪个是不正确的,并且由谁解决
@Reference
private CustomerService customerService;
String resolver = customerService.getCustomerData();
答案 1 :(得分:0)
我还相信,您不应使用管理员用户(如屏幕快照中所述)来访问服务,因为开发了服务映射是为了防止对存储库的无限制访问。
相反,创建一个新的系统用户,并仅向用户授予服务所需的那些权限。
要创建新的系统用户,您可以按照以下步骤操作:
有关完整步骤,请参阅此帮助。 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