osgi - 多接口(interface)实现 - AEM/CQ - OSGi

标签 osgi aem sling

有服务接口(interface)HelloService ,这是由2个Service实现实现的

HelloService接口(interface)

public interface HelloService {
    public String getRepositoryName();
}

HelloServiceImpl1 实现
@Service
@Component(metatype = false)
public class HelloServiceImpl1 implements HelloService {

    @Reference
    private SlingRepository repository;

    public String getRepositoryName() {
        return repository.getDescriptor(Repository.REP_NAME_DESC);
    }
}

HelloServiceimpl2 实现
@Service
@Component(metatype = false)
public class HelloServiceImpl2 implements HelloService {

    public String getRepositoryName() {
        return "Response from HelloServiceImpl2";
    }
}

现在使用我们使用的服务
@Reference
HelloService helloService;

在所需方法内部,调用为
helloService.getRepositoryName();

我总是收到来自 HelloServiceImpl1 的回复.检查了 AEM API 中的另一个示例,SlingRepositoryAbstractSlingRepository 扩展和 AbstractSlingRepository2 ,如何在内部选择实现,因为我们只指定 @Reference SlingRepository repository;
这在 AEM OSGi 中是如何处理的?

根据响应更新

检查了语法,以下是观察结果

要使用服务排名,请使用以下服务实现
@Properties({
    @Property(name = Constants.SERVICE_RANKING, intValue = 100)
})

为此,消费没有变化,选择更高的服务排名实现,控制权在提供者手中
@Reference
HelloService helloService;

要使用目标过滤器,请使用以下注释指定属性
@Properties({
   @Property(name="type", value="Custom")
})

在基于过滤器消费时,指定目标,控制在消费者手中
 @Reference (target="(type=Custom)")
 HelloService helloService;

如果同时使用服务排名和过滤器,则过滤器优先。

最佳答案

这与Declaratives Services 如何连接@Reference 有关。从规范:

If the reference has a unary cardinality and there is more than one target service for the reference, then the bound service must be the target service with the highest service ranking as specified by the service.ranking property.

If there are multiple target services with the same service ranking, then the bound service must be the target service with the highest service ranking and the lowest service id as specified by the service.id property.



即,它取决于组件的“服务排名”。如果未指定此排名,那么您可以有任何实现(您通常会获得最旧的服务)。如果要针对特定​​实现,可以使用过滤器。

关于osgi - 多接口(interface)实现 - AEM/CQ - OSGi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36147366/

相关文章:

aem - CQ5 - 在选择 xtype 中动态填充选项

osgi - Fabric8 或 JbossFuse 中实例的自动启动

java - jersey-all (v.2.22.2) 在系统控制台中生成未解析的包 - AEM 6.2

aem - HTL/Sightly 是否可以有条件地打开或关闭标签?

java - 签名算法的算法约束检查失败 : MD5withRSA

java - Sling 可以处理 "virtual resources"吗?

recursion - 递归地列出Children

java - 使用 Spring OSGi 时,BundleActivator 无法解析为类型

java - OSGi:Import-Package/Export-Package 和Require-Capability/Provide Capability 有什么区别?

osgi - 用于关系驱动程序的 xPages OSGi 扩展库