Blueprint and injection

Blueprint is also used to wire together components of an application declaratively. The technique that is used is called injection, and it is used extensively throughout InfoSphere® MDM.

Injection is a mechanism used by the OSGi container to locate and inject into one bean, the references to another bean or even a service. This allows a programmer to write his code with the assumption that a given service will be available in his bean without his having to write code to get the service or bean explicitly. Here’s an example taken from InfoSphere MDM:
<service id="Component.Party" 
  interface="com.dwl.tcrm.coreParty.interfaces.IParty"> 
   <bean class="com.dwl.tcrm.coreParty.component.TCRMPartyComponent" > 
    <property name="BObjQueryFactory"> 
<reference interface="...PartyModuleBObjQueryFactory" 
  availability="optional"/> 
     </property> 
    <property name="BObjPersistenceFactory"> 
       <reference interface="...PartyModuleBObjPersistenceFactory"  
    availability="optional"/> 
     </property> 
   </bean> 
 </service>  
You’ll recognize the service definition and bean definitions from the previous discussion. New to this definition is the following:
<property name="BObjPersistenceFactory"> 
       <reference interface="...PartyModuleBObjPersistenceFactory"  
    availability="optional"/> 
     </property> 
In this a snippet, a property in a blueprint file equates to a member variable in the TCRMPartyComponent bean. The property name is BObjPersistenceFactory and if you look in the TCRMPartyComponent class you’ll find these lines:
298   private  PartyModuleBObjQueryFactory bObjQueryFactory = null; 
299   private  PartyModuleBObjPersistenceFactory bObjPersitenceFactory = null;

The name of the property in blueprint equates to the member variable name in the bean. You’ll also find corresponding getter and setter methods for those two variables. This property has a <reference> to an interface named "...PartyModuleBObjPersistenceFactory" (the full package name is omitted for brevity). Remember that services are defined by their interface, and so this property has a reference to a service.

In another blueprint definition, you’ll find this snippet:
<service id="PartyModule.BObjPersistenceFactory" 
 interface="...PartyModuleBObjPersistenceFactory">  
 <bean  class="...query.PartyModuleBObjQueryFactoryImpl" /> 
</service> 
This is where the service to which the previous snipped referred is defined.

When the bundle or bundles containing the previous blueprint definitions start, the OSGi Blueprint container will automatically inject the PartyModuleBObjPersistenceFactory service into the TCRMPartyComponent using the appropriate setter methods. You will not need to worry about where it came from. That’s injection.