What is the best way to inject dependencies into Servlets when you don't want to use any sort of DI frameworks? Shall I put them into the ServletContext
in a ServletContextListener
?

- 59,493
- 71
- 188
- 276
-
2Not even the DI framework that is part of JavaEE 6? – skaffman May 05 '11 at 14:50
-
@skaffman I only need to inject 2-3 dependencies. I'm unsure if the added complexity is worth the effort. – helpermethod May 05 '11 at 14:55
-
@skaffman: You probably don't want to use a DI framework if you're building a simple servlet-based library or plugin. Not that this is the case, but just pointing out other reasons. – Jeremy May 05 '11 at 14:58
-
@skaffman: Jetty is far from Java EE 6. – BalusC May 05 '11 at 15:23
-
What kind of dependencies? Does Jetty support JNDI? If so, just `@Resource` ought to be enough. – BalusC May 05 '11 at 15:24
-
@BalusC I use @Resource for the DataSource, which works fine but I also need inject classes of my own, which isn't possible in Jetty (AFAIK). – helpermethod May 05 '11 at 17:27
-
You could just define those classes in JNDI as well, exactly the same way as you did for `DataSource`. – BalusC May 05 '11 at 17:27
-
@BalusC Could you show a link/example of how to do this? I haven't found anything on the web. – helpermethod May 05 '11 at 17:32
-
1I don't do Jetty, so I can't go in detail. For Tomcat/Glassfish/JBoss/etc, I'd just say, the same way as you did for `DataSource`. Just define it in JNDI and give it a JNDI name. It just becomes an applicationwide instance which is available by `@Resource`. Or is it really that tightcoupled/specific in Jetty? Setting as a `ServletContext` attribute is then your best resort. That's already been answered. Here's another example from my hand: http://stackoverflow.com/questions/4491596/get-database-connection-from-a-connection-pool/4492111#4492111 – BalusC May 05 '11 at 17:33
-
@BalusC I'll just try that out. Would be great if that would work! – helpermethod May 05 '11 at 17:56
2 Answers
Yes. You could initialize them in a ServletContextListener
(if you need them pre-initialized) and then put them into the ServletContext
for all your servlets to access.
It's probably best to store the objects by their class name so retrieval is type-safe.
Foo foo = servletContext.getAttribute(Foo.class.getName());

- 22,188
- 4
- 68
- 81
To inject something in servlets, you need to get the servlet instances in another class. And you can't do that, because the getServlet(name)
method is deprecated (and not working).
So each servlet will have to register itself manually in the context. In the init()
method of each servlet you can add itself to a collection in the servlet context:
((List<HttpServlet>) servletContext.getAttribute("servlets")).add(this);
Then, in a ServletContextListener
you can loop all registered servlets and invoke some setters, or user reflection, to externally set the dependencies.
But..that seems too complicates, so you might stick with the new
operator here and there.

- 588,226
- 146
- 1,060
- 1,140