Guice Servlets的简单示例 [英] Simple Example with Guice Servlets

查看:116
本文介绍了Guice Servlets的简单示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何处理一个简单的guice示例。阅读完文档后,我完成了以下工作:

I don't know how to proceed with a simple guice example. After reading the documentation I've done the following:


  • 设置guiceFilter

  • 创建了一个注入器并在 GuiceServletContextListener 中实例化一个新的ServletModule,并将监听器添加到web.xml

  • 绑定 serve( * .jsp)。with(IndexController.class); in configure servlets

  • setup the guiceFilter
  • created an injector and instantiated a new ServletModule in a GuiceServletContextListener and added the listener to web.xml
  • bound serve("*.jsp").with(IndexController.class); in configure servlets

我之后完成了如何使用依赖注入?假设我有一个index.jsp,IndexController.class(s​​ervlet),以及两个名为Person和Order with Person的类,具体取决于Order。如何通过guice将Order依赖注入到Person构造函数中,在我这样做之后,我需要返回说这个人的命令列表回到控制器?我在过去使用过Ninject和ASP.NET MVC这很简单,但我对如何用Guice实现最简单的DI示例感到很困惑。谢谢。

After 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.

推荐答案

首先,这是一个注入服务的示例,该服务将名称列表返回到索引控制器中。 (在这个例子中没有任何技巧,一切都是明确的。)

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 接口定义简单服务。

ListService interface defines simple service.

public interface ListService {
    List<String> names();
}

DummyListService 提供简单的实现。

DummyListService provides trivial implementation.

public class DummyListService implements ListService {
    public List<String> names() {
        return Arrays.asList("Dave", "Jimmy", "Nick");
    }
}

ListModule 连接 ListService 到虚拟实现。

ListModule wires ListService to the dummy implementation.

public class ListModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(ListService.class).to(DummyListService.class);
    }
}

GuiceServletContextListener implementation将servlet映射到索引,并创建一个 ListModule ,如上所述。

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 将名称放入请求范围(手动)并转发到JSP页面。

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页面转储名称(仅限片段)。

<c:forEach items="${names}" var="name">
  ${name}<br/>
</c:forEach>

这篇关于Guice Servlets的简单示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆