Results 1 to 4 of 4

Thread: ServiceRegistration injection

  1. #1
    Join Date
    Jan 2009
    Posts
    2

    Default ServiceRegistration injection

    Hi all!


    I have a problem injecting a org.osgi.framework.ServiceRegistration object.

    Suppose the following declaration:

    osgi:service id="myServiceRegistration" ref="beanToPublish"
    interface="com.xyz.MessageService"/>

    According to the spring dm docs, there is now a bean defined in the application context with id="myServiceRegistration" of type "org.osgi.framework.ServiceRegistration"


    However, when I try to inject a reference to this bean into another bean like this:

    <bean id="beanToPublish" class="com.xyz.beanToPublish">
    <property name="serviceRegistration"><ref bean="myServiceRegistration"/></property>
    </bean>

    I get the following error (both declarations are in the same bundle):

    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'myServiceRegistration': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'myBean' defined in URL [bundleentry://21/META-INF/spring/application-context.xml]: Cannot resolve reference to bean 'myServiceRegistration' while setting bean property 'registration'; nested exception is org.springframework.beans.factory.BeanCurrentlyInC reationException: Error creating bean with name 'myServiceRegistration': FactoryBean which is currently in creation returned null from getObject


    So the problem seems to be that spring dm cannot resolve the name "myServiceRegistration" although it has been defined (through osgi:service id). Any ideas? Thank you!

  2. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Code:
    <osgi:service id="myServiceRegistration" ref="beanToPublish"
         interface="com.xyz.MessageService"/>
    According to the spring dm docs, there is now a bean defined in the application context with id="myServiceRegistration" of type "org.osgi.framework.ServiceRegistration"
    Where did you read this in the docs? The bean definition above is of type "com.xyz.MessageService" - that's why one defines the interface.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  3. #3
    Join Date
    Jan 2009
    Posts
    2

    Default

    Alright, obviously I got something wrong.
    In

    http://static.springframework.org/os...egistry:export

    it says

    The bean defined by the service element is of type org.osgi.framework.ServiceRegistration and is the ServiceRegistration object resulting from registering the exported bean with the OSGi service registry. By giving this bean an id you can inject a reference to the ServiceRegistration object into other beans if needed. For example:

    <service id="myServiceRegistration" ref="beanToPublish"
    interface="com.xyz.MessageService"/>

    So I thought,there is now a org.osgi.framework.ServiceRegistration bean defined with id=myServiceRegistration. Anyhow, as I already said, I got something wrong with this statement. Could you shed some light on this?

    Apart from that, how can I get a reference to the according ServiceRegistration object then?
    Thank you!

  4. #4
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Actually, it was me misreading your post - I thought you meant a ServiceReference not ServiceRegistration and osgi:reference instead of osgi:service - my apologies.

    Back to the question - the definition should return indeed a ServiceRegistration to the service published by the definition. The problem in your case is that you try to inject into the bean that is going to be published, the registration to its publication.
    Since the registration is null (since the bean hasn't been published, as it hasn't been created since it cannot be configured as it misses the registration) you end up with the error.
    The easiest way to solve this is to break the cycle by injecting he service and its registration into an extra object.

    P.S. the doc contains some advices on listener and bean cycles: http://is.gd/eREN
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

Posting Permissions

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