从 Waffle/Pico 转换为 Struts2/Guice

发布于 2024-07-30 19:51:50 字数 174 浏览 3 评论 0原文

我的任务是将我自己在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮生面具三千个 2024-08-06 19:51:50

是的,我意识到这个问题有点模糊,但我最终还是完成了。 DI 的 Waffle/Pico 机制利用一个名为 Registrar 的类来设置您想要注入的类及其在 Web 应用程序中的范围。 他们网站上的示例是:

 public class MyRegistrar extends AbstractRegistrar {  

   public MyRegistrar(Registrar delegate) {  
     super(delegate);  
   }  

   public void application() {  
     register("helloworld", HelloWorldController.class);  
   }  
 } 

此示例显示了一个应用程序范围的类 - 对于会话和请求范围,您只需将它们放在相关的 session()request() 中方法。

对于 Struts2 和 Guice,事情的结构略有不同。 首先使用 Guice,它使用构造函数上方的 @Inject 注释注入依赖项。 注入配置是通过名为 Modules 的类完成的,这些类必须重写名为 configure() 的方法,以便将接口绑定到它们的类 - 下面给出了 Google 的示例。

public class BillingModule extends AbstractModule {
  @Override 
  protected void configure() {
    bind(TransactionLog.class).to(DatabaseTransactionLog.class);
    bind(CreditCardProcessor.class).to(PaypalCreditCardProcessor.class);
    bind(BillingService.class).to(RealBillingService.class);
  }
}

范围也在这些模块中配置。 Singleton 实际上是一个应用程序范围的类,可以这样指定:

bind(TransactionLog.class).to(InMemoryTransactionLog.class).in(Singleton.class);

但是 Guice 也有 SessionScoped.class 和 RequestScoped.class 因此转换相当简单。

关于 Struts2,那里使用的依赖注入实际上是最终成为 Guice 的早期版本,因此问题是将此行添加到 struts.xml

<constant name="struts.objectFactory" value="guice" />

并在 web.xml 中指定 StrutsPrepareAndExecuteFilter

<filter>         
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class>
</filter>     
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/* </url-pattern>
</filter-mapping>

这应该足以让任何人使用类似的设置问题开始了。

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 :

 public class MyRegistrar extends AbstractRegistrar {  

   public MyRegistrar(Registrar delegate) {  
     super(delegate);  
   }  

   public void application() {  
     register("helloworld", HelloWorldController.class);  
   }  
 } 

This example shows an application-scoped class - for session and request scopes, you simply place them inside the relevant session() or request() 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 called configure() in order to bind interfaces to their classes - Google's example is given below.

public class BillingModule extends AbstractModule {
  @Override 
  protected void configure() {
    bind(TransactionLog.class).to(DatabaseTransactionLog.class);
    bind(CreditCardProcessor.class).to(PaypalCreditCardProcessor.class);
    bind(BillingService.class).to(RealBillingService.class);
  }
}

Scoping is also configured in these modules. A Singleton is effectively an application-scoped class and can be specified like this:

bind(TransactionLog.class).to(InMemoryTransactionLog.class).in(Singleton.class);

But Guice also has SessionScoped.class and RequestScoped.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

<constant name="struts.objectFactory" value="guice" />

And specifying the StrutsPrepareAndExecuteFilter in the web.xml

<filter>         
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class>
</filter>     
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/* </url-pattern>
</filter-mapping>

That should be enough to get anyone with a similar setup issue started.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文