The reason for having both an init
and start
method is as follows. The dependency manager allows you to declaratively define components and their dependencies in (Java) code. This can be done when you start the bundle, but sometimes a component might have a dependency that depends on some configuration, or one of its other dependencies. In other words, you might have dependencies that you want to add to the component at runtime.
In such cases, what you can do is define an init(Component c)
method on your component implementation. This method will be invoked after your component has been initialized and all its required dependencies have been injected (or their callbacks invoked). So at that point you have access to such dependencies in your component and you could decide, based on information obtained through such dependencies, to dynamically add yet another component, like this:
public volatile OtherService s; // an injected service dependency
public void init(Component c) {
if (s.needsSomeService()) {
DependencyManager dm = c.getDependencyManager();
c.add(dm.createServiceDependency()
.setService(SomeService.class)
.setInstanceBound(true)
.setRequired(true));
}
}
Once SomeService
becomes available, the start()
method will be invoked and your component becomes available (meaning if it publishes a service, that will be done now).
In short: the init
method exists to allow you to manipulate your own component definition and add extra dependencies dynamically at runtime. The start
method is then invoked once all dependencies are available.
In simple scenarios, where you have no such requirements, using either method to do your initialization is fine.