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 }
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 }
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 } }
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.