Using a factory method to create POJO object
By default, iPOJO calls the POJO constructor to create objects. This constructor can either has no argument or receive the bundle context. However sometimes, you may want a more advanced object creation policy.
iPOJO allows you describing a factory method called to create POJO objects instead of the constructor. So, every time that iPOJO needs to create an instance of your POJO class, this method will be called to obtain the object.
To use this feature you need to add the factory-method attribute in the Component element as illustrated below:
<component className="org.apache.felix.ipojo.test.scenarios.component.FooProvider" factory-method="createProvider" > ... </component>
The specified method must be a static method of the implementation class returning an instance of this implementation class. The following code shows an example of usage:
public static FooProvider createProvider() { if (singleton == null) { singleton = new FooProvider(); } return singleton; }
This method must then call any valid constructor (potentially private) of the implementation class.
However, be aware that if you create an instance by using a factory-method, the called constructor hasn't access to values injected by the iPOJO container. So, the called constructor must not try accessing to services, properties ... These objects will be accessible when the constructor returns.
As for "normal" constructor, the method-factory can receive the bundle context in argument, such as in:
public static FooProvider createProvider (BundleContext bc) { if (singleton == null) { singleton = new FooProvider(bc); } return singleton; }
Overview
Getting Started
- iPOJO in 10 minutes
- How to use iPOJO Annotations
- iPOJO Hello Word (Maven-Based) tutorial
- iPOJO Advanced Tutorial
- iPOJO Composition Tutorial
User Guide
- Describing components (handler list)
- Using XML Schemas
- Describing components with the iPOJO-API
- Testing components
- Advanced Topics
- Eclipse Integration
- FAQ
- iPOJO Reference Card
Tools
- iPOJO Eclipse Plug-in
- iPOJO Ant Task
- iPOJO Maven Plug-in
- Online-Manipulator
- iPOJO Arch Command
- iPOJO Webconsole plugin
- Junit4OSGi
Developer Guide
- Javadoc: 1.2
- How to write your own handler
- How to use iPOJO Manipulation Metadata
- Dive into the iPOJO Manipulation depths
Misc & Contact
- Issues Tracker
- Supported JVMs
- Supported OSGi Implementations
- iPOJO's Dark Side Blog
- Future Ideas
- Article & Presentations
Experimentation