如何使用Dagger注入Servlet? [英] How do I inject into a Servlet with Dagger?

查看:70
本文介绍了如何使用Dagger注入Servlet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Dagger将对象注入到Servlet中?

How do I inject objects into a Servlet using Dagger?

由于Servlet容器实例化了Servlet本身,因此它们不是使用Dagger创建的。因此,我能看到的唯一注入机制是通过静态注入,匕首主页警告不要这样做。

Since the servlet container instantiates the Servlets themselves, they are not created with Dagger. Therefore, the only mechanism I can see to inject into them is via static injections, which the dagger homepage warns against doing. Is there another (best practices) way to do it?

具体地说,我使用的是Jetty和GWT(我的servlet扩展了RemoteServiceServlet),但是我不认为这些细节

Specifically, I am using Jetty and GWT (my servlets extend RemoteServiceServlet), but I don't think those details matter.

推荐答案

(目前)尚无任何基础架构代码可支持Dagger的Java EE servlet堆栈。

There is not (yet) any stock infrastructure code to support a Java EE servlet stack for Dagger.

这就是说,有一些方法可以自制,直到我们了解为止。如果仅将其用于单例,则可以镜像某些人在android上的操作,并在应用程序启动时使用上下文侦听器初始化图形,然后使用Servlet的init()方法自我注入

That said, there are ways you could home-brew it until we get to it. If you were using it only for singletons, then you could mirror what some people are doing on android, and initialize your graph at app startup using a context listener, then use the Servlet's init() method to self-inject

当您尝试向请求添加作用域时,这将变得非常棘手,这并非没有可能,但需要更多的脚手架。

It gets much trickier when you try to add scoping to requests and such - not impossible, but it requires more scaffolding.

这篇关于如何使用Dagger注入Servlet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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