Search code examples
javaosgi-bundleblueprint-osgi

OSGI Bundle bean delcaration issue


I'm new to OSGI blueprint definition, I'm trying to accomplish the following :

Bundle 1 : Contains Bean1 (Which has a Bean2 injection) all declared in blueprint1

Bundle 2 : In it's blueprint2, I would like to use Bean1 (without declaring Bean2)

Result : Bundle 2 is failing on : missing dependency to Bean2, this makes sense because Bundle 2 doesn't add Bean2, but I don't want to make it visible in this bundle, It has some complex configs included in Blueprint1 (And I don't want to recopy the whole thing in Blueprint2) I was hoping to create it in Bundle1 and use it in Bundle2

Question : Is there any way to get this kind of access from Bundle 2 (bean1 is supposed to be like a service used by other bundles) ? If no, is there a different way to do ?

Thank you for your help.

As requested, below my blueprints :

Blueprints 1 :

`

<?xml version="1.0"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" ... >
   <bean id="bean1" class="Bean1" />
   <bean id="bean2" class="Bean2" />
</blueprint>

`

Blueprints 2 :

`

<?xml version="1.0"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" ... >
   <bean id="bean1" class="Bean1" />
</blueprint>

`

Bean 1 :

public class Bean1 {

  @BeanInject("bean2")
  Bean2 bean2;

  .... 

}

Blueprint 2 is failing


Solution

  • Below my solution :

    Bean declaration in the blueprint will always take the local bundle context, this one should identify all the dependencies used in this bean, so there is no way to call bean1 in blueprint2 without declaring Bean2 as well.

    The solution is to use a service, declare bean1 as a service in Blueprint1 and use it as a reference in Blueprint2, no need to define any dependency then.

    Detailed solution described here : Service OSGI