|
Page Created :
FELIX :
How to use iPOJO Annotations
How to use iPOJO Annotations has been created by Clement Escoffier (Aug 13, 2007). Content:iPOJO defines several annotations to help developer to define their components. This page presents an example of iPOJO annotation and describes provided annotations. Getting iPOJO Annotations:iPOJO Annotations are defines inside the org.apache.felix.ipojo.annotations project. You can download the Jar file of this project here In Eclipse:Add the org.apache.felix.ipojo.annotations jar file in your build path. Do not forget to use a Java compiler accepting annotations (1.5 or higher). In Maven:Add the following dependency: <dependency> <groupId>org.apache.felix</groupId> <artifactId>org.apache.felix.ipojo.annotations</artifactId> <version>0.7.3-SNAPSHOT</version> </dependency>
Hello Service ProviderThe provider uses two annotations. The "component" annotation is mandatory and defines that the class defines a component type. Then the "provides" annotation just declare that the defined component type provides a service. package ipojo.example.hello.impl; import ipojo.example.hello.Hello; import org.apache.felix.ipojo.annotations.Component; import org.apache.felix.ipojo.annotations.Provides; /** * Component implementing the Hello service. **/ @Component(name="HelloImpl") @Provides public class HelloImpl implements Hello { public String sayHello(String name) { return "hello " + name; } } Hello Service ConsumerThe Hello Service Consumer use more annotations. First it used the componenet annotation. To defines its "immediate" behavior, it add the 'immediate' attribute. package ipojo.example.hello.client; import org.apache.felix.ipojo.annotations.Component; import org.apache.felix.ipojo.annotations.Invalidate; import org.apache.felix.ipojo.annotations.Requires; import org.apache.felix.ipojo.annotations.Validate; import ipojo.example.hello.Hello; @Component(name="AnnotedHelloClient", immediate=true) public class HelloClient implements Runnable { @Requires private Hello[] m_hello; // Service Dependency private final static int DELAY=10000; private boolean end; public void run() { while (!end) { try { invoke(); Thread.sleep(DELAY); } catch (InterruptedException ie) { } /* will recheck end */ } } public void invoke() { for (int i = 0; i < m_hello.length; i++) { System.out.println(m_hello[i].sayHello("Clement")); } } @Validate public void starting() { Thread T = new Thread(this); end = false; T.start(); } @Invalidate public void stopping() { end = true; } } Defined AnnotationsThis section lists defined annotations and how to use them. @ComponentGoal: Defines a component type
@ProvidesGoal: Defines that the component type provide services
Note: "FACTORY" means OSGi service factory. @RequiresGoal: Defines a service dependency
@ServicePropertyGoal: Defines a service property
@PropertyGoal: Defines a property
@BindGoal: Defines a bind method
@UnbindGoal: Defines an unbind method
@ValidateGoal: defines a validate lifecycle callback @InvalidateGoal: defines a validate lifecycle callback Metadata file and annotation mergeIt is possible to defines component type both in the metadata file (in XML) and by using annotation. However, if a component type defined by using annotations has the same name than a type define in the XML file, the XML descriptor override the annotation defined type. However, a warning message is launched during the manipulation. Instance creationAnnotation can only be used to define component type. To define instances, you need to use the XML descriptor. Instance can refer to annotated types by referring to their names. <instance component="HelloImpl"/> <instance component="AnnotedHelloClient"/> Generic AnnotationsTo support external handlers, iPOJO defines two generic annotations. These annotations can be used if you want to use an external handler. However, due to annotation limitation, not all external handlers can be used with these annotations. @ElementGoal: defines an element
@AttributesGoal: defines an element attribute
@SubElementGoal: defines an sub-element
|
Unsubscribe or edit your notifications preferences
