Java Reference
In-Depth Information
<reference-list
id="specialOffers"
interface="fancyfoods.offers.SpecialOffer">
<reference-listener
ref="offerAggregator"
bind-method="bind"
unbind-method="unbind" />
</reference-list>
The reference listener need not be the same as the bean that uses the services. It can
even be a completely new bean declared with an inlined <bean> element, although in
that case the bean will always have a singleton scope.
The bind method is only called if the new service is consumed by the bean. In the
case of a reference list, this is every time a matching service is added to the Service
Registry. When a reference is used, on the other hand, the consumed service will only
be changed (and the bind method called) if the new service has a higher service rank-
ing than the existing one.
The bind method for the offer aggregator is simple; when something gets added to
the list, the cache should be updated. If you're interested in the service properties,
your bind and unbind methods can also be written to take an optional Map that lists all
of the service properties:
public void bind(SpecialOffer offer) {
sortOfferList();
}
The bind method is called after the new service has been added to the list, so the
sorted list will be accurate. The unbind method, on the other hand, is called before the
reference is removed. Calling the method to re-sort the offer list and update the cache
in the unbind method isn't much use, because it's the old list that will be sorted.
Instead, you'll need to get your hands dirty and remove the obsolete offer from the
cache manually:
public synchronized void unbind(SpecialOffer offer) {
if (sortedOffers != null) {
sortedOffers.remove(offer);
}
}
WARNING: PROXYING OBJECTS One side effect of the proxying of services is
that you'll find that the service you get injected with isn't == to the service in the
Service Registry, even though you might expect it to be. The proxied object may
not even be of the same type as the real service object! Rest assured, if you've
implemented equals() and hashCode() on your service, then the proxy code
does the correct unwrapping to make sure that they work properly.
Somewhat unfortunately, a bug in the early releases of the Aries proxy code
(prior to 0.4) sometimes fails to do this unwrapping if your service uses the
default version of equals() .
Search WWH ::




Custom Search