Java Reference
In-Depth Information
Table 12.3
How Blueprint scopes apply to different managers
Singleton
Prototype
Bean
One instance of the bean object is con-
structed when the bean is activated.
This pattern is usually applied to state-
less services and core components. This
is the default scope for bean managers.
A new instance of the bean object is constructed
each time it's requested from the Blueprint con-
tainer using the getComponentInstance()
method. All inlined beans (which you saw in the last
subsection) are automatically prototype scope.
Service
A single service object is shared by all
clients of the service.
A new service object is returned to each bundle,
which provides a similar result if your bean imple-
ments an OSGi ServiceFactory .
TYPE CONVER TERS
The Blueprint specification defines a rich inversion of control (IoC) framework for
wiring objects together at execution time. Often, you need to convert between types
in order to perform an injection. The most obvious example is converting from
String values in XML to integer , boolean , or other basic Java types. The Blueprint
specification defines a default set of type converters that can convert a String value to
a target typed value.
The Blueprint specification also allows you to extend the default set of type con-
verters. Type converters are defined in the <type-converters> XML element, which is
a child element of the top-level <blueprint> element. In the <type-converters> ele-
ment, you can use <bean> or <reference> elements, which let you define local con-
verters for one particular Blueprint definition or shared converters in the OSG i service
registry. Consider the following XML snippet:
<type-converters>
<bean class="AtomicConverter">
<argument ref="blueprintConverter"/>
</bean>
</type-converters>
Here, you define a type converter using the class AtomicConverter that takes a refer-
ence to the top-level Blueprint converter as an argument in its constructor. A type con-
verter doesn't need to implement any specific interface, although it must implement
two methods:
canConvert(Object, ReifiedType)
convert(Object, ReifiedType)
The code for the atomic conversion class is shown in the following listing.
Listing 12.4 Converter class to coerce an Object to an AtomicReference
public class AtomicConverter {
Converter bpc;
public AtomicConverter(Converter bpc) { this.bpc=bpc; }
public boolean canConvert(Object s, ReifiedType T) {
return T.getRawClass() == AtomicReference.class
 
Search WWH ::




Custom Search