OSGi服务
Last update: Wed Mar 26 2025 00:00:00 GMT+0000 (Coordinated Universal Time)
- 主题:
- 自适应表单
创建对象:
- 初学者
- 开发人员
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;
}
}
第10行的注释@Component(...)
将此Java类标记为OSGi组件,并将其注册为OSGi服务。
@Reference
注释是OSGi声明性服务的一部分,用于将Outputservice的引用插入到变量outputService
中。
生成并部署捆绑包
- 打开 命令提示符窗口
- 导航到
c:\aemformsbundles\mysite\core
- 执行命令
mvn clean install -PautoInstallBundle
- 上述命令将自动构建捆绑包,并将其部署到在localhost:4502上运行的AEM实例
该包还将在以下位置C:\AEMFormsBundles\mysite\core\target
提供。 也可以使用Felix Web控制台将该捆绑包部署到AEM中。
使用服务
现在,您可以在JSP页中使用服务。 以下代码片段显示了如何获取对服务的访问权限,以及如何使用由服务实施的方法
MyFirstAEMFormsService myFirstAEMFormsService = sling.getService(com.mysite.samples.MyFirstAEMFormsService.class);
com.adobe.aemfd.docmanager.Document generatedDocument = myFirstAEMFormsService.mergeDataWithXDPTemplate(xdp_or_pdf_template,xmlDocument);
recommendation-more-help
8de24117-1378-413c-a581-01e660b7163e