Runtime components : Core components : ElementFactory : Tasks : Injecting dependency
  
Injecting dependency
Element instance exists with other element instances. For example, one instance "a" might keep a reference to another instance "b". This relationship is dependency. The instance "a" is a depending element instance, and instance "b" is a depended element instance. When the ElementFactory set the reference of "b" to "a", the ElementFactory injects the dependency to "a".
In the definition for ElementFactory, all the depended element instance must be defined as a sub tag of the depending element instance.
This section describes how to inject dependency using tag attribute, setter method and general method.
Tag attribute
An element instance might be required to populate with some simple data value, and the Element also complies with JavaBean standard. In this case, you can specify the field value as attributes of the tag.
For example, following is a sample element:
public class SimpleElement {
  private String fieldA;
  private int fieldB;
  private Long fieldC;
  //getters and setters here
}
Its definition is as follows:
<myPackage.SimpleElement id="aSimpleElement" fieldA="valueA" fieldB="123" fieldC="12345" />
The BasicElementFactory converts the string value of attributes to the required simple type automatically.
The following table lists the supported simple types:
 
Primary type
Simple object
int
java.lang.Integer
byte
java.lang.Byte
char
java.lang.Character
long
java.lang.Long
float
java.lang.Float
double
java.lang.Double
short
java.lang.Short
The sample definition equals to the following one:
<myPackage.SimpleElement id="aSimpleElement">
<string Injection="fieldA" value="valueA"/>
<integer Injection="fieldB" value="123"/>
<long Injection="fieldC" value="12345"/>
<myPackage./SimpleElement>
Injection
An element instance may refer to another element instance. It must comply with JavaBean standard.
Following is an example:
public class SimpleElement {
  private String fieldA;
  private int fieldB;
  //getters and setters here
}
public class ComplexElement {
  private String fieldA;
  private SimpleElement simpleElement1;
  private SimpleElement simpleElement2;
  //getters and setters here
}
You can define the element instance as follows:
<mypackage.ComplexElement id="aElement" fieldA="valueA">
  <mypackage.SimpleElement Injection="simpleElement1" fieldA="valueA" fieldB="123"/>
  <mypackage.SimpleElement Injection="simpleElement2" fieldA="valueA" fieldB="321"/>
</mypackage.ComplexElement>
In this way, you must define the depended element instance as a subtag of the depending element instance. The attribute value of Injection in depended element should be the field name in the depending element.
Injecting
Sometimes, you might need to inject some dependencies into your element by calling some method other than setters. This is also supported by ElementFactory.
Take the following class for example:
public class A {
  public void populate(String a, String b) {
    //Some logic here
  }
}
You can define the element as follows:
<mypackage.A id="abc">
  <arguments Inject="populate">
    <string ArgumentType="java.lang.String" value="abc"/>
    <string ArgumentType="java.lang.String" value="def"/>
  </arguments>
</mapackage.A>
To inject by method, you must define the parameters as arguments, and add an attribute Inject to the arguments. The value of the Inject attribute must be the name of the injecting method.
Go up to
Tasks