Oracle GlassFish Server 3.0.1 Add-On Component Development Guide


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:

ConfigService config;

The following example shows how to use @Extract at the getter level:

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.

protected Habitat habitat;
public void doSomething(String name) {
	ConfigService config = habitat.getComponent(ConfigService.class);