in the service through the service registry DeclarationBuilderService service = ... // Get a fresh instance builder DeclarationBuilder builder = service.newInstance("the.full.name.of.the.component.to.instantiate"); DeclarationHandle handle = builder.name("a-unique-name") // Make sure name is unique for the expected type .configure() .property("a-property", "a-value") .property("another-property", "another-value") .build(); // Push the InstanceDeclaration service in the registry handle.publish(); Except the {@link InstanceBuilder#build()} call, all methods are optional:
- {@link InstanceBuilder#name(String)}: if no name is provided, a default one will be generated by iPOJO.
- {@link InstanceBuilder#version(String)}: if no version is provided, the first un-versioned type will be used.
- {@link InstanceBuilder#configure()}: if no configuration is required, can be omitted.
- {@link InstanceBuilder#type(String)}: can be used to change the required component type.
- {@link InstanceBuilder#context(org.osgi.framework.BundleContext)}: by default, the bundle context used to register the created {@link org.apache.felix.ipojo.extender.Declaration} is the one of thecaller. It can be changed though this method.
Once an instance handle has been created, its configuration (name, type, version and properties) is immutable. It can only be {@linkplain DeclarationHandle#publish() published} (so that the framework will try to instantiate theinstance) or {@linkplain DeclarationHandle#retract() retracted} (framework will remove the instance).Notice that all created instances will appear as "coming from" the bundle that requires the {@link DeclarationBuilderService} service. Just like having a {@literal metadata.xml} file in yourbundle that declares your instances. It is possible to override this default behavior using {@link InstanceBuilder#context(org.osgi.framework.BundleContext)}.
@see org.apache.felix.ipojo.extender.InstanceDeclaration
@see org.apache.felix.ipojo.extender.TypeDeclaration
@see org.apache.felix.ipojo.extender.ExtensionDeclaration
@see org.apache.felix.ipojo.extender.InstanceBuilder
@see org.apache.felix.ipojo.extender.ConfigurationBuilder
@see org.apache.felix.ipojo.extender.DeclarationHandle
@since 1.11.2