Java Reference
In-Depth Information
types you need, and then separately deploy a bundle containing an application config-
uration describing which instances of which components to create and how to config-
ure them. For example, consider the following simple component:
@Component(name="hello")
@Provides
public class HelloImpl implements Hello {
@Property
private String name;
public void sayHello() {
System.out.println("Hello my name is " + name);
}
}
This component prints a message telling you its name, where its name is injected into
the member field
name
. You indicate this by using the i
POJO
@Property
annotation.
Here's how to create and configure four different instances of the component:
<instance component="hello">
<property name="name" value="David"/>
</instance>
<instance component="hello">
<property name="name" value="Karl"/>
</instance>
<instance component="hello">
<property name="name" value="Richard"/>
</instance>
<instance component="hello">
<property name="name" value="Stuart"/>
</instance>
You declare four different component instances and uniquely configure each. When
the bundle containing this component configuration is activated, the i
POJO
runtime
finds the component type associated with the name
hello
and instantiates it four
times, injecting the appropriate configuration into the corresponding instance. In
addition to simple name-value properties, i
POJO
also supports lists, maps, arrays, sets,
and dictionaries as configuration properties.
This is the recommended approach for creating component instances. And
remember, the
XML
is only parsed at build timeāno
XML
parsing goes on at
execution time. Regardless, some people wish to avoid
XML
, which brings us to the
next approach.
@INSTANTIATE INSTANCE CREATION
i
POJO
also supports the
@Instantiate
annotation. It provides a way to create a com-
ponent instance without
XML
and is largely equivalent to declaring a static singleton
in Java code. You use it like this:
@Instantiate
@Component
@Provides
public class FooImpl implements Foo {
public void doFoo() {