多接口实现 - AEM/CQ - OSGi

San*_*mar 4 osgi sling aem

有一个服务接口HelloService,这是由2个服务实现实现的

HelloService接口

public interface HelloService {
    public String getRepositoryName();
}
Run Code Online (Sandbox Code Playgroud)

HelloServiceImpl1实现

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

    @Reference
    private SlingRepository repository;

    public String getRepositoryName() {
        return repository.getDescriptor(Repository.REP_NAME_DESC);
    }
}
Run Code Online (Sandbox Code Playgroud)

HelloServiceimpl2实现

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

    public String getRepositoryName() {
        return "Response from HelloServiceImpl2";
    }
}
Run Code Online (Sandbox Code Playgroud)

现在使用我们使用的服务

@Reference
HelloService helloService;
Run Code Online (Sandbox Code Playgroud)

在需要的内部方法中,调用为

helloService.getRepositoryName();
Run Code Online (Sandbox Code Playgroud)

我总是得到回应HelloServiceImpl1.经过在AEM API的另一个例子,SlingRepository是通过延长AbstractSlingRepository和AbstractSlingRepository2,如何实施内部挑选,如同时消耗我们只能指定@Reference SlingRepository repository;

如何处理AEM OSGi?


根据响应进行更新

检查了这个的语法,以下是观察

要使用服务排名,请使用以下服务实现

@Properties({
    @Property(name = Constants.SERVICE_RANKING, intValue = 100)
})
Run Code Online (Sandbox Code Playgroud)

对于这种消费没有变化,更高的服务等级实施被提取,控制是与提供者

@Reference
HelloService helloService;
Run Code Online (Sandbox Code Playgroud)

要使用目标过滤器,请使用以下注释指定属性

@Properties({
   @Property(name="type", value="Custom")
})
Run Code Online (Sandbox Code Playgroud)

在基于过滤器消费时,指定目标,控制与消费者

 @Reference (target="(type=Custom)")
 HelloService helloService;
Run Code Online (Sandbox Code Playgroud)

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

Jér*_*e B 6

这与Declaratives Services如何连接@Reference有关.从规格:

如果引用具有一元基数并且引用有多个目标服务,则绑定服务必须是service.ranking属性指定的具有最高服务等级的目标服务.

如果存在具有相同服务排名的多个目标服务,则绑定服务必须是具有最高服务等级和service.id属性指定的最低服务标识的目标服务.

即,它取决于组件的"服务排名".如果未指定此排名,则可以进行任何实现(通常获得最旧的服务).如果要定位特定实现,可以使用过滤器.