Sun GlassFish Enterprise Server v3 Add-On Component Development Guide

Extraction

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);
	...
}