Java Reference
In-Depth Information
12.2.3
Consuming services with iPOJO
i POJO defines two mechanisms for injecting services into a component: method injec-
tion and field injection. In most cases, the two can be used interchangeably or mixed
and matched. In either case, a component service dependency description can
include (among others)
Service specification —The actual service type of interest
Optionality —Whether it's mandatory (default) or optional
Aggregation —Whether it's for a single service or many
Filter —An LDAP filter over service properties for additional constraints
Binding policy —How dynamism is handled with respect to the component's life-
cycle (we'll discuss this more when we look at the i POJO component lifecycle)
Proxy injection —Whether injected services are proxied
Given the similarities between method and field injection, the approach you choose
often comes down to preference. Still, there are some things you can do only with one
or the other. For example, if you want to be notified whenever a desired service
becomes available, you need to use method injection to get a callback, which makes it
possible to react immediately to dynamic changes. Yet it's possible to use field injec-
tion and method injection at the same time to get the best of both worlds.
To proxy or not to proxy
By default, iPOJO injects proxies instead of the actual service object. This creates a
managed reference to the service that can be passed around internally in the com-
ponent. iPOJO uses byte-code generation instead of Java's dynamic proxy mecha-
nism, which improves performance and avoids the limitation of working only with
interfaces. For platforms like Android where dynamic byte-code generation isn't sup-
ported, iPOJO reverts to Java's dynamic proxies.
Note that iPOJO proxies aren't like Blueprint proxies, in that they don't do any sort of
blocking of the calling thread if no backing service is available. Instead, by default,
they hide the fact that the service is missing by using the null-object pattern, which
we'll discuss shortly. If you'd rather not use proxies, you can disable them on a de-
pendency-by-dependency basis or completely.
We'll first explore method injection, because it's similar to the mechanisms you saw in
Declarative Services and Blueprint.
METHOD INJECTION
i POJO defines @Bind and @Unbind method-level annotations to declare the binding
methods for a specific service dependency, where @Bind is associated with the method
used to inject a service and @Unbind is associated with the method used to remove a
previously injected service. These annotations can be applied to methods with any of
the following signatures:
 
Search WWH ::




Custom Search