从 Waffle/Pico 转换为 Struts2/Guice
我的任务是将我自己在 Waffle 框架中使用 PicoContainer 作为 DI 机制开发的应用程序转换为我们的新“堆栈”,即使用 Struts2 作为框架,并使用 Guice 作为 DI 机制。 对于如何以最小的痛苦做到这一点,同时充分利用新框架和 DI 机制,是否有人有任何有用的指示?
提前谢谢了。
I've been tasked with converting an application which was developed by myself in the Waffle Framework using PicoContainer as a DI mechanism into our new "stack" which is to use Struts2 as a framework with Guice as the DI mechanism. Does anyone out there have any helpful pointers as to how to do this with minimal pain and at the same time getting the best out of both the new framework and DI mechanism?
Many thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,我意识到这个问题有点模糊,但我最终还是完成了。 DI 的 Waffle/Pico 机制利用一个名为 Registrar 的类来设置您想要注入的类及其在 Web 应用程序中的范围。 他们网站上的示例是:
此示例显示了一个应用程序范围的类 - 对于会话和请求范围,您只需将它们放在相关的
session()
或request()
中方法。对于 Struts2 和 Guice,事情的结构略有不同。 首先使用 Guice,它使用构造函数上方的
@Inject
注释注入依赖项。 注入配置是通过名为 Modules 的类完成的,这些类必须重写名为configure()
的方法,以便将接口绑定到它们的类 - 下面给出了 Google 的示例。范围也在这些模块中配置。 Singleton 实际上是一个应用程序范围的类,可以这样指定:
但是 Guice 也有 SessionScoped.class 和 RequestScoped.class 因此转换相当简单。
关于 Struts2,那里使用的依赖注入实际上是最终成为 Guice 的早期版本,因此问题是将此行添加到 struts.xml
并在 web.xml 中指定 StrutsPrepareAndExecuteFilter
这应该足以让任何人使用类似的设置问题开始了。
Right, I realise the question was a bit vague, but I did get it done in the end. The Waffle/Pico mechanism for DI utilises a class called a Registrar to set up the classes you want injected and their scope within the webapp. The example on their site is :
This example shows an application-scoped class - for session and request scopes, you simply place them inside the relevant
session()
orrequest()
methods.With Struts2 and Guice, things are structured a little differently. Taking Guice first, it injects dependencies using the
@Inject
annotation above the constructor. The injection configuration is done via classes called Modules, which must override a method calledconfigure()
in order to bind interfaces to their classes - Google's example is given below.Scoping is also configured in these modules. A Singleton is effectively an application-scoped class and can be specified like this:
But Guice also has
SessionScoped.class
andRequestScoped.class
so the transition is fairly trivial.Regarding Struts2, the dependency injection used there was in fact an early version of what eventually became Guice, so it becomes a question of adding this line to struts.xml
And specifying the StrutsPrepareAndExecuteFilter in the web.xml
That should be enough to get anyone with a similar setup issue started.