I don't know how to proceed with a simple guice example. After reading the documentation I've done the following:
GuiceServletContextListener
and added the listener to web.xmlserve("*.jsp").with(IndexController.class);
in configure servletsAfter I've done that how do I use dependency injection? Let's say I have an index.jsp, IndexController.class (servlet), and two classes called Person and Order with Person depending on Order. How do I inject the Order dependency into the Person constructor via guice and after I do that I would need to return say a list of this person's orders back to the controller? I've used Ninject with ASP.NET MVC in the past and that was pretty simple, but I'm very confused on how to implement even the simplest DI example with Guice. Thanks.
Guice Servlet provides a complete story for use in web applications and servlet containers. Guice's servlet extensions allow you to completely eliminate web. xml from your servlet application and take advantage of type-safe, idiomatic Java configuration of your servlet and filter components.
Use of Google Guice for implementing dependency injection in application is very easy and it does it beautifully. It's used in Google APIs so we can assume that it's highly tested and reliable code. Download the project from above and play around with it to learn more.
Beyond Dependency Injection, the benefits of using Google Guice is: Guice has a very clean implementation of constructor Injection. As you can see from the example you just add @Inject annotation constructor. Guice also has setter Injection using the same annotation.
Google Guice (pronounced like "juice") is an open-source software framework for the Java platform released by Google under the Apache License. It provides support for dependency injection using annotations to configure Java objects.
To get started, here's an example that injects a service returning a list of names into an index controller. (No trickery in this example, everything is explicit.)
ListService
interface defines simple service.
public interface ListService {
List<String> names();
}
DummyListService
provides trivial implementation.
public class DummyListService implements ListService {
public List<String> names() {
return Arrays.asList("Dave", "Jimmy", "Nick");
}
}
ListModule
wires ListService
to the dummy implementation.
public class ListModule extends AbstractModule {
@Override
protected void configure() {
bind(ListService.class).to(DummyListService.class);
}
}
GuiceServletContextListener
implementation maps a servlet to index, and creates a ListModule
as above.
@Override
protected Injector getInjector() {
return Guice.createInjector(
new ServletModule() {
@Override protected void configureServlets() {
serve("/index.html").with(IndexController.class);
}
},
new ListModule());
}
IndexController
puts the names into the request scope (manually) and forwards to a JSP page.
@Singleton
public class IndexController extends HttpServlet {
@Inject ListService listService;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("names", listService.names());
req.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(req, resp);
}
}
JSP page dumps the names (fragment only).
<c:forEach items="${names}" var="name">
${name}<br/>
</c:forEach>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With