Results 1 to 4 of 4

Thread: Spring DMa and service Polymorphism

  1. #1
    Join Date
    Mar 2009
    Posts
    3

    Default Spring DMa and service Polymorphism

    Hello,
    relatively new to Spring DM and I was looking for an example of Service Consumer capable to consume different services all implementing the same interface. I have seen example of service implementing multiple interfaces but none related to different service implementation of the same interface. Can the consumer decide which implementation to use ? For instance in the case of MailBox (interface) and RSS based or POP/IMAP based implementation can the consumer decide which implementation to use ? Can anybody point out any example ? I would like the consumer not to have any deploy time constrain (deployment descriptors) as such service implementations can be added at later stage. Consumer could use some sort of parameter to based the decision.

    So far my understanding is that this is not possible as each Provider exports interfaces and Consumers import interfaces and so it is not possible to access specific implementations. But I might be wrong. In other words does spring DM/OSGi support Polymorphism ?

    Regards.

  2. #2
    Join Date
    Dec 2005
    Location
    Lyon, France
    Posts
    311

    Default

    In OSGi, interface is not the only way to export and then consume services. You can also use properties (export) and filters (consumption).

    With Spring DM, you export the service with some properties (i.e. metadata):

    Code:
    <service ref="beanToBeExported" interface="com.xyz.MyServiceInterface">
      <service-properties>
        <beans:entry key="myOtherKey" value="aStringValue"/>
        <beans:entry key="aThirdKey" value-ref="beanToExposeAsProperty"/>
      </service-properties>
    </service>
    And then consume it with filters:

    Code:
    <reference id="asyncMessageService" interface="com.xyz.MessageService"
      filter="(asynchronous-delivery=true)"/>
    In your case, the properties would value to rss or pop.imap, etc.

  3. #3
    Join Date
    Dec 2009
    Posts
    3

    Default

    Another possible solution is to include identification methods in the service interface.

    For example :

    Code:
    interface IMailBox {
      String getMailBoxId();//<-- return something like "RSS" or "IMAP"
    
      : //the real stuff
      : 
    }
    you can then use these ids to show a dropbox that let's the user select the impl he wants.

    You can go even farther by returning something more structured than a String, by defining a Descriptor class.

    Cheers,

  4. #4
    Join Date
    Oct 2005
    Posts
    26

    Default

    Quote Originally Posted by gsarno View Post
    So far my understanding is that this is not possible as each Provider exports interfaces and Consumers import interfaces and so it is not possible to access specific implementations. But I might be wrong. In other words does spring DM/OSGi support Polymorphism ?
    Note that in addition to the good answers already provided, it is sometimes - but not always - possible to provide polymorphism. What you need to understand is that for accessed services Spring creates a proxy and this proxy - and the interfaces it implements - can never change. Thus for a single service reference Spring will only bind to a service that implements the interfaces specified and it will never add to those interfaces.

    For service collections the story is a little different - the proxies are created as services become available and these proxies can reflect the actual interfaces implemented by the underlying service. So service collections might give you what you need, but in general you are probably better off using service properties to select an implementation at runtime.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •