Skip to content

Getting Started

Randgalt edited this page Aug 16, 2012 · 27 revisions

Quick Start

Governator is mostly transparent. Use Guice as you normally would but with enhanced features. The major difference is that you now create the Injector through Governator.

Injector    injector = LifecycleInjector.builder()
    .withModules(yourModules).createInjector();

By creating your Injector this way @PostProcess annotations will be processed.

Just a Bit More

By adding two more lines of code a number of other Governator enhancements are enabled.

LifecycleManager    manager = injector.getInstance(LifecycleManager.class);
manager.start();

Starting the Governator LifecycleManager causes field validation to be processed and the Governator @WarmUp methods to get executed.

When your application is shutting down, stop the LifecycleManager so that @CoolDown and @PreDestroy methods are executed.

manager.close();

AutoBindSingleton

While Guice has some support for automatic binding (Just in Time – JIT – binding), it’s functionality is limited. Further, many Guice users turn off JIT and require explicit binding. Governator normalizes automatic binding via classpath scanning and the @AutoBindSingleton annotation.

Classpath scanning is done in a manner similar to Jersey scanning. You specify a set of base packages and the Classpath is scanned for matching classes.

LifecycleInjector.builder().usingBasePackages(yourPackages)

All classes in the specified packages (searched recursively) annotated with @AutoBindSingleton are bound as Guice singletons. By default, the binding is lazy. i.e. the singleton is only created if it’s needed (as an injection into another instance, etc.). However, each @AutoBindSingleton can optionally be set as an eager singleton.

Next Steps

Governator has many more features than the above. See the feature specific wikis to learn more detail about the above features or additional features: