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.
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.
public class SimpleElement { private String fieldA; private int fieldB; private Long fieldC; //getters and setters here }
<myPackage.SimpleElement id="aSimpleElement" fieldA="valueA" fieldB="123" fieldC="12345" />
The BasicElementFactory converts the string value of attributes to the required simple type automatically.
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 |
<myPackage.SimpleElement id="aSimpleElement"> <string Injection="fieldA" value="valueA"/> <integer Injection="fieldB" value="123"/> <long Injection="fieldC" value="12345"/> <myPackage./SimpleElement>
An element instance may refer to another element instance. It must comply with JavaBean standard.
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 }
<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.
Sometimes, you might need to inject some dependencies into your element by calling some method other than setters. This is also supported by ElementFactory.
public class A { public void populate(String a, String b) { //Some logic here } }
<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.