Java Reference
In-Depth Information
&& bpc.canConvert(s, T.getActualTypeArgument(0));
}
public Object convert(Object s, ReifiedType T)
throws Exception {
Object obj = bpc.convert(s, T.getActualTypeArgument(0));
return new AtomicReference<Object>(obj);
}
}
The
canConvert()
method checks whether it can convert the supplied object to the
given reified type. The
convert()
method is called by the Blueprint container if the
canConvert()
method returns
true
. The top-level Blueprint converter is injected into
the constructor of the
AtomicConverter
class to allow it to convert generic arguments.
For example,
AtomicConverter
can use the top-level converter to convert a source
object to an
Integer
and then create an
AtomicReference
to this converted object.
WARNING
Type converters shouldn't require type conversion in their initial-
ization because the state of the converter isn't well defined at this time. The
Blueprint built-in type converter delegates to registered converters, so a call
to the converter during construction may fail because a needed type con-
verter may not have been registered yet.
Having defined this converter, any injection targeting an
AtomicReference<T>
value is
automatically converted into an
AtomicReference
of the appropriate type using the
example converter. To illustrate, consider the following code:
public class Foo<T extends Integer> {
public Foo(AtomicReference<T> v) {}
}
Here's the corresponding
XML
snippet:
<bean id="foo" class="Foo"> <argument value="6"/></bean>
This pattern of conversion is useful if you have to adapt third-party code that you can't
change, but you nonetheless want to have a common model at execution time.
Next, we'll discuss metadata, which is the last advanced Blueprint feature before
we move on to the i
POJO
component framework.
METADATA
Metadata
is a programmatic representation of the
XML
description of the Blueprint
component. In Spring, the main use case for accessing metadata is to dynamically
modify the model (add new elements, modify elements, and so on) at execution time.
This forms a flexible component pipeline much like aspect weaving in Java code, but
at the component level. But in the current Blueprint specification, this model is
largely for informational purposes. Still, it's useful because it can be used to build
diagnostic tools to analyze the structure of Blueprint components, for example.
The Blueprint specification defines many classes to model the Blueprint compo-
nents. We won't list every method and its meaning here, but figure 12.3 provides a
view of the Blueprint metadata interface hierarchy.