用 Guice 注入许多装饰实例——关于将 HessianServlet 与 Guice 混合

发布于 2025-01-04 15:39:32 字数 1783 浏览 1 评论 0原文

给定 Guice servlet 模块 configureServlets() 中的以下代码:

serve("/a").with(new Decorator(new A()));
serve("/b").with(new Decorator(new B()));
...
serve("/z").with(new Decorator(new Z()));
...

正确注入所有这些的最佳/最简单的解决方案是什么? (即在各种装饰类 A、B、C 中注入字段...)

我正在考虑使用命名提供程序,但我希望不必为每个 A、B、C 定义一个新的提供程序类...理想的解决方案因此将是通用的。


关于将 Hessian 与 GUICE 混合...

为了帮助精确解决问题,实际代码将 Web 服务与 Hessian 一起使用。在网上进行挖掘后,目前这个问题没有正确的答案(我见过很多丑陋的黑客),Guillaume给出的解决方案正好符合这个问题案件。

总结一下最初的问题:要使用 Hessian 实现 Web 服务,必须实现一个 HessianServlet,它是一个 GenericServlet;然而Guice只能绑定HttpServlet。因此,诀窍是实现一个薄包装器 HessianHttpServlet

class HessianHttpServlet extends HttpServlet {
    public HessianHttpServlet(HessianServlet hessianServlet) {
        this.hessianServlet = hessianServlet;
    }
    @Override public void init(ServletConfig config) throws ServletException {
        hessianServlet.init(config);
    }
    @Override public void service(ServletRequest request, ServletResponse response) {                 
        hessianServlet.service(request, response);
    }
}

并调用:

serve("/a").with(new HessianHttpServlet(new WebServiceA()));

在这种情况下,完整的解决方案是注入 WebServiceX

void configureServlet() {
    serve("/a").with(new HessianHttpServlet(inject(new WebServiceA())));
    serve("/b").with(new HessianHttpServlet(inject(new WebServiceB())));
    ...
    serve("/z").with(new HessianHttpServlet(inject(new WebServiceZ())));
}
private HessianServlet inject(HessianServlet hessianServlet) {
    requestInjection(hessianServlet);
    return hessianServlet;
}

Given the following code in a Guice servlet module configureServlets():

serve("/a").with(new Decorator(new A()));
serve("/b").with(new Decorator(new B()));
...
serve("/z").with(new Decorator(new Z()));
...

What would be the best / easiest solution to properly inject all this? (i.e. injecting fields in the various decorated classes A, B, C...)

I was thinking using named providers, but I would prefer not having to define a new provider class for each A, B, C... The ideal solution would be thus generic.


ON MIXING HESSIAN WITH GUICE...

To help precise the question, the real code uses web-services with Hessian. After digging on the net there is currently no proper answer on this problem (I've seen many ugly hacks), and the solution given by Guillaume fits the bill on this precise case.

To summarize the original problem: to implement a web-service using Hessian, one must implement an HessianServlet, which is a GenericServlet; however Guice can only bind HttpServlet. The trick was thus to implement a thin wrapper HessianHttpServlet:

class HessianHttpServlet extends HttpServlet {
    public HessianHttpServlet(HessianServlet hessianServlet) {
        this.hessianServlet = hessianServlet;
    }
    @Override public void init(ServletConfig config) throws ServletException {
        hessianServlet.init(config);
    }
    @Override public void service(ServletRequest request, ServletResponse response) {                 
        hessianServlet.service(request, response);
    }
}

And calling:

serve("/a").with(new HessianHttpServlet(new WebServiceA()));

The complete solution is thus in that case to inject WebServiceX :

void configureServlet() {
    serve("/a").with(new HessianHttpServlet(inject(new WebServiceA())));
    serve("/b").with(new HessianHttpServlet(inject(new WebServiceB())));
    ...
    serve("/z").with(new HessianHttpServlet(inject(new WebServiceZ())));
}
private HessianServlet inject(HessianServlet hessianServlet) {
    requestInjection(hessianServlet);
    return hessianServlet;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

我喜欢麦丽素 2025-01-11 15:39:32

您可以在每个装饰器上使用 requestInjection(Object instance)

You can use requestInjection(Object instance) on each of your decorators.

情深缘浅 2025-01-11 15:39:32

我创建了一个小型开源项目,可以轻松集成 hessian 和 guice。您可以使用基于注释的配置,如下所示:
WebService:

@HessianWebService
public class UserServiceImpl implements UserService {
    ...
}

Guice 配置:

public class WebServiceGuiceServletContextListener extends GuiceServletContextListener {
    @Override
    protected Injector getInjector() {
        return Guice.createInjector(
                /* your guice modules */
                new HessianWebServicesModule("your web service implementations package")
        );
    }
}

或使用 EDSL 的手动方式:

public class WebServiceGuiceServletContextListener extends GuiceServletContextListener {
    @Override
    protected Injector getInjector() {
        return Guice.createInjector(
                /* your guice modules */
                new HessianWebServicesModule(){
                    @Override
                    protected void configureHessianWebServices() {
                        serveHessianWebService(UserService.class).usingUrl("/Users");
                    }
                }
        );
    }
}

更多信息、配置选项和完整示例可在此处找到:https://bitbucket.org/richard_hauswald/hessian-guice/

I created a little open source project which enables easy integration of hessian and guice. You can use annotation based configuration like this:
WebService:

@HessianWebService
public class UserServiceImpl implements UserService {
    ...
}

Guice configuration:

public class WebServiceGuiceServletContextListener extends GuiceServletContextListener {
    @Override
    protected Injector getInjector() {
        return Guice.createInjector(
                /* your guice modules */
                new HessianWebServicesModule("your web service implementations package")
        );
    }
}

or the manual way using the EDSL:

public class WebServiceGuiceServletContextListener extends GuiceServletContextListener {
    @Override
    protected Injector getInjector() {
        return Guice.createInjector(
                /* your guice modules */
                new HessianWebServicesModule(){
                    @Override
                    protected void configureHessianWebServices() {
                        serveHessianWebService(UserService.class).usingUrl("/Users");
                    }
                }
        );
    }
}

More information, configuration options and complete examples are available here: https://bitbucket.org/richard_hauswald/hessian-guice/

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