Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dagger for a Servlet Example
import dagger.ObjectGraph;
public abstract class BaseServlet extends HttpServlet {
private ObjectGraph graph;
/**
* inits the Servlet with the object graph from Dagger
* if you override this, be sure your implementation calls that of this super class
*
* @param config
*/
public void init(ServletConfig config) {
this.graph = (ObjectGraph) config.getServletContext().getAttribute(DIListener.ATTR_OBJECT_GRAPH);
}
/**
* used to get injected instances
*
* @param arg
* @return
*/
public <T> T get(Class<T> arg) {
return this.getObjectGraph().get(arg);
}
}
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import dagger.ObjectGraph;
/**
* DaggerListener - inits objectgraph and sets it in ServletContext, so it can be accessed in servlet's init() method
*
* @author Erik Schultink <erik@engetc.com>
*/
public class DIListener implements ServletContextListener {
static final public String ATTR_OBJECT_GRAPH = "ObjectGraph";
ObjectGraph objectGraph;
/***
* called when servlet initialized
*
* @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
*/
@Override
public void contextInitialized(ServletContextEvent sce) {
//your object graph is initialized here, from your Dagger Module(s)
this.objectGraph = ObjectGraph.create(new ProductionModule());
sce.getServletContext().setAttribute(ATTR_OBJECT_GRAPH, this.objectGraph);
}
/**
* no-op
*
* @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
*/
@Override
public void contextDestroyed(ServletContextEvent sce) {
//do nothing
}
}
public class ExampleServlet extends BaseServlet {
//some dependency that the servlet needs
private Dependency dependency;
public void init(ServletConfig config) {
super.init(config);
this.dependency = this.get(Dependency.class);
}
//you're now free to implement doGet()/etc as you wish
//dependency should be defined, with the binding provided by the Dagger Module
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.