OSGi服务
OSGi服务是一个Java类或服务接口,以及许多作为名称/值对的服务属性。 服务属性区分使用相同服务接口提供服务的不同服务提供商。
OSGi服务由其服务接口语义定义,并作为服务对象实现。 服务的功能由其实现的接口定义。 因此,不同的应用程序可以实现相同的服务。 服务接口允许捆绑包通过绑定接口而不是实现进行交互。 指定服务接口时应尽可能少地提供实现详细信息。
定义接口
一个简单接口,通过一种方法将数据与 XDP 模板。
package com.mysite.samples;
import com.adobe.aemfd.docmanager.Document;
public interface MyfirstInterface
{
public Document mergeDataWithXDPTemplate(Document xdpTemplate, Document xmlDocument);
}
实施接口
创建一个名为的新包 com.mysite.samples.impl
保存接口的实现。
package com.mysite.samples.impl;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.aemfd.docmanager.Document;
import com.adobe.fd.output.api.OutputService;
import com.adobe.fd.output.api.OutputServiceException;
import com.mysite.samples.MyfirstInterface;
@Component(service = MyfirstInterface.class)
public class MyfirstInterfaceImpl implements MyfirstInterface {
@Reference
OutputService outputService;
private static final Logger log = LoggerFactory.getLogger(MyfirstInterfaceImpl.class);
@Override
public Document mergeDataWithXDPTemplate(Document xdpTemplate, Document xmlDocument) {
com.adobe.fd.output.api.PDFOutputOptions pdfOptions = new com.adobe.fd.output.api.PDFOutputOptions();
pdfOptions.setAcrobatVersion(com.adobe.fd.output.api.AcrobatVersion.Acrobat_11);
try {
return outputService.generatePDFOutput(xdpTemplate, xmlDocument, pdfOptions);
} catch (OutputServiceException e) {
log.error("Failed to merge data with XDP Template", e);
}
return null;
}
}
注释 @Component(...)
第10行将此Java类标记为OSGi组件,并将其注册为OSGi服务。
此 @Reference
注释是OSGi声明性服务的一部分,用于注入 输出服务 到变量中 outputService
.
生成并部署捆绑包
- 打开 命令提示符窗口
- 导航到
c:\aemformsbundles\mysite\core
- 执行命令
mvn clean install -PautoInstallBundle
- 上述命令将自动构建捆绑包,并将其部署到在localhost:4502上运行的AEM实例
该捆绑包还将在以下位置提供 C:\AEMFormsBundles\mysite\core\target
. 也可以使用将捆绑包部署到AEM中。 Felix Web控制台。
使用服务
现在,您可以在JSP页中使用服务。 以下代码片段显示了如何获取对服务的访问权限,以及如何使用由服务实施的方法
MyFirstAEMFormsService myFirstAEMFormsService = sling.getService(com.mysite.samples.MyFirstAEMFormsService.class);
com.adobe.aemfd.docmanager.Document generatedDocument = myFirstAEMFormsService.mergeDataWithXDPTemplate(xdp_or_pdf_template,xmlDocument);
包含JSP页的示例包可以是 从此处下载
测试包
使用将包导入并安装到AEM中 包管理器
使用Postman进行POST调用并提供输入参数,如下面的屏幕快照中所示
后续步骤
recommendation-more-help
8de24117-1378-413c-a581-01e660b7163e