...
Code Block | ||||
---|---|---|---|---|
| ||||
public class HelloConsumer {
private Hello m_hello;
public doSomething() {
System.out.println(m_hello.getMesage());
}
}
|
For this component, metadata could be:
...
Note, that the bind the unbind method can be have different signatures. By using this mechanism, you need to be sure to manage the dynamism correctly.
(See note on type discovery)
Field injections and Method invocations
...
Code Block | ||||
---|---|---|---|---|
| ||||
public class HelloConsumer {
private Hello m_hello; // Injected Field
public void bindHello() { System.out.println("Hello appears"); }
public void unbindHello() { System.out.println("Hello disapears"); }
public doSomething() { System.out.println(m_hello.getMesage()); }
}
|
...
To declare an optional requirement, you need to add the 'optional' attribute. To avoid null pointer exception, iPOJO injects a Nullable object in the field when no service provider is available. The nullableobject implements the service interface, but does nothing. For further information refer to the note about nullable object. Moreover, it is possible to set a default-implementation for the service. A default-implementation is a class implementing the service but used only when no others service providers are available. The default-implementation object will be injected instead of the Nullable objet. For further information refer to the note about nullable object.
Optional Dependency with method invocation
...
The FOO component type declares a service dependency with the 'id1' id. This dependency has no filter by default. The first instance is just an instance of the FOO component type and does not modify the dependency. The second one adds a filter to the declared dependency to target providers with foo.property = BAR. The last one adds another filter to the declared dependency. By using instance filter customization, it is possible to create complex applications where you avoid binding problems by filtering dependencies instance by instance.
Binding
...
Policies
Three binding policies are supported inside iPOJO.
...
Code Block | ||||
---|---|---|---|---|
| ||||
<component classname="...HelloConsumer"> <requires field="m_hellos" policy="dynamic-priority" comparator="my.comparator"/> ... </component> |
Anchor | ||||
---|---|---|---|---|
|
Note about nullable object & default-implementation
...
If the log service is not available, iPOJO creates an object of the 'org.apache.felix.ipojo.example.default.MyLogService' class. This object is injected instead of a Nullable object. For instance, the default implementation can print messages on the System.err stream. The nullable object does no display anything.
Anchor | ||||
---|---|---|---|---|
|
Note about Callbacks
Dependency manages two type of callback: bind and unbind. A callback with a type "bind" is called each type that a service provider arrives and the binding is necessary. According to the cardinality of the dependency it means:
...
The method can receive in argument the service object or the service reference (in order to obtain service properties). The bind methods are delayed since a POJO object is created.
Anchor | ||||
---|---|---|---|---|
|
Note on service interface discovery
...