Sun GlassFish Enterprise Server v3 Add-On Component Development Guide

HK2 Lifecycle Interfaces

Components can attach behaviors to their construction and destruction events by implementing the org.jvnet.hk2.component.PostConstruct interface, the org.jvnet.hk2.component.PreDestroy interface, or both. These are interfaces rather than annotations for performance reasons.

The PostConstruct interface defines a single method, postConstruct, which is called after a component has been initialized and all its dependencies have been injected.

The PreDestroy interface defines a single method, preDestroy, which is called just before a component is removed from the system.


Example 2–1 Example Implementation of PostContruct and PreDestroy

@Service(name="com.example.container.MyContainer")
public class MyContainer implements Container, PostConstruct, PreDestroy {
	@Inject
	Logger logger;
	...
	public void postConstruct() {
		logger.info("Starting up.");
	}

	public void preDestroy() {
		logger.info("Shutting down.");
	}
}