Sun GlassFish Enterprise Server v3 Add-On Component Development Guide

Instantiation Cascading in HK2

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.


Example 2–2 Example of Instantiation Cascading

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 ... {...}