Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Include Page
apache-felix-ipojo-header
apache-felix-ipojo-header

HTML

<div class="content">

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 sophisticated 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:

...

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.

...

Code Block
    public static FooProvider createProvider (BundleContext bc) {
        if (singleton == null) {
            singleton = new FooProvider(bc);
        }
        return singleton;
    }



Include Page
apache-felix-ipojo-footer
apache-felix-ipojo-footer