Aleksandr
Aleksandr

Reputation: 1465

Programmatically create ipojo component instances when user request

Uses Apache Felix 4.2.1 iPOJO 1.11.0.

Requires programmatically create component instances when the user request. But i can not use none-static field in static factory method.

@Component
@Provides(specifications = {IProcessSearch.class})
public class ProcessSearch implements IProcessSearch {
    ...
    @Requires(filter = "(factory.name=ProcessSearch)")
    private Factory mProcessSearchFactory;
    ...
    /**
     * Factory methods for receive new component instance:
     */
    public static ProcessSearch createInstance() {
        return createInstance(null);
    }
    public static ProcessSearch createInstance(Properties pProperties) {
        InstanceManager lInstanceManager = (InstanceManager) mProcessSearchFactory.createComponentInstance(pProperties);
        return (ProcessSearch) lInstanceManager.getPojoObject();
    }

1) If I understand correctly, then field with @Requires annotation can not be static. How to create factory method which take properties and receive new component instance?

2) Is it normal practice to do so?

Upvotes: 1

Views: 541

Answers (1)

Clement
Clement

Reputation: 3192

Do do that, you need to provide your own creation strategy. This strategy is applied on the provided service (@Provides) and not on the consumer side (@Requires).

More info on: http://felix.apache.org/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/providing-osgi-services.html#service-serving-object-creation

Upvotes: 1

Related Questions