Over a million developers have joined DZone.

Learning Netflix Governator - Part 2

DZone's Guide to

Learning Netflix Governator - Part 2

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

To continue from the previous entry on some basic learnings on Netflix Governator, here I will cover one more enhancement that Netflix Governatorbrings to Google Guice - Lifecycle Management

Lifecycle Management essentially provides hooks into the different lifecycle phases that an object is taken through, to quote the wiki article on Governator: 

Allocation (via Guice)
Pre Configuration
Set Resources
Post Construction
Validation and Warm Up
  -- application runs until termination, then... --    
Pre Destroy

To illustrate this, consider the following code:

package sample.gov;

import com.google.inject.Inject;
import com.netflix.governator.annotations.AutoBindSingleton;
import sample.dao.BlogDao;
import sample.model.BlogEntry;
import sample.service.BlogService;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@AutoBindSingleton(baseClass = BlogService.class)
public class DefaultBlogService implements BlogService {
    private final BlogDao blogDao;

    public DefaultBlogService(BlogDao blogDao) {
        this.blogDao = blogDao;

    public BlogEntry get(long id) {
        return this.blogDao.findById(id);

    public void postConstruct() {
        System.out.println("Post-construct called!!");
    public void preDestroy() {
        System.out.println("Pre-destroy called!!");

Here two methods have been annotated with @PostConstruct and @PreDestroy annotations to hook into these specific phases of the Governator's lifecycle for this object. The neat thing is that these annotations are not Governator specific but are JSR-250 annotations that are now baked into the JDK.

Calling the test for this class appropriately calls the annotated methods, here is a sample test:

mport com.google.inject.Injector;
import com.netflix.governator.guice.LifecycleInjector;
import com.netflix.governator.lifecycle.LifecycleManager;
import org.junit.Test;
import sample.service.BlogService;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

public class SampleWithGovernatorTest {

    public void testExampleBeanInjection() throws Exception {
        Injector injector  = LifecycleInjector

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


        BlogService blogService = injector.getInstance(BlogService.class);
        assertThat(blogService.get(1l), is(notNullValue()));

Spring Framework has supported a  similar mechanism for a long time - so the exact same JSR-250 based annotations work for Spring bean too.

If you are interested in exploring this further,  here is my github project with samples with Lifecycle management.

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.


Published at DZone with permission of Biju Kunjummen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}