Inversion of control (IoC) refers to a style of software architecture where the behavior of a system is determined by the runtime capabilities of the individual, discrete components that make up the system. This architecture is different from traditional styles of software architecture, where all the components of a system are specified at design-time. With IoC, discrete components respond to high-level events to perform actions. While performing these actions, the components typically rely on other components to provide other actions. In an IoC system, components use injection to gain access to other components, and extraction to make component variables available to the system.
Services usually rely on other services to perform their tasks. The HK2 runtime identifies the @Contract implementations required by a service by using the org.jvnet.hk2.annotations.Inject annotation. Inject can be placed on fields or setter methods of any service instantiated by the HK2 runtime. The target service is retrieved and injected during the calling service's instantiation by the component manager.
The following example shows how to use @Inject at the field level:
@Inject ConfigService config;
The following example shows how to use @Inject at the setter level:
@Inject public void set(ConfigService svc) {...}
Injection can further qualify the intended injected service implementation by using a name and scope from which the service should be available:
@Inject(Scope=Singleton.class, name="deploy") AdminCommand deployCommand;
Although all services are automatically placed into a scope for later retrieval, a component may need to extract more than itself. One practical way of doing so is to use a factory service. For simplicity, however, the HK2 runtime extracts all fields or getter methods annotated with the org.jvnet.hk2.annotations.Extract annotation.
The following example shows how to use @Extract at the field level:
@Extract ConfigService config;
The following example shows how to use @Extract at the getter level:
@Extract public ConfigService getConfigService() {...}
Extraction, like injection, can also use the name and scope annotation fields to further qualify the extracted Contract implementation.
Extracted fields and properties are made available to other service instances by exporting them to the org.jvnet.hk2.component.Habitat instance. Habitat instances can be injected into other components, and the components can then extract and use the data contained in the Habitat instance.
@Inject protected Habitat habitat; ... public void doSomething(String name) { ... ConfigService config = habitat.getComponent(ConfigService.class); ... }
Injection of instances that have not been already instantiated triggers more instantiation. You can see this as a component instantiation cascade where some code requests for a high-level service will, by using the @Inject annotation, require more injection and instantiation of lower level services. This cascading feature keeps the implementation as private as possible while relying on interfaces and the separation of contracts and providers.
The following example shows how the instantiation of DeploymentService as a Startup contract implementation will trigger the instantiation of the ConfigService.
@Contract public interface Startup {...}
Iterable<Startup> startups; startups = componentMgr.getComponents(Startup.class);
@Service public class DeploymentService implements Startup { @Inject ConfigService config; }
@Service public Class ConfigService implements ... {...}