我读过几篇关于@Service
和@Component
之间差异的文章。现在我明白@Component
是用户注释一个对象作为OSGi组件,然后它的生命周期将由OSGi管理。但是,使用@Service
将对象声明为服务的需要是未知的。如果在声明为组件的对象中编写业务逻辑,会发生什么?
我也想知道以下声明的含义:
组件可以引用/调用(使用容器注入 - @Reference)其他服务但不能引用组件。换句话说,组件不能注入另一个组件/服务。只有服务可以注入另一个组件。
@Component
描述的声明性服务(DS)概念。避免使用Felix SCR的注释(包org.apache.felix.scr.annotations
)。
http://felix.apache.org/documentation/subprojects/apache-felix-maven-scr-plugin/scr-annotations.html:
注释本身不支持R6或更高版本的新功能。建议使用官方OSGi注释来声明服务。
使用@Component
包中的org.osgi.service.component.annotations
,这个注释取代了Felix的@Component
和@Service
。
奖励:阅读这篇文章:https://medium.com/adobetech/using-the-osgi-declarative-service-in-aem-6-4-21102f649d54