首页 > 解决方案 > What is the @Service annotation in Apache Felix?

问题描述

I've read several articles about the difference between @Service and @Component. Now I understand that @Component is user to annotate an object as an OSGi component and its lifecycle will then be managed by OSGi. However, what is the need to declare an object as service with @Service is unknown. What happens if you write your business logic in a object that is declared as a component?

I also want to know what does the below statement means:

Components can refer/call (using container injection – @Reference) other services but not components. In other words, a component cannot be injected into another component / service. Only services can be injected into another component.

标签: serviceannotationsosgiaemsling

解决方案


避免使用Felix SCR(包org.apache.felix.scr.annotations)中的注释。

http://felix.apache.org/documentation/subprojects/apache-felix-maven-scr-plugin/scr-annotations.html

注释本身不支持 R6 或更高版本的新功能。建议改用声明式服务的官方 OSGi 注释。

使用@Componentfrom package org.osgi.service.component.annotations,这个注解替换了@Component@Service来自 Felix。

  • 问题2

    如果您在声明为组件的对象中编写业务逻辑会发生什么?

    碰巧工作正常。

  • 问题 3

    我也想知道下面的语句是什么意思:

    组件可以引用/调用(使用容器注入 - @Reference)其他服务,但不能引用组件。换句话说,一个组件不能被注入到另一个组件/服务中。只有服务可以注入那里的另一个组件。

    这就是组件在 OSGi 中共享功能的方式。他们将其功能作为 OSGi 服务提供。但是当 OSGi 将对象注入到引用中时,您就得到了组件。

奖励:阅读这篇文章:https ://medium.com/adobetech/using-the-osgi-declarative-service-in-aem-6-4-21102f649d54


推荐阅读