简体   繁体   English

Guice Servlets的简单示例

[英]Simple Example with Guice Servlets

I don't know how to proceed with a simple guice example. 我不知道如何处理一个简单的guice示例。 After reading the documentation I've done the following: 阅读完文档后,我完成了以下工作:

  • setup the guiceFilter 设置guiceFilter
  • created an injector and instantiated a new ServletModule in a GuiceServletContextListener and added the listener to web.xml 创建了一个注入器并在GuiceServletContextListener实例化了一个新的ServletModule,并将该侦听器添加到web.xml
  • bound serve("*.jsp").with(IndexController.class); bound serve("*.jsp").with(IndexController.class); in configure servlets 在配置servlet中

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

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

DummyListService provides trivial implementation. DummyListService提供了简单的实现。

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

ListModule wires ListService to the dummy implementation. ListModule ListService连接到虚拟实现。

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. GuiceServletContextListener实现将servlet映射到索引,并如上所述创建ListModule

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM