0

i am new to CQ5, when i am going through the OSGi concepts, i found some thing called services. I created a bundle which has an interface and implementation class for that, which overrides the method (simply returns a string "hello world").If i want to utilize that class in jsp/some java class i will create a new instance of that object and use its methods.So my question is, what is the exact difference between creating a new object and utilizing service. And please mention the advantages of services over a new object.And help with configuring services using spring-DM.

4

2 回答 2

2

我想说组件和服务的主要区别在于它们的生命周期是由 OSGi 管理的。这意味着它们的状态独立于使用组件的类。

您的 OSGi 服务有一个状态,它可用于存储信息或响应消息,它们将超出使用它们的类的范围。它们还允许您将接口与实际实现分开。您可以轻松地在正在运行的实例中交换某些服务的实现(IE,将提供程序从 v1 升级到 v1.0.1 )。此外,您可能希望在不关闭应用程序的情况下暂时停止组件。该服务将在激活时再次连接。

它们中的每一个的基本用例都是关于它们的功能。如果一个类只是一个带有一些 getter 和 setter 的 bean,那么您可能会实例化它、填充它并在需要时使用它。另一方面,如果一个类提供某种功能(处理、存储、排队等)。它可能应该是一项服务。具有大量静态方法(助手、管理器)的类通常可以很容易地重构为服务。

不确定spring-dm。但在 Adob​​e CQ5 中,您通常将Felix 注释用于声明式服务

于 2014-02-21T15:07:52.487 回答
0

In addition to the great answer by @santiagozky, I will add that the other major difference is dependency management.

When you instantiate an object, you inherit all of its dependencies. You are forever bound to changes in the implementation that can change those dependencies. When utilizing a service, you are following a Java best practice of coding to an interface. Your code is implementation agnostic and is only dependent on the interfaces dependencies, which will always be equal or less than the implementation. Typically, a much smaller set of dependencies. Modifying an existing implementation or changing the implementation altogether has no impact on your code.

于 2014-03-03T17:18:08.140 回答