Java Reference
In-Depth Information
You use @Component to declare the Circle class as an i POJO component; see the side-
bar “Immediate components and service properties” for why you use the immediate
flag. With the @Provide annotation, you indicate that your component provides a ser-
vice. You leave i POJO the task of determining the type of the service, which defaults to
all implemented interfaces (only SimpleShape in this case).
You use the @ServiceProperty annotation to declare the m_name and m_icon
member fields as service properties, which i POJO automatically attaches to your pro-
vided service and even dynamically updates if the component code changes the field
values at execution time. Notice also that because you're using annotations, which are
part of the Java source code, you can use static constant fields for the attribute names,
unlike in Declarative Services or Blueprint; this greatly reduces the risks of metadata
rot due to changing attribute names.
Immediate components and service properties
Just as with Declarative Services and Blueprint, iPOJO delays class loading and com-
ponent instance creation for as long as possible. Sometimes this delay is inconvenient,
and you want the component created immediately. When using @ServiceProperty ,
iPOJO uses the member field value as a service-property value. But if component cre-
ation is deferred (which is the default behavior), iPOJO can't get the field value because
the field doesn't yet exist.
As a result, iPOJO first registers the service with no service properties. When the ser-
vice is requested by another component, then the component is instantiated, which
causes the field to be initialized and added to the service. To rectify this situation,
@ServiceProperty supports a value attribute to set the default value of the service
property; but this works only for simple types, not for complex types like this exam-
ple's icon. To deal with complex types, you need to use the immediate attribute of
@Component to tell iPOJO to instantiate the component immediately.
In listing 12.5, you use the default behavior of @Provides to tell i POJO to register all of
the component's implemented interfaces as service interfaces, including inherited
interfaces. You can also explicitly specify interfaces or classes to provide, as shown in
the following snippet:
@Component
@Provides(specifications=java.awt.Window.class)
public class PaintFrame extends JFrame
implements MouseListener, MouseMotionListener {
As with Declarative Services and Blueprint, the circle bundle no longer needs to have
a bundle activator because i POJO manages service publication. As mentioned previ-
ously, you have to modify the component's build process to include the i POJO Ant
task, but that's all there is to it. i POJO takes care of everything at execution time.
Now, let's look into consuming services.
 
Search WWH ::




Custom Search