为什么要对CQ中的每个服务使用@Component批注 [英] Why use @Component annotation with each service in CQ

查看:103
本文介绍了为什么要对CQ中的每个服务使用@Component批注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下事情感到困惑.我理解@Service@Component批注是在OSGi中定义组件或服务时的主要​​批注.我指的是 http://felix.apache.org/documentation/subprojects/apache-felix-maven-scr-plugin/scr-annotations.html

I am bit confused about following things. I understand @Service and @Component annotations are main annotations when we define a component or a service in OSGi. I am referring to http://felix.apache.org/documentation/subprojects/apache-felix-maven-scr-plugin/scr-annotations.html and What is the difference between OSGi Components and Services

问题:

  1. 没有@Component注释就无法创建服务,为什么?

  1. A service can not be created without @Component annotation, why is that?

我了解一旦定义了服务,其生命周期将由OSGi进行不同的管理,但是这样做的好处是什么?

I understand once we define a service its life-cycle is managed by OSGi differently but what are the advantages of doing so?

我们如何使用定义为@Component的类,因为可以通过sling.getService(ServiceName.class)

How do we use class defined as @Component as service can be accessed via sling.getService(ServiceName.class)

推荐答案

  1. 服务 可以发布而没有@Component批注,但是您必须以编程方式进行.如果使用注释,则可以从构建工具中的自动元数据生成中受益,也可以从Declarative Services运行时框架中受益.这简化了很多事情.如果要使用低级代码,则必须编写BundleActivator的实现,并使用Bundle-Activator清单标头声明,调用context.registerService等.底线:只需使用@Component批注!

  1. A service can be published without a @Component annotation, but you have to do it programmatically. If you use the annotation then you benefit from the automatic metadata generation in the build tool, and also from the Declarative Services runtime framework. This simplifies a lot of things. If you want to do it with low-level code you have to write an implementation of BundleActivator, declare that with the Bundle-Activator manifest header, call context.registerService etc. Bottom line: just use the @Component annotation!

简单:懒惰.当组件是服务时,则可以延迟按需"实例化它,即仅当消费者首先尝试使用该服务时.另一方面,非服务组件通常会在自己内部做其他事情,例如运行Web服务器或GUI或轮询线程(无论如何).这些需要一直运行,而不是按需运行.

Simple: laziness. When a component is a service then it can be instantiated lazily "on-demand", i.e. only when consumer first tries to use the service. Non-service components, on the other hand, usually do other kinds of things inside themselves, e.g. running a web server or a GUI or a polling thread, whatever. These need to be running all the time, rather than on-demand.

<罢工> 3.我不明白这个问题.

  1. 无法从捆绑包外部访问未发布为服务 的组件.如果您希望它可访问,则它必须是一项服务.如果您认为这没有用,请考虑创建HTTP服务器的组件.它打开端口80,并响应来自外部世界的网络请求.因此,即使它不是一项服务,也无法从其他捆绑软件中访问它,它还是会做一些有用的事情.这种组件就像您的应用程序与外界之间的桥梁.而服务是您的应用程序的一部分与另一部分之间的桥梁.
  1. A component that is not published as a service cannot be accessed from outside the bundle. If you want it to be accessible then it has to be a service. In case you think this is useless, consider a component that creates an HTTP server. It opens port 80 and responds to network requests from the outside world. So it does something useful even though it's not a service and not accessible from other bundles. This kind of component is like a bridge between your application and the outside world; whereas services are a bridge between one part of your application and another part.

这篇关于为什么要对CQ中的每个服务使用@Component批注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆