Java Reference
In-Depth Information
i
POJO
, you do so by declaring the component class with a constructor that accepts
BundleContext
as a parameter. Here's an example from the window listener:
@Component(immediate=true)
public class WindowListener extends WindowAdapter {
private BundleContext m_context;
public WindowListener(BundleContext context) {
m_context = context;
m_log.log(LogService.LOG_INFO, "Created " + this);
}
i
POJO
automatically injects the
BundleContext
into your component when it's instan-
tiated. So how do you instantiate your components in i
POJO
? You'll find out next.
12.2.5
Instantiating components with iPOJO
At this point, you've seen how to define an i
POJO
component using Java annotations,
and we've looked into component lifecycle issues; but, surprisingly, nothing you've
learned so far creates any component instances. Unlike Declarative Services and Blue-
print, where component definitions are typically treated as configured component
instances, i
POJO
always treats a component definition as a type definition. The distinc-
tion is the same as between a class (type) and an object (instance).
An i
POJO
component description defines a template for creating component
instances; but creating an instance requires an extra step, much like using
new
in Java
to create an object. How do you accomplish this in i
POJO
? There are four possibilities:
■
Static
XML
when the component is instrumented
■
Static
@Instantiate
annotation in the Java source code
■
Programmatically using an i
POJO
component factory service
■
Programmatically using the
ConfigurationAdmin
service
We'll look into each of these options in this section.
XML INSTANCE CREATION
Recall earlier that when we discussed setting up the build process for an i
POJO
compo-
nent, you saw the following Ant task referencing a circle.xml file:
<ipojo input="${dist}/${ant.project.name}-${version}.jar"
metadata = "OSGI-INF/circle.xml"/>
Now you can see what this file contains:
<?xml version="1.0" encoding="UTF-8"?>
<ipojo>
<instance component="org.foo.shape.circle.Circle"/>
</ipojo>
This instructs i
POJO
to create an instance of the circle component. Although the cir-
cle.xml file is contained in the same bundle as the circle component, this needn't be
the case. The beauty of i
POJO
's strict separation between component type and
instance is that you can package all your component types into bundles, deploy which