1

我有一个关于在捆绑包之间在 OSGi 中注册/使用服务的问题。我有两个捆绑包,捆绑包 A - 我的核心服务将多个“处理程序”绑定到地图(类似于工厂)和捆绑包 B - 实现额外的处理程序接口。

我的期望是其他包可以实现接口,并且它们可以注册到包 A 中的单个服务,这样当我调用该核心服务时,我可以查找适当的处理程序并处理它。我希望从其他捆绑包中提供处理程序的实现。

我正在处理这个例子:

使用 SCR 注释的多基数 OSGI @References ' http://architectslog.wordpress.com/2013/02/18/multiple-cardinality-osgi-references-using-scr-annotations

在核心包中,我的处理程序实现正确绑定。在我的其他捆绑包中,服务是通过不绑定到核心服务来注册的。

所以,我的问题是,我可以像这样在 Bundle A 中的其他包中注册不同的实现和接口吗?

Bundle A - 处理程序服务:

@Component
@Service(value = IContentService.class)
public class ContentService implements IContentService
{
    @Reference(
        referenceInterface = IContentHandler.class,
        cardinality = ReferenceCardinality.MANDATORY_MULTIPLE,
        bind = Constants.OSGI_BIND_METHOD,
        unbind = Constants.OSGI_UNBIND_METHOD,
        policy = ReferencePolicy.DYNAMIC)
    private final Map<String, IContentHandler> handlers = Maps.newHashMap();


    protected void bind(final IContentHandler handler)
    {
        final Content h = handler.getClass().getAnnotation(Content.class);
        if (h != null)
        {
            final String id = getHandlerId(h);
            this.handlers.put(id, handler);
        }
    }

Bundle A - 处理程序实现(注册良好):

@Component
@Service(value = IContentHandler.class)
@Content(name = "bean")
public class BeanContentHandler implements IContentHandler
{
    public IContentBean process(final Resource resource, final ContentOptions options) throws ContentException
    {
        ...

Bundle B - 处理程序实现(不注册):

@Component
@Service(value = IContentHandler.class)
@Content(name = "sample")
public class SampleHandler implements IContentHandler
{
    @Override
    public IContentBean process(Resource resource, ContentOptions options) throws ContentException
    {
        ...
4

0 回答 0